package com.xwkj.jcjcgz.mvp.ui.adapter;

import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.xwkj.jcjcgz.R;
import com.xwkj.jcjcgz.app.utils.StringUtil;
import com.xwkj.jcjcgz.mvp.model.entity.TaskDxzpEntity;
import com.xwkj.jcjcgz.mvp.model.entity.TaskEntity;

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

public class TaskDxzpAdapter extends BaseRecycleAdapter<TaskDxzpEntity.Task> {
    public TaskDxzpAdapter(List<TaskDxzpEntity.Task> datas) {
        super(datas);
    }

    @Override
    protected void bindData(BaseViewHolder holder, int position) {
        TaskDxzpEntity.Task task = datas.get(position);
        if (!TextUtils.isEmpty(task.getWarnAccount()) && task.getWarnAccount().length() == 18) {
            setItemImageView(holder.getView(R.id.item_dxzp_iv_phone), R.drawable.icon_phone);
            setItemText(holder.getView(R.id.item_dxzp_tv_phone), StringUtil.replaceId(task.getWarnAccount()));
        } else {
            setItemImageView(holder.getView(R.id.item_dxzp_iv_phone), R.drawable.icon_id);
            setItemText(holder.getView(R.id.item_dxzp_tv_phone), StringUtil.replaceString(task.getWarnAccount()));
        }
        Picasso.with(holder.getView(R.id.item_dxzp_iv_type).getContext()).load(task.getRiskFeaturesIcon()).into(((ImageView) holder.getView(R.id.item_dxzp_iv_type)));
        setItemText(holder.getView(R.id.item_dxzp_tv_type), task.getRiskFeatures());
        setItemText(holder.getView(R.id.item_dxzp_tv_status), TextUtils.isEmpty(task.getFkjg()) ? "待反馈" : task.getFkjg());
        String tip;
        if (!TextUtils.isEmpty(task.getFkjg())) {
            tip = "";
        } else {
            tip = calculateTime(task.getFkJzrq());
        }
        setItemText(holder.getView(R.id.item_dxzp_tv_time), "下发时间：" + task.getXfsj() + tip);

    }

    private String calculateTime(String end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return "";
        }
        long diffInMillies = new Date(date.getTime() + 1 * 24 * 60 * 60 * 1000).getTime() - System.currentTimeMillis();
        if (diffInMillies < 0) {
            return " (已超时)";
        } else {
            long num = diffInMillies / (1000 * 60 * 60 * 24);
            if (num < 1) {
                return " (需当天反馈)";
            } else {
                return " (需" + num + "天内反馈)";
            }
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.item_dxzp;
    }
}
