package com.mjsheng.myappstore.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.bean.CommonPicData;
import com.mjsheng.myappstore.bean.ProjectDateInfo;
import com.mjsheng.myappstore.image.ImageLoader;

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


public class BrandListAdapter extends RecyclerView.Adapter<BrandListAdapter.ViewHolder> {
    public BrandItemClick click;
    private List<CommonPicData> mOriginalValues = new ArrayList<>();
    private Activity mActivity;

    public BrandListAdapter(List<CommonPicData> mOriginalValues, Activity activity) {
        if (mOriginalValues != null) {
            this.mOriginalValues.addAll(mOriginalValues);
        }
        this.mActivity = activity;
    }

    public void loadmore(List<CommonPicData> mList) {
        mOriginalValues = mList;
        mOriginalValues.addAll(mList);
        notifyDataSetChanged();
    }
    public void addData(List<CommonPicData> mList) {
        mOriginalValues.clear();
        mOriginalValues.addAll(mList);
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        View v = View.inflate(parent.getContext(), R.layout.item_brand, null);
        v.setLayoutParams(layoutParams);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        CommonPicData info = mOriginalValues.get(position);
        if (info == null) {
            return;
        }
        holder.brandBgIv.setScaleType(ImageView.ScaleType.FIT_CENTER);
        ImageLoader.getDefault().loadUrl(mActivity, info.getImg(), holder.brandBgIv);
        holder.brandBgIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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 brandBgIv;

        public ViewHolder(final View itemView) {
            super(itemView);
            brandBgIv = (ImageView) itemView.findViewById(R.id.iv_brand_bg);
        }
    }
}
