package com.sunzee.adapter;

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

import com.sunzee.R;
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
    };
    private final String[] paraAddress = {
            "M311", "M312", "M313", "M315",
            "M316", "M317", "M318"};

    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() {
            @Override
            public void onClick(View view) {
                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);
    }
}
