package com.taikor.investment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.taikor.investment.R;
import com.taikor.investment.bean.SettingProduct;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 智能投顾，灵活型，添加必配置产品，适配器
 * Created by Any on 2017/5/4.
 */
public class FlexibleGroupAdapter extends RecyclerView.Adapter<FlexibleGroupAdapter.MyViewHolder>{

    private DeleteItemListener listener;
    private List<SettingProduct> mList;
    private List<String> nameList;
    private Context mContext;
    
    public interface DeleteItemListener {
        void setProductName(int position);
        void delete(int position);
    }

    public FlexibleGroupAdapter(Context context) {
        this.mContext = context;
        listener = (DeleteItemListener) mContext;
    }

    public void setData(List<SettingProduct> list,List<String> nameList) {
        this.mList = list;
        this.nameList=nameList;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_flexible, parent, false);
        return new MyViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        if(nameList==null){
            holder.tvProduct.setText("");
        }else{
            for(int i=0;i<nameList.size();i++){
                if(i==position){
                    holder.tvProduct.setText(nameList.get(i));
                    break;
                }
            }
        }
      
        //设置产品名称
        holder.tvProduct.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    listener.setProductName(holder.getAdapterPosition());
                }
                return true;
            }
        });
        
        //删除
        holder.clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.delete(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    
    public class MyViewHolder extends RecyclerView.ViewHolder{

        @BindView(R.id.tv_product)
        public TextView tvProduct;
        @BindView(R.id.iv_clear)
        ImageView clear;

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