package com.sheep.gamegroup.view.adapter;

import android.content.Context;
import android.text.TextUtils;
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.sheep.gamegroup.model.entity.Release_task;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;

/**
 * Created by realicing on 2018/5/9.
 */

public class CreditCardTaskAdp extends BaseAdapter {
    private Context context;
    private List<Release_task> etyList;

    public CreditCardTaskAdp(Context context, List<Release_task> etyList) {
        this.context = context;
        this.etyList = etyList;
    }

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

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

    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(final 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.credit_card_task, null);
            holder.credit_card_task_iv = convertView.findViewById(R.id.credit_card_task_iv);
            holder.credit_card_task_name_tv = convertView.findViewById(R.id.credit_card_task_name_tv);
            holder.credit_card_task_tv = convertView.findViewById(R.id.credit_card_task_tv);
            holder.credit_card_task_tip1_tv = convertView.findViewById(R.id.credit_card_task_tip1_tv);
            holder.credit_card_task_tip2_tv = convertView.findViewById(R.id.credit_card_task_tip2_tv);
            convertView.setTag(holder);
        }

        final Release_task ety = (Release_task) getItem(position);
        if(ety != null) {
            holder.credit_card_task_tv.setText("+");
            holder.credit_card_task_tv.append(ety.getBonusText());
            holder.credit_card_task_tv.append("元");
            holder.credit_card_task_name_tv.setText(ety.getName());
            String[] remarks = new String[0];
            if(ety.getTask() != null) {
                if(!TextUtils.isEmpty(ety.getTask().getIcon()))
                    Glide.with(context).load(ety.getTask().getIcon()).into(holder.credit_card_task_iv);
                if(!TextUtils.isEmpty(ety.getTask().getRemarks())) {
                    remarks = ety.getTask().getRemarks().split(";");
                }
            }
            holder.credit_card_task_tip1_tv.setText(remarks.length > 0 ? remarks[0] : "");
            holder.credit_card_task_tip2_tv.setText(remarks.length > 1 ? remarks[1] : "");
        }
        return convertView;
    }

    class ViewHolder{
        ImageView credit_card_task_iv;
        TextView credit_card_task_name_tv, credit_card_task_tv;
        TextView credit_card_task_tip1_tv, credit_card_task_tip2_tv;
    }

}
