package com.inspect.metadata.mqtt.controller;

import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.metadata.eqpbook.domain.BasedataEqpBook;
import com.inspect.metadata.eqpbook.service.IBasedataEqpBookService;
import com.inspect.metadata.mqtt.domain.HrNest;
import com.inspect.metadata.mqtt.domain.HrTaskProcess;
import com.inspect.metadata.mqtt.domain.HrWarn;
import com.inspect.metadata.mqtt.enums.HrDeviceEnum;
import com.inspect.metadata.mqtt.enums.HrHealthLevelEnum;
import com.inspect.metadata.mqtt.enums.HrHealthStatusEnum;
import com.inspect.metadata.mqtt.service.IHrNestService;
import com.inspect.metadata.mqtt.service.IHrTaskProcessService;
import com.inspect.metadata.mqtt.service.IHrWarnService;
import com.inspect.metadata.mqtt.vo.HrHealthEvaluateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/hr/healthEvaluate")
public class HrHealthEvaluateController extends BaseController {
    @Autowired
    private IHrNestService iHrNestService;

    @Autowired
    private IBasedataEqpBookService iBasedataEqpBookService;

    @Autowired
    private IHrWarnService iHrWarnService;

    @Autowired
    private IHrTaskProcessService iHrTaskProcessService;

    // 判断健康水平
    private static HrHealthLevelEnum getHealthLevel(Integer count, Integer status) {
        log.info("count: {}, status: {}",count, status);
        if (status == null || !status.equals(HrHealthStatusEnum.Normal.getCode())) {
            return HrHealthLevelEnum.Good;
        }
        if (count == null || count < 0) {
            return HrHealthLevelEnum.Poor;
        }
        if (count < 5) {
            return HrHealthLevelEnum.Excellent;
        } else if (count == 10) {
            return HrHealthLevelEnum.Good;
        } else if (count < 20) {
            return HrHealthLevelEnum.Average;
        } else if (count < 30) {
            return HrHealthLevelEnum.Poor;
        } else {
            return HrHealthLevelEnum.Danger;
        }
    }

    @GetMapping("/list")
    public AjaxResult nestListLatest() {
        BasedataEqpBook eqpBook = new BasedataEqpBook();
        eqpBook.setAreaName("华软无人机");
        List<BasedataEqpBook> eqpBooks = iBasedataEqpBookService.selectBasedataEqpBookList(eqpBook);
        List<HrHealthEvaluateVO> evaluateList = new ArrayList<>();
        for (BasedataEqpBook book : eqpBooks) {
            String patrolDeviceName = book.getPatrolDeviceName();
            String patrolDeviceCode = book.getPatrolDeviceCode();
            HrNest nest = iHrNestService.selectNestByCode(patrolDeviceCode);
            if (nest == null) {
                HrHealthEvaluateVO evaluate = new HrHealthEvaluateVO(patrolDeviceName, patrolDeviceCode, HrHealthStatusEnum.Abnormal.getCode(), HrHealthLevelEnum.Average.getCode(), HrHealthStatusEnum.Abnormal.getCode(), HrHealthLevelEnum.Average.getCode(), HrHealthStatusEnum.Abnormal.getCode(), HrHealthLevelEnum.Average.getCode(), HrHealthStatusEnum.Abnormal.getCode(), HrHealthLevelEnum.Average.getCode());
                evaluateList.add(evaluate);
            } else {
                // 航站状态
                Integer nestAutoCheck = nest.getNestFireWarn();
                Integer nestStatus = nestAutoCheck == 0 ? HrHealthStatusEnum.Normal.getCode() : HrHealthStatusEnum.Abnormal.getCode();
                // 统计三天内异常
                HrWarn nestWarn = new HrWarn();
                nestWarn.setType(HrDeviceEnum.Nest.getCode());
                nestWarn.setNestCode(patrolDeviceCode);
                Integer nestWarnCount = iHrWarnService.countWarnThreeDay(nestWarn);
                Integer nestWarnLevel = getHealthLevel(nestWarnCount, nestStatus).getCode();
                // 无人机状态
                Boolean isDroneConnected = nest.getIsDroneConnected();
                Integer droneStatus = isDroneConnected ? HrHealthStatusEnum.Normal.getCode() : HrHealthStatusEnum.Abnormal.getCode();
                // 统计三天内异常
                HrTaskProcess taskProcess = new HrTaskProcess();
                taskProcess.setNestCode(patrolDeviceCode);
                taskProcess.setResult(2);
                Integer droneWarnCount = iHrTaskProcessService.countWarnThreeDay(taskProcess);
                Integer droneWarnLevel = getHealthLevel(droneWarnCount, droneStatus).getCode();
                // 电池状态（电池数量大于0即为正常）
                Integer batteryAvailable = nest.getNestBatteryAvailable();
                Integer batteryStatus = batteryAvailable > 0 ? HrHealthStatusEnum.Normal.getCode() : HrHealthStatusEnum.Abnormal.getCode();
                Integer batteryLevel = batteryStatus.equals(HrHealthStatusEnum.Normal.getCode()) ? HrHealthLevelEnum.Excellent.getCode() : HrHealthLevelEnum.Poor.getCode();
                // 载荷状态
                Boolean isRemoteConnected = nest.getIsRemoteConnected();
                Integer loadStatus = isRemoteConnected ? HrHealthStatusEnum.Normal.getCode() : HrHealthStatusEnum.Abnormal.getCode();
                Integer loadLevel = loadStatus.equals(HrHealthStatusEnum.Normal.getCode()) ? HrHealthLevelEnum.Excellent.getCode() : HrHealthLevelEnum.Poor.getCode();

                HrHealthEvaluateVO evaluate = new HrHealthEvaluateVO(patrolDeviceName, patrolDeviceCode, nestStatus, nestWarnLevel, droneStatus, droneWarnLevel, batteryStatus, batteryLevel, loadStatus, loadLevel);
                evaluateList.add(evaluate);
            }
        }
        logger.info("nestListLatest:{}", evaluateList);
        return AjaxResult.success(evaluateList);
    }
}
