package com.sipai.jspatrol.Patrol;

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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.sipai.jspatrol.Bean.InputPoint;
import com.sipai.jspatrol.R;

import java.util.List;

/**
 * Created by lvliming on 2019/12/3.
 */

public class EditMPointAdapter extends RecyclerView.Adapter<EditMPointAdapter.ViewHolder> implements View.OnClickListener{
    //数据源
    private List<InputPoint> list;
    private Context context;
    //接口实例
    private EditMPointAdapter.RecyclerViewOnItemClickListener onItemClickListener;

    public EditMPointAdapter(List<InputPoint> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_edit_m_point, parent, false);
        ViewHolder vh = new ViewHolder(root);
        //为Item设置点击事件
        root.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        InputPoint point = list.get(position);
        holder.rl_sp.setVisibility(View.VISIBLE);
        holder.ll_value.setVisibility(View.GONE);
        holder.tv_name.setText(point.getParmname());
        holder.tv_unit.setText(point.getUnit());
        if (holder.edt_value.getTag()!=null&&holder.edt_value.getTag() instanceof TextWatcher){
            holder.edt_value.removeTextChangedListener((TextWatcher) holder.edt_value.getText());
        }
        holder.edt_value.setText(list.get(position).getParmvalue());
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s!=null&&s.length()>0) {
                    list.get(position).setParmvalue(s.toString());
                }else {
                    list.get(position).setParmvalue("");
                }
            }
        };
        holder.edt_value.addTextChangedListener(textWatcher);
        holder.edt_value.setTag(textWatcher);
        String[] types = new String[]{};
        if (point.getValuemeaning().equals("NOTHING3")){
            types = context.getResources().getStringArray(R.array.VM_NOTHING3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("HAVE2")){
            types = context.getResources().getStringArray(R.array.VM_HAVE2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("HIGH2")){
            types = context.getResources().getStringArray(R.array.VM_HIGH2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("START3")){
            types = context.getResources().getStringArray(R.array.VM_START3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("YELLOW3")){
            types = context.getResources().getStringArray(R.array.VM_YELLOW3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("HIGH3")){
            types = context.getResources().getStringArray(R.array.VM_HIGH3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("NORMAL2")){
            types = context.getResources().getStringArray(R.array.VM_NORMAL2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("FULL2")){
            types = context.getResources().getStringArray(R.array.VM_FULL2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("FULL3")){
            types = context.getResources().getStringArray(R.array.VM_FULL3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("MORE3")){
            types = context.getResources().getStringArray(R.array.VM_MORE3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("OPEN2")){
            types = context.getResources().getStringArray(R.array.VM_OPEN2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("DAMAGE2")){
            types = context.getResources().getStringArray(R.array.VM_DAMAGE2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("GOOD2")){
            types = context.getResources().getStringArray(R.array.VM_GOOD2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("YES2")){
            types = context.getResources().getStringArray(R.array.VM_YES2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("VOL2")){
            types = context.getResources().getStringArray(R.array.VM_VOL2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("VOL3")){
            types = context.getResources().getStringArray(R.array.VM_VOL3);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("FLOW2")){
            types = context.getResources().getStringArray(R.array.VM_FLOW2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else if (point.getValuemeaning().equals("DRY2")){
            types = context.getResources().getStringArray(R.array.VM_DRY2);
            list.get(position).setSel(Integer.parseInt(list.get(position).getParmvalue()));
        }else {
            holder.rl_sp.setVisibility(View.GONE);
            holder.ll_value.setVisibility(View.VISIBLE);
        }
        ArrayAdapter typeAdapter = new ArrayAdapter(context, R.layout.item_normal_little_spin, R.id.moudle_normal_little_spin_tv_title, types);
        holder.sp_value.setAdapter(typeAdapter);
        if (holder.sp_value.getTag()!=null){
            holder.sp_value.setSelection(list.get(position).getSel());
        }else {
            holder.sp_value.setTag(list.get(position).getSel());
            holder.sp_value.setSelection(list.get(position).getSel());
        }
//        final String[] finalTypes = types;
        holder.sp_value.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {
                list.get(position).setParmvalue(String.valueOf(i));
                list.get(position).setSel(i);
                holder.sp_value.setTag(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //设置Tag
        holder.root.setTag(position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            onItemClickListener.onItemClickListener(v, (Integer) v.getTag());
        }
    }

    //视图管理
    public class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_name,tv_unit;
        private Spinner sp_value;
        private EditText edt_value;
        private RelativeLayout rl_sp;
        private LinearLayout ll_value;
        private View root;
        public ViewHolder(View itemView) {
            super(itemView);
            this.root = itemView;
            tv_name = root.findViewById(R.id.moudle_item_edit_m_point_tv_name);
            tv_unit = root.findViewById(R.id.moudle_item_edit_m_point_tv_unit);
            sp_value = root.findViewById(R.id.moudle_item_edit_m_point_sp_value);
            edt_value = root.findViewById(R.id.moudle_item_edit_m_point_et_value);
            rl_sp = root.findViewById(R.id.moudle_item_edit_m_point_rl_sp);
            ll_value = root.findViewById(R.id.moudle_item_edit_m_point_ll_value);
        }
    }
    //设置点击事件
    public void setRecyclerViewOnItemClickListener(RecyclerViewOnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    //接口回调设置点击事件
    public interface RecyclerViewOnItemClickListener {
        //点击事件
        void onItemClickListener(View view, int position);

    }
}
