package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
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.event.BigEvent;
import com.kfzs.duanduan.event.EventTypes;
import com.sheep.gamegroup.model.entity.DialogEntity;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.model.entity.TaskEty;
import com.sheep.gamegroup.model.entity.TaskReleaseEty;
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.jiuyan.samllsheep.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

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) {
        ViewHolder holder = null;
        if(holder == null){
            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);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final TaskAcceptedEty ety = (TaskAcceptedEty) getItem(position);
        holder.name_tv.setText(ety.getRelease_task().getName()+"");
        try {
            holder.date_tv.setText(TimeUtil.getDate(TimeUtil.FORMAT, Long.valueOf(ety.getUpdate_time()))+"");
        }catch (Exception e){
            e.printStackTrace();
        }
        holder.price_tv.setText(returnValue(ety));
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TaskEty task = ety.getRelease_task().getTask();
                task.setScreenshots(ety.getScreenshots());

                TaskReleaseEty releaseTask = ety.getRelease_task();
                releaseTask.setAccepted_task_id(ety.getId());

                Jump2View.getInstance().goTaskDetailView(context, ety);
            }
        });
        TestUtil.setDebugDownLoad(context, convertView, ety.getRelease_task().getTask());
        holder.detail_task_tv.setText(ety.getRemarks());
//        holder.detail_task_tv.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                uploadImag(context);
//                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.TASK_AGAIN_UPLOAD_FILE)
//                        .setData(ety));
//            }
//        });
        Glide.with(context)
                .load(ety.getRelease_task().getTask().getIcon())
                .apply(new RequestOptions().placeholder(R.mipmap.x_ic_def_head))
                .into(holder.icon_iv);
        return convertView;
    }

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

    /**
     * 1,已接受任务 2,正在进行中,3完成任务,4放弃任务,5任务失败,6审核失败,7提交审核
     * @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().getBonus() +"元";
                break;
            case 4:
                stateStr = "放弃任务";
                break;
            case 5:
                stateStr = "任务失败";
                break;
            case 6:
                stateStr = "审核失败";
                break;
            case 7:
                stateStr = "提交审核";
                break;
            case 8:
                stateStr = "任务已下线";
                break;
        }
        return stateStr;
    }
    /**
     * update file
     */
    public void uploadImag(Context context) {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        ((Activity)context).startActivityForResult(intent, 1);
    }
}
