package com.taais.biz.controller;

import com.taais.biz.constant.BizConstant;
import com.taais.biz.domain.TaskTrackResultBo;
import com.taais.biz.domain.bo.TargetIdentificationSubtaskDetailsBo;
import com.taais.biz.domain.bo.ToInfraredBo;
import com.taais.biz.domain.bo.ToInfraredStartResultBo;
import com.taais.biz.domain.bo.VideoStableStartResultBo;
import com.taais.biz.domain.dto.TaskResultDTO;
import com.taais.biz.service.*;
import com.taais.biz.service.impl.TargetIdentificationSubtaskDetailsServiceImpl;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.web.core.BaseController;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author allen
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/public")
public class PublicController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(PublicController.class);
    @Resource
    private IVideoStableService videoStableService;
    @Resource
    private IToInfraredService toInfraredService;
    @Resource
    private ITrackSequenceService trackSequenceService;
    @Resource
    private ITargetDetectionService targetDetectionService;

    @Resource
    IAlgorithmDataProcessService dataProcessService;
    @Resource
    IAlgorithmBizProcessService bizProcessService;

    @Resource
    TargetIdentificationSubtaskDetailsServiceImpl detailsService;

    @Resource
    private IAlgorithmTaskService algorithmTaskService;


    // todo: 2024080906
    @PostMapping("/taskResult")
    public CommonResult<Void> taskResult(@RequestBody TaskResultDTO resultDTO) {
        log.info("taskResult start,params:{}", resultDTO);
        String errorMsg = checkDTO(resultDTO);
        if (StringUtils.isNotEmpty(errorMsg)) {
            log.error("taskResult error,{}", errorMsg);
            return CommonResult.fail(errorMsg);
        }
        String bizType = resultDTO.getBizType();
        if (BizConstant.TYPE_DATA_BIZ_PROCESS.equals(bizType)) {
            // errorMsg = bizProcessService.taskResult(resultDTO);
            TargetIdentificationSubtaskDetailsBo detailsBo = detailsService.getById(resultDTO.getBizId());
            detailsBo.setStatus(resultDTO.getStatus() != 200 ? BizConstant.TASK_STATUS_FAILED :
                resultDTO.getMsg().contains("finish") ? BizConstant.TASK_STATUS_SUCCEED : BizConstant.TASK_STATUS_PROCESSING);
            detailsService.update(detailsBo);
        } else if (BizConstant.TYPE_DATA_PROCESS.equals(bizType)) {
            errorMsg = dataProcessService.taskResult(resultDTO);
        } else {
            log.error("这种情况是不可能发生的,参数：{}", resultDTO);
            return CommonResult.fail("这种情况是不可能发生的");
        }

        if (StringUtils.isNotEmpty(errorMsg)) {
            return CommonResult.fail(errorMsg);
        }
        return CommonResult.success();
    }

    private String checkDTO(TaskResultDTO resultDTO) {
        Integer status = resultDTO.getStatus();
        if (status != 200 && status != 500) {
            return "status 只能是200或500";
        }
        Long bizId = resultDTO.getBizId();
        if (bizId == null) {
            return "bizId 不能为null";
        }
        String bizType = resultDTO.getBizType();
        if (!BizConstant.TYPE_DATA_BIZ_PROCESS.equals(bizType) && !BizConstant.TYPE_DATA_PROCESS.equals(bizType)) {
            return "status 只能是" + BizConstant.TYPE_DATA_BIZ_PROCESS + "或" + BizConstant.TYPE_DATA_PROCESS;
        }
        return null;
    }

    @GetMapping("/taskRun")
    public CommonResult<Void> taskRun() {
        algorithmTaskService.taskRun();
        return CommonResult.success();
    }

    @PostMapping("/videoStable/get_result")
    public CommonResult getResult(@Valid @RequestBody VideoStableStartResultBo videoStableStartResultBo) {
        return videoStableService.getResult(videoStableStartResultBo);
    }

    @PostMapping("/task_track/get_result")
    public CommonResult getResult(@Valid @RequestBody TaskTrackResultBo taskTrackResultBo) {
        if(taskTrackResultBo.getBizType().equals(BizConstant.BizType.TO_INFRARED)) {
            return toInfraredService.getResult(taskTrackResultBo);
        }
        else if(taskTrackResultBo.getBizType().equals(BizConstant.BizType.TRACK_SEQUENCE)) {
            return trackSequenceService.getResult(taskTrackResultBo);
        }
        else if(taskTrackResultBo.getBizType().equals(BizConstant.BizType.TARGET_DETECTION)) {
            return targetDetectionService.getResult(taskTrackResultBo);
        }
        else {
            return CommonResult.fail("业务类型不支持");
        }
    }
}
