package com.mjsheng.myappstore.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.mjsheng.myappstore.R;
import com.mjsheng.myappstore.bean.AppDateInfo;
import com.mjsheng.myappstore.image.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class GaringRecommedAdapter extends BaseAdapter {

    private Context context;
    private List<AppDateInfo> mRecommedAppList = new ArrayList<>();

    public GaringRecommedAdapter(Context context) {
        this.context = context;
    }


    public void setData(List<AppDateInfo> datas) {
        if (mRecommedAppList != null && !mRecommedAppList.isEmpty()) {
            mRecommedAppList.clear();
        }
        this.mRecommedAppList.addAll(datas);
    }

    @Override
    public int getCount() {
        if (mRecommedAppList == null) {
            return 0;
        }
        return mRecommedAppList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mRecommedAppList.get(arg0);
    }

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


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
                convertView =  LayoutInflater.from(context).inflate(R.layout.caring_recom_item, null);
            holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
            holder.appName = (TextView) convertView.findViewById(R.id.app_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final AppDateInfo info = (AppDateInfo) getItem(position);

        if (info != null) {

            ImageLoader.getDefault().loadUrl(context, info.getApp_img(),  holder.appIcon);
            holder.appName.setText(info.getApp_name());
        }
        return convertView;
    }

    public final class ViewHolder {
        public ImageView appIcon;
        public TextView appName;
    }

}
