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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sunzee.R;
import com.sunzee.model.domain.ParameterBean;
import com.sunzee.utils.ToastUtil;
import com.wuxiaolong.androidutils.library.LogUtil;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 进阶参数 AdvanceParameterAdapter
 */
public class AdvanceParameterAdapter extends RecyclerView.Adapter<AdvanceParameterAdapter.ViewHolder> {

    private final int[] paraName;
    private ArrayList<ParameterBean> parameterBeans = new ArrayList<>();

    public ArrayList<ParameterBean> getParameterBeans() {
        return parameterBeans;
    }

    public void setParameterBeans(ArrayList<ParameterBean> parameterBeans) {
        this.parameterBeans = parameterBeans;
    }

    private final String[] paraAddress = {
            "D426", "D427", "D428", "D429",
            "D430", "D431"};
    //保存editText的值
    public HashMap<Integer, String> contents = new HashMap<>();

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

    }

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

    private AdvanceParameterAdapter.ItemParaClickListener listener;

    public void setValues(String[] values) {
        this.values = values;
        parameterBeans.clear();
        for (int i = 0; i < values.length; i++) {
            parameterBeans.add(new ParameterBean(paraAddress[i], values[i]));
        }
    }

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


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

    }

    private static final String TAG = "AdvanceParameterAdapter";

    @Override
    public void onBindViewHolder(@NonNull AdvanceParameterAdapter.ViewHolder viewHolder, final int i) {
        viewHolder.tvName.setText(paraName[i]);
        viewHolder.etPara.addTextChangedListener(new AdvanceParameterAdapter.MyTextChangedListener(viewHolder, contents));
        contents.put(i, viewHolder.etPara.getText().toString().trim());
        viewHolder.btnUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = contents.get(i);
                if (text.equals("")) {
                    ToastUtil.showToast("不能输入空值哦！！！");
                    return;
                }
                listener.onItemListener(v, i, text);
                parameterBeans.set(i, new ParameterBean(paraAddress[i], text));
            }
        });
        if (values != null && values.length > 0) {
            viewHolder.etPara.setText(values[i]);
        }
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView tvName;
        public EditText etPara;
        public Button btnUpdate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_para_name);
            etPara = itemView.findViewById(R.id.et_para);
            btnUpdate = itemView.findViewById(R.id.btn_update_para);
        }
    }


    public class MyTextChangedListener implements TextWatcher {

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

        public MyTextChangedListener(AdvanceParameterAdapter.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);
    }
}
