package com.example.administrator.zhapp.adapter;

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.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.zhapp.R;
import com.example.administrator.zhapp.entity.TmdyInfo;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/8/icon1.
 */

/**
 * 物料标签列表适配器
 */
public class TmdyAdapter extends BaseAdapter {

    private List<TmdyInfo.Tmdy> list;
    private Context context;
    private int index;

    private TmdyAdapter.CallBack mCallBack;


    public TmdyAdapter(List<TmdyInfo.Tmdy> list, Context context, TmdyAdapter.CallBack mCallBack) {
        this.list = list;
        this.context = context;
        this.mCallBack = mCallBack;

    }

    public interface CallBack {
        void Click(View view);
    }

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

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

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.tmdy_item, viewGroup, false);
            holder.et_dysl = (EditText) view.findViewById(R.id.et_dysl);
            holder.tv_kcxm = (TextView) view.findViewById(R.id.tv_kcxm);
            holder.tv_ms = (TextView) view.findViewById(R.id.tv_ms);
            holder.cb_isprint = (CheckBox) view.findViewById(R.id.cb_isprint);
            holder.tv_sl = (TextView) view.findViewById(R.id.tv_sl);
            holder.jssj = (TextView) view.findViewById(R.id.tv_jssj);
            holder.ponum = (TextView) view.findViewById(R.id.tv_hth);
            holder.watcher = new MyTextWatcher();
            holder.et_dysl.addTextChangedListener(holder.watcher);
            holder.watcher.updatePosition(i);
            view.setTag(holder);
            AutoUtils.autoSize(view);
        } else {
            holder = (ViewHolder) view.getTag();
            holder.watcher.updatePosition(i);
        }
        holder.tv_ms.setText(list.get(i).getDescription());
        holder.et_dysl.setText(list.get(i).getCount());
        holder.tv_kcxm.setText(list.get(i).getItemnum());
        holder.cb_isprint.setChecked(list.get(i).isCheck());
        holder.tv_sl.setText(list.get(i).getOrderqty() + "");
        holder.jssj.setText(list.get(i).getJssj());
        holder.ponum.setText(list.get(i).getPonum());
        holder.et_dysl.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                    index = i;
                }
                return false;
            }
        });

//        holder.cb_isprint.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//                list.get(i).setCheck(b);
//            }
//        });

        holder.cb_isprint.setTag(i);

        holder.cb_isprint.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCallBack.Click(view);
            }
        });

        holder.et_dysl.clearFocus();
        if (index != -1 && index == i) {
            holder.et_dysl.requestFocus();
        }
        return view;
    }

    static class ViewHolder {
        TextView tv_ms;
        TextView tv_kcxm;
        EditText et_dysl;
        CheckBox cb_isprint;
        TextView tv_sl;
        TextView jssj;
        TextView ponum;
        MyTextWatcher watcher;
    }


    class MyTextWatcher implements TextWatcher {
        //由于TextWatcher的afterTextChanged中拿不到对应的position值，所以自己创建一个子类
        private int mPosition;

        public void updatePosition(int position) {
            mPosition = position;
        }

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

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            list.get(mPosition).setCount(s.toString());
        }
    }

}


