package com.yutou.jianr_mg.Adapters;

import android.content.Context;
import android.content.Intent;
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.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.yutou.jianr_mg.Data.TMod;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Utils;
import com.yutou.jianr_mg.views.ModActivity;


import java.util.List;


/**
 * Created by 58381 on 2018/2/14.
 */

public class ReModListAdapter extends BaseAdapter {
    private Context context;
    private ImageLoader imageLoader;
    private List<TMod> list;

    public ReModListAdapter(Context context,List<TMod> list) {
        this.list = list;
        this.context=context;
        imageLoader= Utils.initImageLoader(context);
    }

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

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Items items;
        if(view==null){
            view= LayoutInflater.from(context).inflate(R.layout.item_remod,null);
            items=new Items();
            items.title=view.findViewById(R.id.title);
            items.info=view.findViewById(R.id.info);
            items.icon=view.findViewById(R.id.icon);
            view.setTag(items);
        }else{
            items= (Items) view.getTag();
        }
        items.title.setText(list.get(i).getTitle());
        items.info.setText(list.get(i).getOverview());
        imageLoader.displayImage(list.get(i).getIcon(),items.icon);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ModActivity.class);
                intent.putExtra("mod", JSON.toJSONString(list.get(i)));
                context.startActivity(intent);
            }
        });
        return view;
    }
    private class Items{
        private ImageView icon;
        private TextView title,info;
    }
}
