package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
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.bumptech.glide.request.RequestOptions;
import com.kfzs.duanduan.utils.NumberFormatUtils;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.presenter.TaskListPresenter;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TestUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;

import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

/**
 * Created by ljy on 2018/3/21.
 */

public class TaskListItemAdp extends BaseAdapter {
    private Context context;
    private List<TaskAcceptedEty> etyList;
    @Inject
    TaskListPresenter presenter;

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

    /**
     * 传入presenter
     */
    public void addPresenter(TaskListPresenter presenter){
        this.presenter = presenter;
    }
    @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(int position, View convertView, ViewGroup parent) {
        final Object object = getItem(position);
        if(!(object instanceof TaskAcceptedEty)){
            return LayoutInflater.from(context).inflate(R.layout.find_item_bottom, null);
        }
        final TaskAcceptedEty ety = (TaskAcceptedEty) object;
        if(ety.getRelease_task() == null || ety.getRelease_task().getTask() == null){
            return convertView;
        }
        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.task_list_item, null);
            holder.name_tv = convertView.findViewById(R.id.name_tv);
            holder.date_tv = convertView.findViewById(R.id.date_tv);
            holder.price_tv = convertView.findViewById(R.id.price_tv);
            holder.icon_iv = convertView.findViewById(R.id.icon_iv);
            holder.detail_task_tv = convertView.findViewById(R.id.detail_task_tv);
            holder.desc_tv = convertView.findViewById(R.id.desc_tv);
            convertView.setTag(holder);
        }

        ViewUtil.setText(holder.name_tv, ety.getRelease_task().getName());
        ViewUtil.setText(holder.date_tv, TimeUtil.getDate(TimeUtil.FORMAT, NumberFormatUtils.parseLong(ety.getUpdate_time())));
        String stateStr;
        if(ety.getStatus() == 3){
            stateStr = String.format(Locale.CHINA, "+%s元", ety.getRelease_task().getBonusText());
            holder.price_tv.setTextColor(context.getResources().getColor(R.color.txt_red));
        }else {
            stateStr = String.format(Locale.CHINA, "%s元", ety.getRelease_task().getBonusText());
            holder.price_tv.setTextColor(context.getResources().getColor(R.color.time_created));
        }
        holder.price_tv.setText(stateStr);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Jump2View.getInstance().goTaskDetailViewFromTaskList((Activity) context, ety.getRelease_task_id());
            }
        });
        TestUtil.setDebugDownLoad(context, convertView, ety.getRelease_task().getTask());
        returnValue3(ety, holder.detail_task_tv,holder.desc_tv);
        Glide.with(context)
                .load(ety.getRelease_task().getTask().getIcon())
                .apply(new RequestOptions().placeholder(R.mipmap.icon))
                .into(holder.icon_iv);
        return convertView;
    }

    class ViewHolder{
        ImageView icon_iv;
        TextView name_tv, date_tv, price_tv,detail_task_tv,desc_tv;
    }

    /**
     * 1,已接受任务 2,正在进行中,3完成任务,4放弃任务,5任务失败,6审核失败,7提交审核8任务已下线9连续任务
     * @return
     */
    private String returnValue(TaskAcceptedEty ety){
        String stateStr = "已接受任务";
        switch (ety.getStatus()){
            case 1:
                stateStr = "已接受任务";
                break;
            case 2:
                stateStr = "正在进行中";
                break;
            case 3:
                stateStr = "+"+ ety.getRelease_task().getBonusText() +"元";
                break;
            case 4:
                stateStr = "放弃任务";
                break;
            case 5:
                stateStr = "任务失败";
                break;
            case 6:
                stateStr = "审核失败";
                break;
            case 7:
                stateStr = "提交审核";
                break;
            case 8:
                stateStr = "任务已下线";
                break;
        }
        return stateStr;
    }
    private String returnValue3(TaskAcceptedEty ety, TextView textView, TextView desc_tv){
        desc_tv.setVisibility(View.GONE);
        String stateStr = "已接受任务";
        switch (ety.getStatus()){
            default:
            case 1://提交中
            case 2:
            case 7:
                textView.setBackgroundResource(R.drawable.shape_blue_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.btn_color_main_stroke));
                textView.setText("审核中");
                break;
            case 3://完成
            case 9:
                textView.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.gray_CCCCCC));
                textView.setText("已完成");
                break;
            case 4://失败
            case 5:
            case 8:
                textView.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.gray_CCCCCC));
                textView.setText("任务失败");
                break;
            case 6:
                textView.setBackgroundResource(R.drawable.shape_red_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.red_FD2D54));
                textView.setText("重新提交");
                desc_tv.setVisibility(View.VISIBLE);
                desc_tv.setText(TextUtils.isEmpty(ety.getRemarks())?"":"失败原因:"+ety.getRemarks());
                break;
        }
        return stateStr;
    }
}
