package com.sunzee.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.sunzee.R;
import com.sunzee.utils.PreventSpeedClickUtil;
import com.sunzee.utils.ToastUtil;
import com.wuxiaolong.androidutils.library.LogUtil;

import java.util.HashMap;

/**
 * @author whw
 * @time 2019/3/11
 * @Description 通用参数recyclerview 设配器
 */
public class DebugParameterAdapter extends RecyclerView.Adapter<DebugParameterAdapter.ViewHolder> {
    private int[] paraName = {
            R.string.debug_1, R.string.debug_2,
            R.string.debug_3, R.string.debug_4,
            R.string.debug_5, R.string.debug_6,
            R.string.debug_7, R.string.hole_lamp1,
            R.string.hole_lamp2, R.string.hole_lamp3,
            R.string.hole_lamp4, R.string.hole_lamp5,
            R.string.hole_lamp6,R.string.x_add,
            R.string.x_subtract,R.string.y_add,
            R.string.y_subtract,R.string.z_top,
            R.string.z_bottom,R.string.e_add,
            R.string.e_subtract,R.string.a_top,
            R.string.a_bottom,R.string.x_back_zero,
            R.string.y_back_zero,R.string.z_back_zero,
            R.string.e_back_zero,R.string.a_back_zero,
            R.string.jerk

    };
    private final String[] paraAddress = {
            "M1411", "M1412", "M313", "M315",
            "M316", "M317", "M318", "M320",
            "M321", "M322", "M323", "M324",
            "M325","M1002","M1003","M1102",
            "M1103","M1203","M1202","M1302",
            "M1303","M1403","M1402","M1010",
            "M1110","M1210","M1310","M1410",
            "M10"};

    public DebugParameterAdapter() {

    }

    public void setItemListener(ItemParaClickListener listener) {
        this.listener = listener;
    }

    private ItemParaClickListener listener;


    //地址具体数值

    private static final String TAG = "DebugParameterAdapter";

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_debugparameter, viewGroup, false);
        return new ViewHolder(inflate);

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.cbManual.setText(paraName[i]);
        viewHolder.cbManual.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("ResourceAsColor")
            @Override
            public void onClick(View view) {
                //防止快速点击
                if (!PreventSpeedClickUtil.isFastClick()) {
                    ToastUtil.showToast("点击过快！！！");
                    CheckBox checkBox = (CheckBox) view;
                    checkBox.setChecked(!checkBox.isChecked());
                    return;
                }
                Log.d(TAG, "onClick: "+i);

                listener.onItemListener(view, i, paraAddress[i]);
            }
        });
    }

    @Override
    public int getItemCount() {
        return paraName.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public CheckBox cbManual;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cbManual = itemView.findViewById(R.id.cb_manual);

        }
    }


    public class MyTextChangedListener implements TextWatcher {

        public ViewHolder holder;
        public HashMap<Integer, String> contents;
        public String TAG = "测试";

        public MyTextChangedListener(ViewHolder holder, HashMap<Integer, String> contents) {
            this.holder = holder;
            this.contents = contents;
        }

        @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) {
            if (holder != null && contents != null) {
                int adapterPosition = holder.getAdapterPosition();
                LogUtil.d(TAG, "adapterPosition===================" + adapterPosition);
                contents.put(adapterPosition, editable.toString());
            }
        }
    }

    /**
     * @Description 通用参数界面点击事件监听
     */
    public interface ItemParaClickListener {
        /**
         * @param view     控件
         * @param position 位置
         * @param text
         */
        void onItemListener(View view, int position, String text);
    }
}
