package com.mjsheng.myappstore.adapter;

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


import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.bean.AppDateInfo;
import com.mjsheng.myappstore.bean.CategoryInfo;
import com.mjsheng.myappstore.image.ImageLoader;
import com.mjsheng.myappstore.widget.Sound;

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


public class CategoryTableAdapter extends RecyclerView.Adapter<CategoryTableAdapter.ViewHolder> {
    public BrandItemClick click;
    private List<CategoryInfo> mOriginalValues = new ArrayList<>();
    private Context mContext;
    private int[] typeImg;
    private Sound sound;

    public CategoryTableAdapter(Context context, List<CategoryInfo> mOriginalValues, int[] Img) {
        if (mOriginalValues != null && mOriginalValues.size() >= 0) {
            this.mOriginalValues.addAll(mOriginalValues);
        }
        mContext = context;
        typeImg = Img;
        sound = new Sound(context);
    }

    public void loadmore(List<CategoryInfo> mOriginalValues) {
        this.mOriginalValues.clear();
        this.mOriginalValues = mOriginalValues;
        notifyDataSetChanged();
        //        notifyItemInserted(mOriginalValues.size() - 1);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = View.inflate(parent.getContext(), R.layout.category_table_item, null);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CategoryInfo info = mOriginalValues.get(position);
        if (info == null) {
            return;
        }
        holder.mNameTv.setText(info.getName());
        ImageLoader.getDefault().load(mContext, mOriginalValues.get(position).getImg(), holder.mTypeIv);
        holder.mTypeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sound.playSoundEffect();
                if (click != null) {
                    click.brandItemClick(position);
                }
            }
        });

    }

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

    public interface BrandItemClick {
        void brandItemClick(int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView mTypeIv;
        public TextView mNameTv;
        public RelativeLayout mLayout;

        public ViewHolder(final View itemView) {
            super(itemView);
            mTypeIv = (ImageView) itemView.findViewById(R.id.category_iv);
            mNameTv = (TextView) itemView.findViewById(R.id.category_name);
            mLayout = (RelativeLayout) itemView.findViewById(R.id.category_bg);
            //            ImageLoader.getDefault().loadBackground(mContext, R.drawable.category_bg, mLayout);
        }
    }
}
