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.AbsListView;
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.CommonUtil;
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) {
        ViewHolder holder = null;
        if(holder == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adp_homelist_gridview, null);
            holder.gridview_item_name_tv = convertView.findViewById(R.id.gridview_item_name_tv);
            holder.gridview_item_detail_tv = convertView.findViewById(R.id.gridview_item_detail_tv);
            holder.gridview_item_icon_iv = convertView.findViewById(R.id.gridview_item_icon_iv);
            // 获取屏幕宽度
            int W = G.WIDTH <G.HEIGHT ? G.WIDTH : G.HEIGHT;
            int cricleRadius = W / 2 - 40;
            int h = cricleRadius  / 2;
            convertView.setLayoutParams(new AbsListView.LayoutParams(cricleRadius, h));
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final HomeListEntity entity = getItem(position);
        holder.gridview_item_name_tv.setText(entity.getTitle()+"");
        holder.gridview_item_detail_tv.setText(entity.getDesc()+"");
        Glide.with(context)
                .load(entity.getIcon())
                .apply(new RequestOptions().dontAnimate())
                .into(holder.gridview_item_icon_iv);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.getInstance()
                        .goWhere(context, entity);
            }
        });
        return convertView;
    }

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