package com.ygtx.emcs.activity.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.ygtx.emcs.R;
import com.ygtx.emcs.activity.model.MainModel;

import java.util.ArrayList;


public class FunctionMenuAdapter extends BaseAdapter {
    private ArrayList<MainModel> list;
    private LayoutInflater inflater = null;
    private Context context;

    // 构造器
    public FunctionMenuAdapter(ArrayList<MainModel> list, Context context) {
        this.list = list;
        this.inflater =  LayoutInflater.from(context);;
        this.context = context;
    }

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

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

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

    public int getDrawableIdByName( String name) {
        return context.getResources().getIdentifier(name, "drawable", context.getPackageName());
    }

    public int getMipmapIdByName( String name) {
        return context.getResources().getIdentifier(name, "mipmap", context.getPackageName());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MainHolder holder = new MainHolder();
        final MainModel shop = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.ygtx_home_item, null);
            holder.logo = (ImageView) convertView.findViewById(R.id.logo);
            holder.shopTitle = (TextView)convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            holder = (MainHolder) convertView.getTag();
        }
        if (shop.getLogo()!=null){
            holder.logo.setImageResource(getMipmapIdByName(shop.getLogo()));
        }else{
            convertView.setVisibility(View.GONE);
        }
        if(shop.getTitle()!=null){
            holder.shopTitle.setText(shop.getTitle());
        }
        return convertView;
    }

    class MainHolder {
        public ImageView logo;
        public TextView shopTitle;
    }
}
