package com.qhclh.ytzh.work.productiondailyw;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import com.qhclh.ytzh.R;
import com.qhclh.ytzh.bean.DailyDetailsBean;

import java.util.List;

/**
 * Created by 青花瓷 on 2017/12/7.
 */

public class DailyDetailsAdapter extends BaseAdapter {
    private Context context;
    private List<DailyDetailsBean> list;
    private LayoutInflater layoutInflater;

    public DailyDetailsAdapter(Context context, List<DailyDetailsBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return Long.parseLong(list.get(i).getHouse_ID());
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        DailyDetailsBean message = list.get(i);
        if (view == null) {
            view = layoutInflater.inflate(R.layout.item_dailydetail, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.dailyd_title = view.findViewById(R.id.dailyd_title);
            viewHolder.dailyd_buildinghome = view.findViewById(R.id.dailyd_buildinghome);
            viewHolder.dailyd_dayage = view.findViewById(R.id.dailyd_dayage);
            viewHolder.dailyd_death = view.findViewById(R.id.dailyd_death);
            viewHolder.dailyd_eliminate = view.findViewById(R.id.dailyd_eliminate);
            viewHolder.dailyd_water = view.findViewById(R.id.dailyd_water);
            viewHolder.dailyd_weight = view.findViewById(R.id.dailyd_weight);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        final ViewHolder finalViewHolder = viewHolder;

        viewHolder.dailyd_dayage.setTag(i);
        // 设置触摸事件
        viewHolder.dailyd_dayage.setOnTouchListener(new OnEditTextTouched(i));
        viewHolder.dailyd_dayage.clearFocus();
        if (i == mCurrentTouchedIndex) {
            // 如果该项中的EditText是要获取焦点的
            viewHolder.dailyd_dayage.requestFocus();
        }
        viewHolder.dailyd_dayage.addTextChangedListener(new TextWatcher() {
            @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) {
                int temPosition = (int) finalViewHolder.dailyd_dayage.getTag();
                try {
                    list.get(temPosition).setDays(editable.toString().trim());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });

        viewHolder.dailyd_death.setTag(i);
        // 设置触摸事件
        viewHolder.dailyd_death.setOnTouchListener(new OnEditTextTouched(i));
        viewHolder.dailyd_death.clearFocus();
        if (i == mCurrentTouchedIndex) {
            // 如果该项中的EditText是要获取焦点的
            viewHolder.dailyd_death.requestFocus();
        }
        viewHolder.dailyd_death.addTextChangedListener(new TextWatcher() {
            @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) {
                int temPosition = (int) finalViewHolder.dailyd_death.getTag();
                try {
                    list.get(temPosition).setDieNumber(editable.toString().trim());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
        viewHolder.dailyd_eliminate.setTag(i);
        // 设置触摸事件
        viewHolder.dailyd_eliminate.setOnTouchListener(new OnEditTextTouched(i));
        viewHolder.dailyd_eliminate.clearFocus();
        if (i == mCurrentTouchedIndex) {
            // 如果该项中的EditText是要获取焦点的
            viewHolder.dailyd_eliminate.requestFocus();
        }
        viewHolder.dailyd_eliminate.addTextChangedListener(new TextWatcher() {
            @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) {
                int temPosition = (int) finalViewHolder.dailyd_eliminate.getTag();
                try {
                    list.get(temPosition).setObsolete(editable.toString().trim());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
        viewHolder.dailyd_water.setTag(i);
        // 设置触摸事件
        viewHolder.dailyd_water.setOnTouchListener(new OnEditTextTouched(i));
        viewHolder.dailyd_water.clearFocus();
        if (i == mCurrentTouchedIndex) {
            // 如果该项中的EditText是要获取焦点的
            viewHolder.dailyd_water.requestFocus();
        }
        viewHolder.dailyd_water.addTextChangedListener(new TextWatcher() {
            @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) {
                int temPosition = (int) finalViewHolder.dailyd_water.getTag();
                try {
                    list.get(temPosition).setWaterIntake(editable.toString().trim());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
        viewHolder.dailyd_weight.setTag(i);
        // 设置触摸事件
        viewHolder.dailyd_weight.setOnTouchListener(new OnEditTextTouched(i));
        viewHolder.dailyd_weight.clearFocus();
        if (i == mCurrentTouchedIndex) {
            // 如果该项中的EditText是要获取焦点的
            viewHolder.dailyd_weight.requestFocus();
        }
        viewHolder.dailyd_weight.addTextChangedListener(new TextWatcher() {
            @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) {
                int temPosition = (int) finalViewHolder.dailyd_weight.getTag();
                try {
                    list.get(temPosition).setWeight(editable.toString().trim());
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });

        viewHolder.dailyd_title.setText(message.getMingxi() + "");
        viewHolder.dailyd_buildinghome.setText(message.getHouse_Name() + "");
        viewHolder.dailyd_dayage.setText(message.getDays() + "");
        viewHolder.dailyd_death.setText(message.getDieNumber() + "");
        viewHolder.dailyd_eliminate.setText(message.getObsolete() + "");
        viewHolder.dailyd_water.setText(message.getWaterIntake() + "");
        viewHolder.dailyd_weight.setText(message.getWeight() + "");

        return view;
    }

    private class ViewHolder {
        private TextView dailyd_title;
        private TextView dailyd_buildinghome;
        private EditText dailyd_dayage;
        private EditText dailyd_death;
        private EditText dailyd_eliminate;
        private EditText dailyd_water;
        private EditText dailyd_weight;
    }

    // 定义变量，记录刷新前获得焦点的EditText所在的位置
    private int mCurrentTouchedIndex = -1;

    // ListView中EditText的触摸事件 以防虚拟键盘弹出取消 getview 重新执行后输入框失去焦点
    private class OnEditTextTouched implements View.OnTouchListener {
        private int position;

        public OnEditTextTouched(int position) {
            this.position = position;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mCurrentTouchedIndex = position;
            }
            return false;
        }
    }
}
