package com.danding.yang800.Adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.danding.yang800.Entity.GoodsSortInfo;
import com.danding.yang800.R;
import com.zhy.autolayout.utils.AutoUtils;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by baoyz on 2014/6/29.
 */
public class SelectgoodAdapter extends RecyclerView.Adapter<SelectgoodAdapter.ViewHolder> implements View.OnClickListener {
    private Context ctx;
    // 数据集
    public ArrayList<GoodsSortInfo> infos;
    public List<Boolean> isClicks;

    public SelectgoodAdapter(Context ctx, ArrayList<GoodsSortInfo> infos) {
        super();
        this.ctx = ctx;
        this.infos = infos;
        isClicks = new ArrayList<>();
        for (int i = 0; i < infos.size(); i++) {
            if (i == 0) {
                isClicks.add(true);
            } else {
                isClicks.add(false);
            }
        }
    }

    public interface OnItemClickListener {
        /*position此处是每一个Item所在的位置，作为Tag*/
        public void setOnItemClick(View v, int position);
    }

    private OnItemClickListener listener = null;

    /*暴露给外部的接口，给内部类的对象赋值*/
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        // 创建一个View，简单起见直接使用系统提供的布局，就是一个TextView
        View view = LayoutInflater.from(ctx).inflate(R.layout.select_adapter_item, viewGroup, false);

        // 创建一个ViewHolder
        ViewHolder holder = new ViewHolder(view);

        holder.mTextView = (TextView) view.findViewById(R.id.item);
        holder.mTextView.setOnClickListener(this);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        // 绑定数据到ViewHolder上
        //将position保存在itemView的Tag中，以便点击时进行获取
        if (isClicks.get(position)) {
            viewHolder.mTextView.setTextColor(Color.parseColor("#ffffff"));
            viewHolder.mTextView.setBackgroundColor(ContextCompat.getColor(ctx, R.color.hhpink));
        } else {
            viewHolder.mTextView.setTextColor(Color.parseColor("#fcc6c6"));
            viewHolder.mTextView.setBackgroundColor(ContextCompat.getColor(ctx, R.color.pink));
        }
        viewHolder.mTextView.setTag(position);
        GoodsSortInfo info = infos.get(position);
        viewHolder.mTextView.setText(info.getSortName());
    }

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

    @Override
    public void onClick(View v) {
        if (listener != null) {
            v.setBackgroundColor(ContextCompat.getColor(ctx, R.color.hhpink));
             listener.setOnItemClick(v, (int) v.getTag());
            for (int i = 0; i < isClicks.size(); i++) {
                isClicks.set(i, false);
                infos.get(i).setClicks(false);
            }
            isClicks.set((int) v.getTag(), true);
            infos.get((int) v.getTag()).setClicks(true);
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            AutoUtils.autoSize(itemView);
        }
    }
}
