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.util.CommonUtil;
import com.sheep.gamegroup.util.Jump2View;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.service.DownloadService;

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


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

public class TaskListItemAdp extends BaseAdapter {
    private Context context;
    private List<TaskAcceptedEty> etyList;

    public TaskListItemAdp(Context context, List<TaskAcceptedEty> 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(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, context.getString(R.string.commit_time_point) + 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));
        }
        CommonUtil.getInstance().setTaskTag(context, convertView, ety.getRelease_task(),CommonUtil.TASK_AUDIT);
        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());
            }
        });
        DownloadService.setDownLoadLongClick(convertView, ety.getRelease_task().getTask());
        setTaskStatusText(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;
    }

    //设置任务完成状态与失败原因
    private void setTaskStatusText(TaskAcceptedEty ety, TextView textView, TextView desc_tv){
        desc_tv.setVisibility(View.GONE);
        switch (ety.getStatus()){
            default:
            case TaskAcceptedEty.STATUS_ACCEPTED_TASK://提交中
            case TaskAcceptedEty.STATUS_RUNNING:
            case TaskAcceptedEty.STATUS_SUBMIT:
                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 TaskAcceptedEty.STATUS_FINISHED://完成
            case TaskAcceptedEty.STATUS_AT_LEAST_COMPLETE_ONE:
                textView.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.gray_CCCCCC));
                textView.setText("已完成");
                break;
            case TaskAcceptedEty.STATUS_GIVE_UP://失败
            case TaskAcceptedEty.STATUS_TASK_FAIL:
            case TaskAcceptedEty.STATUS_OFF_LINE:
                textView.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.gray_CCCCCC));
                textView.setText("任务失败");
                break;
            case TaskAcceptedEty.STATUS_AUDIT_FAILURE:
                textView.setBackgroundResource(R.drawable.shape_red_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.red_FD2D54));
                textView.setText("重新提交");
                if (!TextUtils.isEmpty(ety.getRemarks())) {
                    desc_tv.setVisibility(View.VISIBLE);
                    desc_tv.setText(context.getString(R.string.fail_reason, ety.getRemarks()));
                }
                break;
        }
        switch (ety.getAllowSubmit()){
            case TaskAcceptedEty.ALLOW_SUBMIT_NO:
                textView.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.gray_CCCCCC));
                textView.setText("任务失败");
                if (!TextUtils.isEmpty(ety.getRemarks())) {
                    desc_tv.setVisibility(View.VISIBLE);
                    desc_tv.setText(context.getString(R.string.fail_reason, ety.getRemarks()));
                }
                break;
            case TaskAcceptedEty.ALLOW_SUBMIT_YES:
                textView.setBackgroundResource(R.drawable.shape_red_stroke_rectangle_10_radius);
                textView.setTextColor(context.getResources().getColor(R.color.red_FD2D54));
                textView.setText("重新提交");
                if (!TextUtils.isEmpty(ety.getRemarks())) {
                    desc_tv.setVisibility(View.VISIBLE);
                    desc_tv.setText(context.getString(R.string.fail_reason, ety.getRemarks()));
                }
                break;
        }
    }
}
