package com.brilliance.isc.common.taskdistributor.controller;

import com.brilliance.isc.bo.Que;
import com.brilliance.isc.common.taskdistributor.request.AssignTaskRequest;
import com.brilliance.isc.common.taskdistributor.request.InrRequest;
import com.brilliance.isc.common.taskdistributor.request.UserIdRequest;
import com.brilliance.isc.common.taskdistributor.response.TaskListResponse;
import com.brilliance.isc.common.taskdistributor.service.TaskDistService;
import com.brilliance.isc.common.vo.BaseRequestPageVo;
import com.brilliance.isc.common.vo.PageVo;
import com.brilliance.isc.vo.ResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/taskdist")
public class TaskDistController {

    @Autowired
    private TaskDistService taskDistService;

    @PostMapping("/create")
    public ResponseSet<Boolean> createNewTask(@RequestBody Que que) {
        // 此处应使用线程池异步处理，然后直接返回
        taskDistService.handleTaskEnqueue(que);
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }

    // FIXME for test only
    @PostMapping("/createById")
    public ResponseSet<Boolean> createNewTask(@RequestBody InrRequest request) {
        // 此处应使用线程池异步处理，然后直接返回
        taskDistService.handleTaskEnqueue(request.getInr());
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }

    @PostMapping("/query/assigned")
    public ResponseSet<TaskListResponse> queryAssignedTasks(@RequestBody UserIdRequest request) {
        Assert.notNull(request.getExtkey(), "柜员号不能为空");
        return ResponseSet.simpleSuccess(taskDistService.queryTodoList(request.getExtkey()));
    }

    @PostMapping("/query/available")
    public ResponseSet<PageVo<Que>> queryAvailableTasks(@RequestBody BaseRequestPageVo request) {
        return ResponseSet.simpleSuccess(taskDistService.queryAvailableTasks(request));
    }

    @PostMapping("/pin")
    public ResponseSet<Boolean> pinTask(InrRequest request) {
//        Assert.notNull(request.getInr(), "任务ID不能为空");
        return ResponseSet.simpleSuccess(taskDistService.pinTask(request.getInr()));
    }

    @PostMapping("/unpin")
    public ResponseSet<Boolean> unpinTask(InrRequest request) {
//        Assert.notNull(request.getInr(), "任务ID不能为空");
        return ResponseSet.simpleSuccess(taskDistService.unpinTask(request.getInr()));
    }

    @PostMapping("/assign/manual")
    public ResponseSet<Map<String,Integer>> manualAssignTasks(@RequestBody AssignTaskRequest request) {
        return ResponseSet.simpleSuccess(taskDistService.manualAssignTasks(request));
    }

    @PostMapping("/assign/manualsingle")
    public ResponseSet<Integer> manualAssignSingleTasks(@RequestBody AssignTaskRequest request) {
        Map<String,String> rtnMap = new HashMap<>();
        int rtnCod  = taskDistService.manualAssignSingleTasks(request);
        if(rtnCod == -1){
            rtnMap.put("rtncod","E00001");
            rtnMap.put("rtnmsg","柜员已达阀值，无法分配");
        }else if(rtnCod == -2){
            rtnMap.put("rtncod","E00002");
            rtnMap.put("rtnmsg","无法给经办柜员分配待复核业务");
        }else if(rtnCod == -3){
            rtnMap.put("rtncod","E00003");
            rtnMap.put("rtnmsg","无法给复核柜员分配主管复核业务");
        }else if(rtnCod == 1) {
            rtnMap.put("rtncod", "AAAAAA");
            rtnMap.put("rtnmsg", "分配成功");
        }
        return ResponseSet.simpleSuccess(rtnMap);
    }

    @PostMapping("/assign/auto")
    public ResponseSet<Boolean> autoAssignTasks() {
        taskDistService.fullAutoDistribute();
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }

    @PostMapping("/assign/cor2queByTask")
    public ResponseSet<Boolean> goBackToque(Que task){
        taskDistService.corToQueue(task);
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }

    @PostMapping("/assign/cor2que")
    public ResponseSet<Boolean> goBackToque(@RequestBody AssignTaskRequest request){
        taskDistService.corToQueue(request);
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }

    @PostMapping("/assign/commit")
    public ResponseSet<Boolean> commitTask(@RequestBody UserIdRequest request){
        taskDistService.commitTask(request.getExtkey());
        return ResponseSet.simpleSuccess(Boolean.TRUE);
    }
}
