package com.sipai.jspatrol.Patrol;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.Patrol;
import com.sipai.jspatrol.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by lvliming on 2019/2/19.
 */

public class ProPatrolTaskAdapter extends RecyclerView.Adapter<ProPatrolTaskAdapter.ViewHolder> {
    private List<Patrol> list;
    private Context context;

    public ProPatrolTaskAdapter(List<Patrol> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ProPatrolTaskAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pro_patrol_task_list, parent, false);
        final ViewHolder viewHolder = new ViewHolder(view);
        viewHolder.task.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = viewHolder.getAdapterPosition();
                Patrol item = list.get(position);
                actionStart(item);
            }
        });
        return viewHolder;
    }

    private void actionStart(Patrol item) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        Calendar cnow = Calendar.getInstance();
        Calendar cstart = Calendar.getInstance();
        Calendar cend = Calendar.getInstance();
        int result = 0;
        int result1 = 0;
        try {
            cnow.setTime(curDate);
            cstart.setTime(df.parse(item.getStartTime()));
            cend.setTime(df.parse(item.getEndTime()));
            result = cnow.compareTo(cstart);
            result1 = cnow.compareTo(cend);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        PatrolDetailActivity.actionStart(context,item);
//        if (result < 0) {
//            Toast.makeText(context, "任务尚未开始", Toast.LENGTH_SHORT).show();
//        } else {
//            if (result1 > 0) {
//                try {
//                    Date d1 = df.parse(item.getEndTime());
//                    Date d2 = new Date(System.currentTimeMillis());
//                    long diff = d2.getTime() - d1.getTime();
//                    long hours = diff / (1000 * 60 * 60);
//                    if (hours<1){
//                        if (item.getType().equals("P")) {
//                            PatrolDetailActivity.actionStart(context, item);
//                        }else if (item.getType().equals("E")){
////                            PatrolEquRepairActivity.actionStart(context, item.getId());
//                        }
//                    }else {
//                        if (item.getStatus().equals("4")||item.getStatus().equals("5")){
//                            Toast.makeText(context, "未巡检原因："+item.getWorkResult(), Toast.LENGTH_LONG).show();
//                        }else {
////                            UnfinishedPatrolTaskActivity.actionStart(context, item.getId(), item.getPatrolType());
//                        }
//                    }
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//            } else {
//                if (item.getStatus().equals("3")) {
//                if (item.getType().equals("P")) {
//                    PatrolDetailActivity.actionStart(context, item);
//                }else if (item.getType().equals("E")){
////                    PatrolEquRepairActivity.actionStart(context, item.getId());
//                }
//                } else if (item.getStatus().equals("1")) {
//                    PatrolDetailActivity.actionStart(context,item);
//                }else {
//                    PatrolDetailActivity.actionStart(context,item);
//                }
//            }
//        }
    }

    @Override
    public void onBindViewHolder(ProPatrolTaskAdapter.ViewHolder holder, int position) {
        Patrol item = list.get(position);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());
        Calendar cnow = Calendar.getInstance();
        Calendar cstart = Calendar.getInstance();
        Calendar cend = Calendar.getInstance();
        int result = 0;
        int result1 = 0;
        try {
            cnow.setTime(curDate);
            cstart.setTime(df.parse(item.getStartTime()));
            cend.setTime(df.parse(item.getEndTime()));
            result = cnow.compareTo(cstart);
            result1 = cnow.compareTo(cend);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (result < 0) {
            holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_notstarted));
            holder.tv_status.setTextColor(context.getResources().getColor(R.color.task_top_gray));
            holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_gray));
            holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_gray));
            holder.tv_status.setText("任务未开始");
        } else {
            if (result1 > 0) {
//                holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.red));
                if (item.getStatus().equals("4")){
                    holder.tv_status.setText("部分巡检");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_yellow));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_com_part));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_blue));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_red));

//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                }else if (item.getStatus().equals("5")){
                    holder.tv_status.setText("无法巡检");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_yellow));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_can_not));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                }else if (item.getStatus().equals("3")){
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.green));
                    holder.tv_status.setText("已完成");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_com));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_blue));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_blue));
                }else {
                    holder.tv_status.setText("任务已过期");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.task_top_little_red));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_out_of_date));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_red));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_red));
                }
            } else {
                if (item.getStatus().equals("3")) {
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.green));
                    holder.tv_status.setText("已完成");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.task_top_little_blue));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_com));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_blue));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_blue));
                } else if (item.getStatus().equals("1")||item.getStatus().equals("2")) {
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.title_blue));
                    holder.tv_status.setText("执行中");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.task_top_blue));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_doing));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_blue));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_blue));
                }else if (item.getStatus().equals("4")){
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                    holder.tv_status.setText("部分巡检");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_yellow));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_com_part));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.task_top_blue));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.task_top_little_red));
                }else if (item.getStatus().equals("5")){
                    holder.tv_status.setText("无法巡检");
                    holder.tv_status.setTextColor(context.getResources().getColor(R.color.error_yellow));
                    holder.tv_status.setBackground(context.getResources().getDrawable(R.drawable.bg_task_can_not));
                    holder.v_left.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                    holder.v_right.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
//                    holder.ll_all.setBackgroundColor(context.getResources().getColor(R.color.error_yellow));
                }
            }
        }
        holder.tv_title.setText(item.getName());
        holder.tv_time.setText("任务时限：" + item.getStartTime().substring(5, 16) + "——" + item.getEndTime().substring(5, 16));
        holder.tv_content.setText(item.getContent());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View task;
        LinearLayout ll_all;
        TextView tv_title, tv_status, tv_time, tv_content;
        View v_left,v_right;

        public ViewHolder(View itemView) {
            super(itemView);
            task = itemView;
            ll_all = itemView.findViewById(R.id.moudle_item_pro_patrol_task_ll_all);
            tv_title = itemView.findViewById(R.id.moudle_item_pro_patrol_task_tv_title);
            tv_status = itemView.findViewById(R.id.moudle_item_pro_patrol_task_tv_status);
            tv_time = itemView.findViewById(R.id.moudle_item_pro_patrol_task_tv_time);
            tv_content = itemView.findViewById(R.id.moudle_item_pro_patrol_task_tv_content);
            v_left = itemView.findViewById(R.id.moudle_item_patrol_task_left);
            v_right = itemView.findViewById(R.id.moudle_item_patrol_task_right);
        }
    }
}
