package com.qy.agv.activity.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qy.agv.R;
import com.qy.agv.activity.model.TaskModel;

import java.util.ArrayList;

public class MainListViewAdapter extends BaseAdapter {
    private ArrayList<TaskModel> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MainListViewAdapter(ArrayList<TaskModel> list, Context context) {
        this.mList=list;
        this.mContext=context;
        this.mLayoutInflater=LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return mList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        MainHolder mainHolder = null;
        final TaskModel taskModel = mList.get(position);
        if (view == null) {
            view=mLayoutInflater.inflate(R.layout.item_main_listview,null);
            mainHolder = new MainHolder();
            mainHolder.startTime = view.findViewById(R.id.tv_start_time);
            mainHolder.endTime = view.findViewById(R.id.tv_end_time);
            mainHolder.taskName = view.findViewById(R.id.tv_task_name);
            mainHolder.tv_robot_name = view.findViewById(R.id.tv_robot_name);
            mainHolder.tv_site = view.findViewById(R.id.tv_site);
            mainHolder.taskStatue = view.findViewById(R.id.tv_status);
            mainHolder.desc = view.findViewById(R.id.tv_desc);
            view.setTag(mainHolder);
        }else{
            mainHolder= (MainHolder) view.getTag();
        }
        mainHolder.startTime.setText(taskModel.getDistributionTime());
        mainHolder.taskName.setText(taskModel.getRequestType());
        mainHolder.tv_robot_name.setText(taskModel.getRobotId());
        mainHolder.tv_site.setText(taskModel.getLocationTo());
        StatusEnum status = StatusEnum.filter(taskModel.getRequestStatus());
        mainHolder.taskStatue.setTextColor(status.color);
        mainHolder.taskStatue.setText(status.text);
        mainHolder.desc.setText(taskModel.getRemark());
        return view;
    }

    static class MainHolder {
        public TextView startTime;
        public TextView endTime;
        public TextView taskName;
        public TextView taskStatue;
        public TextView desc;
        public TextView tv_robot_name;
        public TextView tv_site;
    }

    enum StatusEnum {
        STATUS_0(Color.BLUE, "就绪"),
        STATUS_1(Color.GREEN, "已调度"),
        STATUS_2(Color.GREEN, "执行中"),
        STATUS_3(Color.GREEN, "已完成"),
        STATUS_4(Color.BLACK, "已取消"),
        STATUS_5(Color.RED, "异常中"),
        STATUS_6(Color.BLUE, "等待中");

        private final Integer color;
        private final String text;

        StatusEnum(Integer color, String text) {
            this.color = color;
            this.text = text;
        }
        public static StatusEnum filter(int code){
            return StatusEnum.valueOf("STATUS_"+code);
        }
    }

}
