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.sheep.gamegroup.model.entity.CreditCardProgressQuery;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.model.util.EntityUtils;
import com.sheep.gamegroup.util.BtStatusUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.view.activity.ActCreditCardTaskList;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.gamegroup.absBase.AbsChooseImageActivity;

import java.util.List;

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

public class CreditCardProgressQueryAdp extends BaseAdapter {
    private Context context;
    private List<CreditCardProgressQuery> etyList;

    public CreditCardProgressQueryAdp(Context context, List<CreditCardProgressQuery> 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_progress_query, 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_find_tv = convertView.findViewById(R.id.credit_card_task_find_tv);
            holder.credit_card_task_upload_tv = convertView.findViewById(R.id.credit_card_task_upload_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 CreditCardProgressQuery creditCardProgressQuery = (CreditCardProgressQuery) getItem(position);
        final Release_task ety = creditCardProgressQuery.getRelease_task();
        try {
            Glide.with(context).load(ety.getTask().getIcon())
                    .into(holder.credit_card_task_iv);
            holder.credit_card_task_name_tv.setText(ety.getName());
            String[] remarks = ety.getTask().getRemarks().split(";");
            holder.credit_card_task_tip1_tv.setText(remarks.length > 0 ? remarks[0] : "");
            holder.credit_card_task_tip2_tv.setText("¥"+ety.getBonusText());
            holder.credit_card_task_find_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Jump2View.getInstance().goWeb(context, EntityUtils.getExtInfo(ety.getTask().getExt()).getQuery_url(), "进度查询");
                }
            });
            holder.credit_card_task_upload_tv.setEnabled(BtStatusUtil.getEnabled(creditCardProgressQuery.getStatus()));
            holder.credit_card_task_upload_tv.setText(BtStatusUtil.getUploadText(creditCardProgressQuery));
            holder.credit_card_task_upload_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(context instanceof ActCreditCardTaskList){
                        ((ActCreditCardTaskList) context).setCreditCardProgressQuery(creditCardProgressQuery);
                        ((ActCreditCardTaskList) context).showChooseDialog(false, true, AbsChooseImageActivity.DEFAULT_MAX_COUNT);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

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

}
