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

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.lcp.arecyclerview.widget.ARecyclerBaseAdapter;
import com.mgq.base.utils.string.StringUtil;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.module.dasData.domain.DasData;

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 DasDataAdapter extends ARecyclerBaseAdapter<DasDataAdapter.ViewHolder, DasData> {
    private static final int MAX_SIZE = 100;
    private Context context;
    private Set<Integer> selectedPostions = new HashSet<>();

    private OnItemClickListener itemClickListener;

    private boolean mChoiceMode;

    public DasDataAdapter(List<DasData> dataList, Context context, boolean selectable) {
        super(context, dataList);
        this.context = context;
        this.mChoiceMode = selectable;
        allSelected();
    }

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

    public List<DasData> getSelectedItems() {
        List<DasData> selectedItems = new ArrayList<>();
        for (int position : selectedPostions) {
            selectedItems.add(getItem(position));
        }
        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(DasData item, int position);
    }

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

    @Override
    public void onBindViewHolder(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_name)
        EditText textName;
        @BindView(R.id.text_lng)
        TextView textLng;
        @BindView(R.id.text_lat)
        TextView textLat;
        @BindView(R.id.text_alt)
        TextView textAlt;
        @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.textName.setTextColor(colorText);
            this.textLng.setTextColor(colorText);
            this.textLat.setTextColor(colorText);
            this.textAlt.setTextColor(colorText);

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

                textName.setText(dasData.getName());
                this.textLng.setText(StringUtil.formatDigit6(dasData.getLng()));
                this.textLat.setText(StringUtil.formatDigit6(dasData.getLat()));
                this.textAlt.setText(StringUtil.formatDigit6(dasData.getAlt()));

                textName.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                    }

                    @Override
                    public void afterTextChanged(Editable editable) {
                        String strInput = editable.toString();
                        if (!strInput.isEmpty()) {
                            dasData.setName(strInput);
                        }
                    }
                });
            }

            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);
            }
        }
    }
}
