package com.tofly.ghrq_route.module.emergencyHandle.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.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.emergencyHandle.EmergencyTool;
import com.tofly.ghrq_route.module.emergencyHandle.domain.EmergencyHandleData;
import com.tofly.ghrq_route.module.notifyManage.NotifyManageBean;
import com.tofly.ghrq_route.common.util.dialog.DialogUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class EmergencyAdapter extends ARecyclerBaseAdapter<EmergencyAdapter.MyViewHolder, EmergencyHandleData> {
    private int select_state;
    private EmergencyTool emergencyTool;
    private OnItemClickListener onOverSignClickListener;

    public void setOnOverSignClickListener(OnItemClickListener onOverSignClickListener) {
        this.onOverSignClickListener = onOverSignClickListener;
    }

    public EmergencyAdapter(Context context, List<EmergencyHandleData> data) {
        super(context, data);
        emergencyTool = EmergencyTool.getInstance();
        select_state = 0;
    }

    public EmergencyAdapter(Context context, List<EmergencyHandleData> data, int select_state) {
        super(context, data);
        emergencyTool = EmergencyTool.getInstance();
        this.select_state = select_state;
    }

    public void setData(List<EmergencyHandleData> data, int select_state) {
        this.select_state = select_state;
        super.setData(data);
    }

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

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
        EmergencyHandleData emergencyData = getItem(position);
        if (emergencyData != null) {
            holder.textJobTitle.setText(emergencyData.getTypeMainName());
            holder.textJobContent.setText(emergencyData.getAddress());
            String create_time = emergencyData.getCreateTime();
            if (create_time != null) {
                holder.textCreateTime.setText(create_time);
            }

            holder.imageNewJob.setVisibility(View.GONE);
            NotifyManageBean msgBean = emergencyTool.getNotifyMsgBean(emergencyData.getId() + "");
            if (msgBean != null) {
                boolean isRead = msgBean.getIsRead();
                holder.imageNewJob.setVisibility(isRead? View.GONE : View.VISIBLE);
            }

            holder.textOverSign.setVisibility(View.GONE);
            holder.textOverTime.setVisibility(View.GONE);
            holder.layoutJobStatus.setVisibility(View.GONE);
            holder.imageJobState.setImageResource(R.mipmap.image_handled);
            holder.textJobState.setText("已到位");
            holder.textJobState.setTextColor(mContext.getColor(R.color.text_handled));
            if (select_state == 0) {
                boolean isOver = emergencyTool.isEmergencyOver(emergencyData);
                if (isOver) {
/*                    holder.textOverSign.setVisibility(View.GONE);
                    holder.textOverTime.setVisibility(View.VISIBLE);
                    holder.textOverTime.setText(emergencyData.getSignTime());*/
                    holder.layoutJobStatus.setVisibility(View.VISIBLE);
                }else {
/*                    holder.textOverSign.setVisibility(View.VISIBLE);
                    holder.textOverTime.setVisibility(View.GONE);*/
                    holder.layoutJobStatus.setVisibility(View.GONE);
                }
            }
            boolean bRejected = emergencyData.getRejectedFlag() == 1;
            if (bRejected) {
                holder.layoutJobStatus.setVisibility(View.VISIBLE);
                holder.imageJobState.setImageResource(R.mipmap.image_unhandled);
                holder.textJobState.setText("被驳回");
                holder.textJobState.setTextColor(mContext.getColor(R.color.text_unHandled));
            }

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

            holder.textOverSign.setOnClickListener(v -> DialogUtil.showAlertWithConfirmCancelButton(mContext,
                    "到位签到", "是否已到达应急调度所处位置周边?", true, (dialogInterface, i) -> {
                /*emergencyTool.setEmergencyOver(emergencyData.getId());
                notifyItemChanged( position);*/
                    if (onOverSignClickListener != null) {
                        onOverSignClickListener.onItemClick(holder.itemView, holder.getLayoutPosition());
                    }
            }));
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.text_job_title)
        TextView textJobTitle;
        @BindView(R.id.text_over_sign)
        TextView textOverSign;
        @BindView(R.id.text_over_time)
        TextView textOverTime;
        @BindView(R.id.layout_job_status)
        LinearLayout layoutJobStatus;
        @BindView(R.id.image_job_state)
        ImageView imageJobState;
        @BindView(R.id.text_job_state)
        TextView textJobState;
        @BindView(R.id.text_create_time)
        TextView textCreateTime;
        @BindView(R.id.text_job_content)
        TextView textJobContent;
        @BindView(R.id.image_new_job)
        ImageView imageNewJob;

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