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.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.buildManage.domain.BuildManageBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;

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

public class BuildManageAdapter extends ARecyclerBaseAdapter<BuildManageAdapter.MyViewHolder, BuildManageBean> {

    private Context mContext;

    private List<BuildManageBean> mData;

    private OnTextReportEnrollClickListener mEnrollClickListener;

    private OnTextHandleClickListener mHandleClickListener;

    private int mselect;

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

    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 BuildManageAdapter(Context context, List<BuildManageBean> 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<BuildManageBean> data) {
        mData = data;
        selectedPostions.clear();
        notifyDataSetChanged();
    }

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

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

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

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

    public interface OnTextHandleClickListener {
        void onTextClick(View view, int position, List<BuildManageBean> 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 MyViewHolder holder, int position) {
        String[] handleStates = mContext.getResources().getStringArray(R.array.build_hanlde_state);
        String[] handlePeriods = mContext.getResources().getStringArray(R.array.build_handle_period);
        BuildManageBean buildManage = getItem(position);
        if (buildManage != null) {
            String build_name = buildManage.getSiteName();
//            int build_type = buildManage.getBuild_type();
//            if (build_type >= 1 && build_type <= 4){
//                String build_period = handlePeriods[build_type-1];
//                build_name = String.format("%s (%s)", buildManage.getName(), build_period);
//            }
            holder.textBuildName.setText(build_name);
            holder.textProjectType.setText(buildManage.getSiteTypeName());
            holder.textBuildAddr.setText(buildManage.getConstructionAddress());
//            String strBeginDate = buildManage.getBuild_createtime1();
//            String strEndDate = buildManage.getBuild_createtime2();
//            if (build_type != BuildManageBean.BUILD_PERIOD_CUSTOM){
//                if (strBeginDate != null){
//                    strBeginDate = DateUtils.formatTimeYMD(strBeginDate);
//                }
//                if (strEndDate != null){
//                    strEndDate = DateUtils.formatTimeYMD(strEndDate);
//                }
//                if (strBeginDate != null && strEndDate != null) {
//                    holder.textBuildTime.setVisibility(View.VISIBLE);
//                    holder.textBuildTime.setText(strBeginDate + " 至 " + strEndDate);
//                }
//            }else {
//                if (strBeginDate != null && strEndDate != null) {
//                    holder.textBuildTime.setVisibility(View.VISIBLE);
//                    holder.textBuildTime.setText(strBeginDate + " - " + strEndDate);
//                }
//            }

//            holder.textBuildHandle.setEnabled(false);
//            holder.textLogTime.setVisibility(View.GONE);
//            if (handle_state == 0){
//                boolean isEnabled = selectedPostions.contains(position);
//                holder.textBuildHandle.setEnabled(isEnabled);
//                holder.textBuildHandle.setText(R.string.build_handle_start);
//            }else if (handle_state == 1){
//                holder.textBuildHandle.setEnabled(true);
//                holder.textBuildHandle.setText(R.string.build_handle_stop);
//                holder.textLogTime.setVisibility(View.VISIBLE);
//                String time = buildManage.getLog_time();
//                if (time == null || time.equals("null"))
//                    time = "";
//                String log_time = "(" + time + ")";
//                holder.textLogTime.setText(log_time);
//            }else if (handle_state == 2){
//                holder.textBuildHandle.setEnabled(false);
//                holder.textBuildHandle.setText(R.string.build_handle_finish);
//            }
            if (buildManage.getConstructionState().equals("2") || buildManage.getConstructionState().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);
        }
    }
}
