package com.taikor.investment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.taikor.investment.R;
import com.taikor.investment.bean.CostProduct;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 必选产品
 * Created by Any on 2017/4/25.
 */
public class MustProductAdapter extends SwipeMenuAdapter<MustProductAdapter.MustViewHolder> {

    private MustListener listener;
    private List<CostProduct> mList;

    public MustProductAdapter(Context context) {
        listener = (MustListener) context;
    }

    public void setData(List<CostProduct> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_guaranteed, parent, false);
    }

    @Override
    public MustProductAdapter.MustViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new MustViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(final MustProductAdapter.MustViewHolder holder, final int position) {
        holder.productId.setText(position + 1 + "");
        holder.productMoney.setText("");
        holder.productName.setText("");
        holder.productTime.setText("——");
        holder.productIncome.setText("——");
        //添加editText的监听事件  
        holder.productMoney.setOnEditorActionListener(new MyEditorListener());
        holder.productName.setOnTouchListener(new MyTouchListener(position));
    }

    public class MustViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_product_id)
        public TextView productId;
        @BindView(R.id.et_product_name)
        public EditText productName;
        @BindView(R.id.et_product_money)
        public EditText productMoney;
        @BindView(R.id.tv_product_income)
        public TextView productIncome;
        @BindView(R.id.tv_product_time)
        public TextView productTime;

        private MustViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 自定义回调接口
     */
    public interface MustListener {
        void setMustMoney();

        void setMustName(int position);
    }

   //选择产品名称
    private class MyTouchListener implements View.OnTouchListener {

        private int pos;

        private MyTouchListener(int pos) {
            this.pos = pos;
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                listener.setMustName(pos);
            }
            return true;
        }
    }

    //点击完成的时候传递投资金额
    private class MyEditorListener implements TextView.OnEditorActionListener {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                listener.setMustMoney();
            }
            return true;
        }
    }
}