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


import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
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.rushrepair.domain.RushRepairBean;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author YCHK
 */
public class RepairAdapter extends ARecyclerBaseAdapter<RepairAdapter.MyViewHolder, RushRepairBean> {

    private Context mContext;

    private List<RushRepairBean> mData;

    private RepairAdapter.OnTextReportEnrollClickListener mEnrollClickListener;

    private RepairAdapter.OnTextHandleClickListener mHandleClickListener;

    private Set<Integer> selectedPostions = new HashSet<>();

    private int mselect;

    public void toggleSelection(int position) {
        if (selectedPostions.contains(position)) {
            selectedPostions.remove(position);
        } else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

    public void setSelectionEnable(int position, boolean enable) {
        if (enable) {
            if (!selectedPostions.contains(position)) {
                selectedPostions.add(position);
            }
        } else {
            if (selectedPostions.contains(position)) {
                selectedPostions.remove(position);
            }
        }
        notifyItemChanged(position);
    }

    public void cancelAllSelection() {
        selectedPostions.clear();
        this.notifyDataSetChanged();
    }

    public RepairAdapter(Context context, List<RushRepairBean> data, int mselect) {
        super(context, data);
        mContext = context;
        mData = data;
        this.mselect = mselect;
    }

    public void setDatas(int mselect) {
        this.mselect = mselect;
        notifyDataSetChanged();
    }

    public void setData(List<RushRepairBean> data) {
        mData = data;
        selectedPostions.clear();
        notifyDataSetChanged();
    }

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

    public void setEnrollClickListener(RepairAdapter.OnTextReportEnrollClickListener clickListener) {
        mEnrollClickListener = clickListener;
    }

    public void setHandleClickListener(RepairAdapter.OnTextHandleClickListener clickListener) {
        mHandleClickListener = clickListener;
    }

    public interface OnTextReportEnrollClickListener {
        void onReportEnrollClick(View view, int position);
    }

    public interface OnTextHandleClickListener {
        void onTextClick(View view, int position, List<RushRepairBean> dataList);
    }

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

    @Override
    public void onBindViewHolder(final RepairAdapter.MyViewHolder holder, int position) {
        String[] handleStates = mContext.getResources().getStringArray(R.array.build_hanlde_state);
        String[] handlePeriods = mContext.getResources().getStringArray(R.array.build_handle_period);
        RushRepairBean buildManage = getItem(position);
        if (buildManage != null && !TextUtils.isEmpty(buildManage.getWorkOrderState())) {
            String build_name = buildManage.getWorkOrderId();
            holder.textBuildName.setText("工单编号：" + build_name);
            holder.textProjectType.setText(buildManage.getWorkOrderTypeName());
            holder.textBuildAddr.setText(buildManage.getAddress());
            if (buildManage.getWorkOrderState().equals("2") || buildManage.getWorkOrderState().equals("4")) {
                if (mselect == 6) {
                    holder.tvEnd.setVisibility(View.GONE);
                } else {
                    holder.tvEnd.setVisibility(View.VISIBLE);
                }
            } else {
                holder.tvEnd.setVisibility(View.GONE);
            }

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

            if (mEnrollClickListener != null) {
                holder.textBuildReportEnroll.setOnClickListener(v -> mEnrollClickListener.onReportEnrollClick(holder.itemView, holder.getLayoutPosition()));
            }

            if (mHandleClickListener != null) {
                holder.tvEnd.setOnClickListener(v -> mHandleClickListener.onTextClick(holder.itemView, holder.getLayoutPosition(), mData));
            }
        }
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image_build_icon)
        ImageView imageBuildIcon;
        @BindView(R.id.text_build_name)
        TextView textBuildName;
        @BindView(R.id.text_process_state)
        TextView textProcessState;
        @BindView(R.id.text_build_time)
        TextView textBuildTime;
        @BindView(R.id.text_project_type)
        TextView textProjectType;
        @BindView(R.id.text_build_state)
        TextView textBuildState;
        @BindView(R.id.text_log_time)
        TextView textLogTime;
        @BindView(R.id.text_build_addr)
        TextView textBuildAddr;
        @BindView(R.id.text_build_report_enroll)
        TextView textBuildReportEnroll;
        @BindView(R.id.text_build_handle)
        TextView textBuildHandle;
        @BindView(R.id.tv_end)
        TextView tvEnd;

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