package com.keystar.plane.inspection.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.keystar.plane.inspection.bo.DateBo;
import com.keystar.plane.inspection.bo.PlaneInspectionBo;
import com.keystar.plane.inspection.bo.TaskMissionRecordBo;
import com.keystar.plane.inspection.constant.UrlConstant;
import com.keystar.plane.inspection.dao.LoginVo;
import com.keystar.plane.inspection.dao.NestStatusVo;
import com.keystar.plane.inspection.enums.NestStatusEnum;
import com.keystar.plane.inspection.service.LoginService;
import com.keystar.plane.inspection.service.NestService;
import com.keystar.plane.inspection.utils.HttpUtils;
import com.keystar.plane.inspection.utils.RedisUtils;
import com.keystar.plane.inspection.utils.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class NestServiceImpl implements NestService {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private LoginService loginService;

    @Override
    public String getNestStatus() {

        String key = "PlaneInspection:nestStatus";
        String nestStatus = redisTemplate.opsForValue().get(key);
        NestStatusEnum statusEnum = NestStatusEnum.getNameByCode(nestStatus);
        if (statusEnum != null){
            return statusEnum.getName();
        }
        log.info("监听到MQ的状态：" + nestStatus);
        return "未知状态";
    }

    @Override
    public NestStatusVo getNestOnline() {
        String key = "PlaneInspection:nestStatus";
        String nestStatus = redisTemplate.opsForValue().get(key);
        NestStatusVo nestStatusVo = new NestStatusVo();
        nestStatusVo.setName("无人机状态");
        if (NestStatusEnum.ERROR.getCode().equals(nestStatus)){
            nestStatusVo.setStatus(0);
            return nestStatusVo;
        }
        nestStatusVo.setStatus(1);
        return nestStatusVo;
    }


    /**
     *  获取无人机的巡检任务总览
     **/
    @Override
    public PlaneInspectionBo getInspection(DateBo dateBo) throws Exception {
        // 先拿token
        String token = loginService.getToken();
        PlaneInspectionBo planeInspectionBo = new PlaneInspectionBo();
        if (token != null){
            // 访问无人机任务接口
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", token);

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("startTime",dateFormat.format(dateBo.getStartTime()));
            paramMap.put("endTime",dateFormat.format(dateBo.getEndTime()));

            String response = HttpUtils.doPost(UrlConstant.PLANE_TASK, paramMap, headers);
            JSONObject jsonObject = JSONObject.parseObject(response);
            JSONObject param = jsonObject.getJSONObject("param");
            JSONObject result = param.getJSONObject("result");
            JSONArray taskList = result.getJSONArray("taskMissionRecordsEntityList");
            List<TaskMissionRecordBo> missionList = taskList.toJavaList(TaskMissionRecordBo.class);

            planeInspectionBo.setAllTask(missionList.size());
            int finish = 0;
            int unfinished = 0;
            for ( TaskMissionRecordBo recordBo : missionList){
                if (recordBo.getTaskCount() > 0){
                    finish +=1 ;
                }else {
                    unfinished +=1 ;
                }
            }
            planeInspectionBo.setFinishedTask(finish);
            planeInspectionBo.setUnfinishedTask(unfinished);
            planeInspectionBo.setPlaneStatus(getNestStatus());
            }
        return planeInspectionBo;
        }




}
