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.CommonUtil;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;

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

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

    public AdpHomeListListview(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_listview,null);

            holder.q_iv = convertView.findViewById(R.id.q_iv);
            holder.q_name_tv = convertView.findViewById(R.id.q_name_tv);
            holder.q_dec_tv = convertView.findViewById(R.id.q_dec_tv);
            holder.line_ly_q_view = convertView.findViewById(R.id.line_ly_q_view);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        final HomeListEntity entity = getItem(position);
        Glide.with(context)
                .load(entity.getIcon())
                .apply(new RequestOptions().dontAnimate())
                .into(holder.q_iv);
        holder.q_name_tv.setText(entity.getTitle()+"");
        holder.q_dec_tv.setText(entity.getDesc()+"");

        if(position == getCount()-1){
            holder.line_ly_q_view.setVisibility(View.GONE);
        }else {
            holder.line_ly_q_view.setVisibility(View.VISIBLE);
        }

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtil.getInstance()
                        .goWhere(context, entity);
            }
        });
        return convertView;
    }

    class ViewHolder{
        ImageView q_iv;
        TextView q_name_tv, q_dec_tv;
        View line_ly_q_view;
    }
}
