package jnpf.cultivate.v2.controller.app.task;

import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.v2.service.V2CultivateTaskService;
import jnpf.cultivate.v2.util.CultivateCourseStudyUtil;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.learn.FtbCultivateLearnAllocationDTO;
import jnpf.model.cultivate.v2.course.vo.app.V2CourseAppDto;
import jnpf.model.cultivate.v2.enums.PositionBusinessSourceEnum;
import jnpf.model.cultivate.v2.task.req.CheckUserTaskAllPhasesCompleteReq;
import jnpf.model.cultivate.v2.task.req.V2CultivateLearnTaskListForManagerReq;
import jnpf.model.cultivate.v2.task.req.V2MyCultivateTaskListForReq;
import jnpf.model.cultivate.v2.task.vo.*;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * app 培训任务模块
 *
 * @author xgl
 */

@RestController
@Slf4j
@RequestMapping("/v2/app/task")
public class V2CultivateTaskAppController {

    @Autowired
    private V2CultivateTaskService cultivateTaskService;

    @Autowired
    private CultivateCourseStudyUtil cultivateCourseStudyUtil;


    /**
     * 任务管理-查询任务列表
     */
    @GetMapping("/query-task-list")
    public ActionResult<PageListVO<V2CultivateTaskListForManagerVo>> queryTaskList(V2CultivateLearnTaskListForManagerReq req) {
        return ActionResult.success(cultivateTaskService.queryTaskListForApp(req));
    }


    /**
     * 任务管理-tab栏小气泡数
     * noStart 未开始
     * doing 进行中
     * over 已经结束
     *
     * @param keyWord 任务名称
     * @return Map<String, Integer>
     */
    @GetMapping("/query-task-count")
    public ActionResult<Map<String, Integer>> queryTaskCount(@RequestParam(value = "keyWord", defaultValue = "", required = false) String keyWord) {
        return ActionResult.success(cultivateTaskService.queryTaskCountForApp(keyWord));
    }


    /**
     * 任务管理-任务详情展示
     */
    @GetMapping("/query-task-info/{taskId}")
    public ActionResult<V2CultivateTaskDetailsVo> queryTaskInfo(@PathVariable("taskId") String taskId) {
        return ActionResult.success(cultivateTaskService.get(taskId));
    }

    /**
     * 任务管理-任务完成统计
     *
     * @param taskId 任务id
     * @return V2CultivateLearnTaskFinishStatisticsVo
     */
    @GetMapping("/query-complete-statistics/{taskId}")
    public ActionResult<V2CultivateLearnTaskFinishStatisticsVo> getCompletionStatistics(@PathVariable("taskId") String taskId) {
        return ActionResult.success(cultivateTaskService.getCompletionStatisticsForManagerApp(taskId));
    }

    /**
     * 任务管理-人员完成情况列表
     *
     * @param taskId 任务id
     * @return PageListVO<V2MyCultivateLearnTaskFinishUserListVo>
     */
    @GetMapping("/query-task-user-list/{taskId}")
    public ActionResult<PageListVO<V2CultivateTaskFinishUserListVo>> queryTaskUserList(@PathVariable("taskId") String taskId,
                                                                                       CultivatePage page) {
        return ActionResult.success(cultivateTaskService.queryTaskUserListForManagerApp(taskId, page));
    }


    /**
     * 分配任务给用户
     *
     * @param req 任务分配数据传输对象，包含任务ID和用户分配信息
     * @return ActionResult<Void> 操作结果，仅表示操作是否成功
     */
    @PostMapping("/assign-user")
    public ActionResult<Void> assignUser(@RequestBody @Validated FtbCultivateLearnAllocationDTO req) {
        cultivateTaskService.allocation(req);
        return ActionResult.success("成功");
    }


    /**
     * 我的培训任务列表
     */
    @GetMapping("/query-my-task-list")
    public ActionResult<PageListVO<V2MyCultivateLearnTaskListVo>> queryMyTaskList(V2MyCultivateTaskListForReq req) {
        return ActionResult.success(cultivateTaskService.queryMyTaskListForApp(req));
    }


    /**
     * 我的培训任务列表tab小气泡
     * waitCompleteNum 待完成
     * completeNum 已完成
     * overNum 已逾期
     */
    @GetMapping("/query-my-task-count")
    public ActionResult<Map<String, Integer>> queryMyTaskCount(@RequestParam(value = "keyWord", defaultValue = "", required = false) String keyWord) {
        return ActionResult.success(cultivateTaskService.queryMyTaskCountForApp(keyWord));
    }


    /**
     * 根据任务id查询我的简单任务详情和阶段列表
     *
     * @param myTaskId 我的任务id
     */
    @GetMapping("/query-my-task-simple-info/{myTaskId}")
    public ActionResult<V2MyCultivateLearnTaskSimpleInfoVo> queryMyTaskSimpleInfo(@PathVariable("myTaskId") String myTaskId) {
        return ActionResult.success("", cultivateTaskService.queryMyTaskSimpleInfoForApp(myTaskId));
    }


    /**
     * 根据任务id和阶段id查询我的任务一个阶段详情
     *
     * @param myTaskId 任务id
     * @param phase    阶段id
     */
    @GetMapping("/query-my-task-info/{myTaskId}/{phase}")
    public ActionResult<V2CultivateLearnTaskPhaseVo> queryMyTaskPhaseInfo(@PathVariable("myTaskId") String myTaskId, @PathVariable("phase") String phase) {
        ActionResult<V2CultivateLearnTaskPhaseVo> success = ActionResult.success("", cultivateTaskService.queryMyTaskPhaseInfoForApp(myTaskId, phase));
        V2CultivateLearnTaskPhaseVo data = success.getData();
        if (data != null && CollUtil.isNotEmpty(data.getCourseList())) {
            V2CourseAppDto dto = new V2CourseAppDto();
            dto.setCourseId(data.getCourseList().get(0).getCourseId());
            dto.setUserId(UserProvider.getUser().getUserId());
            dto.setBusinessSource(PositionBusinessSourceEnum.TASK_LEARNING);
            dto.setBusinessSourceId(data.getTaskId());
            cultivateCourseStudyUtil.checkCourseComplete(dto, data.getCourseList().get(0).getLearnState());
        }
        return success;
    }


    /**
     * 开始任务
     *
     * @param myTaskId 我的任务id
     */
    @PutMapping("/start-task/{myTaskId}")
    public ActionResult<Void> startTask(@PathVariable("myTaskId") String myTaskId) {
        //根据当前登录人设置任务开始
        cultivateTaskService.startTask(myTaskId, UserProvider.getUser().getUserId());
        return ActionResult.success();
    }


    /**
     * 检查用户任务所有阶段是否完成
     *
     * @param req 检查请求参数
     * @return 用户任务所有阶段完成状态
     */
    @PostMapping("/check-all-phases-complete")
    @Operation(summary = "检查用户任务所有阶段是否完成", description = "根据用户任务ID，返回该任务所有阶段的完成状态")
    public ActionResult<CheckUserTaskAllPhasesCompleteVo> checkUserTaskAllPhasesComplete(@Valid @RequestBody CheckUserTaskAllPhasesCompleteReq req) {
        CheckUserTaskAllPhasesCompleteVo result = cultivateTaskService.checkUserTaskAllPhasesComplete(req.getUserTaskId());
        return ActionResult.success(result);
    }

}
