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.gamegroup.util.UMConfigUtils;
import com.sheep.gamegroup.util.ViewUtil;
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) {
        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_listview,null);

            holder.hot_tv = convertView.findViewById(R.id.hot_tv);
            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);
        }
        final HomeListEntity entity = getItem(position);
        ViewUtil.setText(holder.hot_tv, entity.getTip());
        holder.hot_tv.setVisibility(entity.hasTip()?View.VISIBLE: View.GONE);
        holder.hot_tv.setTextColor(entity.getTipColor());
        Glide.with(context)
                .load(entity.getIcon())
                .apply(new RequestOptions().dontAnimate())
                .into(holder.q_iv);
        ViewUtil.setText(holder.q_name_tv, entity.getTitle());
        ViewUtil.setText(holder.q_dec_tv, 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) {
                UMConfigUtils.IdEvent.HOME_LIST.commit(entity.getId());
                CommonUtil.getInstance()
                        .goWhere(context, entity, "首页");
            }
        });
        return convertView;
    }

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