package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.sheep.gamegroup.model.entity.HomeListEntity;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import java.util.List;

/**
 * Created by ljy on 2018/5/10.
 */

public class AdpHomeListGrideview extends BaseAdapter {
    private List<HomeListEntity> homeListEntity;
    private Context context;

    public AdpHomeListGrideview(List<HomeListEntity> homeListEntity, Context context) {
        this.homeListEntity = homeListEntity;
        this.context = context;
    }

    @Override
    public int getCount() {
        return homeListEntity.size();
    }

    @Override
    public HomeListEntity getItem(int position) {
        return homeListEntity.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Object tag = convertView == null ? null : convertView.getTag();
        ViewHolder holder;
        if(tag instanceof ViewHolder){
            holder = (ViewHolder) tag;
        }else {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adp_homelist_gridview_new, null);
            holder.gridview_item_tag_tv = convertView.findViewById(R.id.gridview_item_tag_tv);
            holder.gridview_item_name_tv = convertView.findViewById(R.id.gridview_item_name_tv);
            holder.gridview_item_icon_iv = convertView.findViewById(R.id.gridview_item_icon_iv);
            convertView.setTag(holder);
        }
        final HomeListEntity entity = getItem(position);
        ViewUtil.updateTag(holder.gridview_item_tag_tv, entity);
        ViewUtil.setText(holder.gridview_item_name_tv, entity.getTitle());
        Glide.with(context)
                .load(entity.getIcon())
                .apply(new RequestOptions().dontAnimate().override(G.WIDTH / 4 /3, G.WIDTH /4 /3))
                .into(holder.gridview_item_icon_iv);
        return convertView;
    }

    class ViewHolder{
        TextView gridview_item_name_tv, gridview_item_tag_tv;
        ImageView gridview_item_icon_iv;
    }
}
