package com.tofly.ghrq_route.module.trouble.ui.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.trouble.domain.AttributesBean;
import com.tofly.ghrq_route.module.trouble.domain.PipeAttributesBean;

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

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Amu on 2017/1/10.
 */

public class TroubleAnalyzePiepAdapter extends ARecyclerBaseAdapter<TroubleAnalyzePiepAdapter.ViewHolder, PipeAttributesBean> {
    private static final int MAX_SIZE = 100;
    private Context context;
    private Set<Integer> selectedPostions = new HashSet<>();

    private OnItemClickListener itemClickListener;

    private boolean mChoiceMode;

    public TroubleAnalyzePiepAdapter(List<PipeAttributesBean> featureList, Context context, boolean selectable) {
        super(context, featureList);
        this.context = context;
        this.mChoiceMode = selectable;
        allSelected();
    }

    public void setData(List<PipeAttributesBean> featureList) {
        mList = featureList;
        allSelected();
    }
    public void toggleSelection(int position) {
        if (selectedPostions.contains(position)) {
            selectedPostions.remove(position);
        } else {
            selectedPostions.add(position);
        }
        notifyItemChanged(position);
    }

    public List<AttributesBean> getSelectedItems() {
        List<AttributesBean> selectedItems = new ArrayList<>();
        for (int postion : selectedPostions) {
            selectedItems.add(getItem(postion));
        }
        return selectedItems;
    }

    public void allSelected() {
        selectedPostions.clear();
        for (int i = 0; i < mList.size(); i++) {
            selectedPostions.add(i);
        }
        this.notifyDataSetChanged();
    }

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

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.itemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(PipeAttributesBean item, int position);
    }

    @Override
    public TroubleAnalyzePiepAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.listview_trouble_analyze_item, parent,false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(TroubleAnalyzePiepAdapter.ViewHolder holder, int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.setData(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        int size = mList.size();
        if (size > MAX_SIZE) {
            ToastUtil.showToast(context,"只显示前"+ MAX_SIZE +"条数据");
        }
        return size > MAX_SIZE ? MAX_SIZE :size;
    }

    protected class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.text_device_type)
        TextView textType;
        @BindView(R.id.text_device_sid)
        TextView textSid;
        @BindView(R.id.text_device_material)
        TextView textMaterial;
        @BindView(R.id.text_device_radius)
        TextView textRadius;
        @BindView(R.id.check_device_select)
        CheckedTextView checkSelected;

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

        public void setData(final int position) {
            if (position % 2 == 1) {
                this.itemView.setBackgroundResource(R.color.scada_shadow);
            } else {
                this.itemView.setBackgroundResource(R.color.white);
            }
            int colorText = context.getResources().getColor(R.color.text);
            this.textType.setTextColor(colorText);
            this.textSid.setTextColor(colorText);
            this.textMaterial.setTextColor(colorText);
            this.textRadius.setTextColor(colorText);

            final AttributesBean feature = mList.get(position);
            if (feature != null) {
                if (selectedPostions.contains(position)){
                    this.checkSelected.setChecked(true);
                }else {
                    this.checkSelected.setChecked(false);
                }

                textType.setText(feature.getLayerName());
                String sid = feature.getSid();
                if (sid == null || sid.isEmpty())
                    sid = "null";
                textSid.setText(sid);
                this.textMaterial.setText(feature.getMaterial());
                this.textRadius.setText(feature.getPipeDiameter());
            }

            if (itemClickListener != null) {
                this.itemView.setOnClickListener(v ->
                        itemClickListener.onItemClick(getItem(this.getLayoutPosition()), this.getLayoutPosition()));
            }

            if (mChoiceMode) {
                this.checkSelected.setVisibility(View.VISIBLE);
                this.checkSelected.setOnClickListener(view -> toggleSelection(position));
            }else {
                this.checkSelected.setVisibility(View.INVISIBLE);
            }
        }
    }
}
