package com.tofly.ghrq_route.common.ui.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.plan.PlanTool;
import com.tofly.ghrq_route.module.plan.domain.OfflinePathBean;
import com.tofly.ghrq_route.module.plan.domain.PlanBean;
import com.tofly.ghrq_route.module.trouble.domain.TroubleBean;
import com.tofly.ghrq_route.common.util.app.AppUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/5/22.
 */

public class PlanPointListAdapter extends ARecyclerBaseAdapter<PlanPointListAdapter.MyViewHolder, OfflinePathBean> {

    private int selectedPos = -1;

    private PlanBean mPlan;

    public PlanPointListAdapter(Context context, List<OfflinePathBean> data, PlanBean plan) {
        super(context, data);
        mPlan = plan;
    }

    private OnNavigateClickListener mNavigateClickListener = null;
    public void setNavigateClickListener(OnNavigateClickListener clickListener){
        mNavigateClickListener = clickListener;
    }

    private OnRecordClickListener mRecordClickListener = null;
    public void setRecordClickListener(OnRecordClickListener clickListener){
        mRecordClickListener = clickListener;
    }

    public interface OnNavigateClickListener {
        void onNavigateClick(View view, int position);
    }

    public interface OnRecordClickListener {
        void onRecordClick(View view, int position);
    }

    public int selectItem(String pointId){
        for (int i = 0; i < mList.size(); i++){
            OfflinePathBean point = mList.get(i);
            if (point.getTemp_id().equals(pointId)){
                int position = i;
                selectedPos = position;
                notifyDataSetChanged();
                break;
            }
        }
        return selectedPos;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(mContext).inflate(R.layout.listview_plan_point_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(rootView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        OfflinePathBean planPoint = getItem(position);
        if (planPoint != null) {
            if (position == selectedPos){
                holder.layoutRoot.setBackgroundColor(mContext.getResources().getColor(R.color.bg));
            }else {
                holder.layoutRoot.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            }

            if (mPlan.isDeviceType()) { //设备
                String point_code = planPoint.getCode();
                if (point_code == null || point_code.isEmpty()) {
                    String pointId = planPoint.getPoint_id();
                    if (pointId != null && !pointId.isEmpty()) {
                        String sid = pointId;
                        int index = pointId.lastIndexOf("-");
                        if (index != -1) {
                            sid = pointId.substring(index + 1);
                        }
                        point_code = sid;
                    }
                }
                holder.textRepairCode.setText(point_code);
            }else if (mPlan.isDangerDeviceType()){  //隐患设备
                PlanTool planTool = PlanTool.getInstance();
                String trouble_id = planPoint.getTrouble_id();
                TroubleBean troubleBean = planTool.getLocalTrouble(trouble_id);
                String trouble_name = "";
                if (troubleBean != null){
                    trouble_name = troubleBean.getTroubleName();
                }
                if (trouble_name == null || trouble_name.isEmpty()){
                    trouble_name = planPoint.getTrouble_id();
                }
                holder.textRepairCode.setText(trouble_name);
            }
            holder.textRepairAddr.setText(planPoint.getAddr());

            int state = planPoint.getState();
            if (state == OfflinePathBean.POINT_STATE_FINISHED){
                holder.textRepairFinishTime.setText(planPoint.getFinish_time());
                holder.textProcessState.setText(mContext.getResources().getString(R.string.finished));
            }else {
                holder.textRepairFinishTime.setText("--");
                holder.textProcessState.setText(mContext.getResources().getString(R.string.unfinished));
            }

            if (onItemClickListener != null) {
                holder.itemView.setOnClickListener(v -> {
                    onItemClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    selectedPos = holder.getLayoutPosition();
                    notifyDataSetChanged();
                });
            }

            if (mNavigateClickListener != null){
                holder.textRepairNavigate.setOnClickListener(v -> {
                    mNavigateClickListener.onNavigateClick(holder.itemView, holder.getLayoutPosition());
                    selectedPos = holder.getLayoutPosition();
                    notifyDataSetChanged();
                });
            }

            String record_btn_name = mContext.getResources().getString(R.string.plan_route);
            if (mPlan.getDevice_type_id() == PlanBean.PlanDeviceType.PLAN_DEVICE_TYPE_YHZG){
                record_btn_name = mContext.getResources().getString(R.string.reported_clear_title);
            }
            holder.textRepairRecord.setText(record_btn_name);
            boolean isValid = checkPlanValid(planPoint);
            holder.textRepairRecord.setEnabled(isValid);
            if (mRecordClickListener != null){
                holder.textRepairRecord.setOnClickListener(v -> {
                    mRecordClickListener.onRecordClick(holder.itemView, holder.getLayoutPosition());
                    selectedPos = holder.getLayoutPosition();
                    notifyDataSetChanged();
                });
            }
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_detail_root)
        LinearLayout layoutRoot;
        @BindView(R.id.image_repair_icon)
        ImageView imageRepairIcon;
        @BindView(R.id.text_repair_code)
        TextView textRepairCode;
        @BindView(R.id.text_process_state)
        TextView textProcessState;
        @BindView(R.id.text_repair_finish_time)
        TextView textRepairFinishTime;
        @BindView(R.id.text_repair_addr)
        TextView textRepairAddr;
        @BindView(R.id.text_repair_navigate)
        TextView textRepairNavigate;
        @BindView(R.id.text_repair_record)
        TextView textRepairRecord;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    private boolean checkPlanValid(OfflinePathBean planPoint) {
        boolean isOutDate = false;
        boolean isOver = false;

        if (mPlan != null) {
            String strDateBegin = mPlan.getPlan_begindate();
            String strDateEnd = mPlan.getPlan_enddate();
            isOutDate = AppUtil.isOutOfDate(strDateBegin, strDateEnd);
        }

        isOver = planPoint.getState() == OfflinePathBean.POINT_STATE_FINISHED;
        if (isOver || isOutDate ){
            return false;
        }

        return true;
    }

}
