package com.sunzee.adapter;

import android.content.Context;
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.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.sunzee.R;
import com.wuxiaolong.androidutils.library.LogUtil;

import java.util.HashMap;

/**
 * @author whw
 * @time 2019/3/11
 * @Description 通用参数recyclerview 设配器
 */
public class GeneralParameterAdapter extends RecyclerView.Adapter<GeneralParameterAdapter.ViewHolder> {
    private int[] paraName;

    //保存editText的值
    public HashMap<Integer, String> contents = new HashMap<>();

    public GeneralParameterAdapter(int[] paraName) {
        this.paraName = paraName;
    }

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

    private ItemParaClickListener listener;

    public void setValues(String[] values) {
        this.values = values;
    }

    //地址具体数值
    private String[] values;

    private static final String TAG = "GeneralParameterAdapter";
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_generalparameter, 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, "");
            }
        });
    }

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