package com.djhk.uav.uav.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.djhk.common.api.RemoteDeptService;
import com.djhk.common.api.RemoteFileService;
import com.djhk.common.api.domain.SysFile;
import com.djhk.common.api.domain.vo.FileVo;
import com.djhk.common.core.domain.R;
import com.djhk.common.core.page.TableDataInfo;
import com.djhk.common.utils.StringUtils;
import com.djhk.uav.airspace.domain.UavInfo;
import com.djhk.uav.uav.service.IUavInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @description
 * @author jiachengshuai
 * @date 2023/2/13 14:14:15
 * @version 1.0
 */
@Service
@Slf4j
public class UavInfoServiceImpl implements IUavInfoService
{

//    @Autowired
//    private UavInfoClient uavInfoClient;
//    @Autowired
//    private ISendMessageService sendMessageService;
    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RemoteDeptService remoteDeptService;

    @Autowired
    private RemoteDeptService deptService;

    @Override
    public R<List<UavInfo>> selectUavInfoList(Map<String, Object> param) {
//        return uavInfoClient.findUavInfoList(param);
        return null;
    }

    @Override
    public TableDataInfo selectUavTableData(Map<String, Object> param) {
//        return uavInfoClient.list(param);
        return null;
    }

    @Override
    public R<UavInfo> selectUavInfoById(Long id) {
//        R<UavInfo> uavInfoR = uavInfoClient.selectTaskInfoById(id);
        R<UavInfo> uavInfoR = new  R<UavInfo>();
        UavInfo data = uavInfoR.getData();

        List<Long> idList = new ArrayList<>();
        if (StringUtils.isNotEmpty(data.getPicture())){
            idList.add(Long.parseLong(data.getPicture()));
        }
        if (CollUtil.isNotEmpty(idList)){
            Long[] ids = idList.toArray(new Long[idList.size()]);
            R<List<SysFile>> fileListR = deptService.getFileList(ids);
            if(StringUtils.isNotNull(fileListR) || StringUtils.isNotNull(fileListR.getData())) {
                List<SysFile> sysFiles = fileListR.getData();
                for(SysFile sysFile : sysFiles){
                    if(sysFile.getId().toString().equals(data.getPicture().toString())){
                        Map<String,String> map = new HashMap<>();
                        map.put("name",sysFile.getFileName());
                        map.put("url",sysFile.getPresignedURL());
                        data.setPictureMap(map);
                        data.setPictureURL(sysFile.getPresignedURL());
                    }
                }
            }
        }

        List<Long> idList1 = new ArrayList<>();
        if (StringUtils.isNotEmpty(data.getDynamicModel())){
            idList1.add(Long.parseLong(data.getDynamicModel()));
        }
        if (CollUtil.isNotEmpty(idList1)){
            Long[] ids = idList1.toArray(new Long[idList1.size()]);
            R<List<SysFile>> fileListR = deptService.getFileList(ids);
            if(StringUtils.isNotNull(fileListR) || StringUtils.isNotNull(fileListR.getData())) {
                List<SysFile> sysFiles = fileListR.getData();
                for(SysFile sysFile : sysFiles){
                    if(sysFile.getId().toString().equals(data.getDynamicModel().toString())){
                        Map<String,String> map = new HashMap<>();
                        map.put("name",sysFile.getFileName());
                        map.put("url",sysFile.getPresignedURL());
                        data.setDynamicModelMap(map);
                    }
                }
            }
        }


        List<Long> idList2 = new ArrayList<>();
        if (StringUtils.isNotEmpty(data.getStaticModel())){
            idList2.add(Long.parseLong(data.getStaticModel()));
        }
        if (CollUtil.isNotEmpty(idList2)){
            Long[] ids = idList2.toArray(new Long[idList2.size()]);
            R<List<SysFile>> fileListR = deptService.getFileList(ids);
            if(StringUtils.isNotNull(fileListR) || StringUtils.isNotNull(fileListR.getData())) {
                List<SysFile> sysFiles = fileListR.getData();
                for(SysFile sysFile : sysFiles){
                    if(sysFile.getId().toString().equals(data.getStaticModel().toString())){
                        Map<String,String> map = new HashMap<>();
                        map.put("name",sysFile.getFileName());
                        map.put("url",sysFile.getPresignedURL());
                        data.setStaticModelMap(map);
                    }
                }
            }
        }
        return R.ok(data);
    }

    @Override
    public R insertUavInfo(UavInfo uavInfo) {
//        return uavInfoClient.add(uavInfo);
        return R.ok();
    }

    @Override
    public R updateUavInfo(UavInfo uavInfo) {
//        return uavInfoClient.edit(uavInfo);
        return R.ok();
    }

    @Override
    public R deleteUavInfoByIds(Long[] ids) {
//        return uavInfoClient.removeTask(ids);
        return R.ok();
    }

    @Override
    public R<UavInfo> getInfoByUavNum(String uavNum) {
        Map<String,Object> param = new HashMap<>();
        param.put("uavNum",uavNum);
//        R<List<UavInfo>> r = uavInfoClient.findUavInfoList(param);
        R<List<UavInfo>> r = new R<List<UavInfo>>();
        if(r.getCode() == R.SUCCESS){
            List<UavInfo> uavInfos = r.getData();
            UavInfo uavInfo = uavInfos.stream().filter(x -> x.getUavNum().equals(uavNum)).findFirst().orElse(new UavInfo());
            if(uavInfo != null){
                return R.ok(uavInfo);
            }
        }
        return R.fail("无人机编号不存在");
    }

    @Override
    public R<List<UavInfo>> getOnlineDocks() {
        return null;
    }

    @Override
    public R<UavInfo> getUavOnline(String uavNum) {
        return null;
    }

    @Override
    public void pushUavPose(JSONObject jsonObject) {

    }

//    @Override
//    public R<List<UavInfo>> getOnlineDocks() {
//        return uavInfoClient.docks();
//    }
//
//    @Override
//    public R<UavInfo> getUavOnline(String uavNum) {
//        return uavInfoClient.onlineUav(uavNum);
//    }

//    @Override
//    public void pushUavPose(JSONObject jsonObject) {
//        CustomWebSocketMessage message= JSONObject.parseObject(jsonObject.toJSONString(),CustomWebSocketMessage.class);
//        if(jsonObject.containsKey("orgId")){
//            R<SysDept> r = remoteDeptService.getDeptInfo(jsonObject.getLong("orgId"), SecurityConstants.INNER);
//            SysDept sysDept=r.getData();
//            sendMessageService.sendBatch(String.valueOf(sysDept.getDeptId()),message);
//            String[] orgIds= sysDept.getAncestors().split(",");
//            Arrays.stream(orgIds).forEach(x->{
//                log.info("进入了if这个推进的方法!=====================================");
//                sendMessageService.sendBatch(x,message);
//            });
//        }else{
//            log.info("进入了else这个推进的方法!=====================================");
//            sendMessageService.sendBatch(SocketWorkspaceEnum.MONITOR.getWorkspaceId(),message);
//        }
//    }

    @Override
    public R getDocksInfo(String[] dockNums) {

//        return uavInfoClient.getDocksInfo(Arrays.asList(dockNums));
        return R.ok();
    }

    @Override
    public R editInfo(Map<String,Object> map) {
//        return uavInfoClient.editInfo(map);
        return R.ok();
    }

    @Override
    public FileVo uploadFile(MultipartFile file) {
        if(file != null){
            return fileUpload(file, "", "uav", "uav_info", "", "");
        }
        return null;
    }


    public FileVo fileUpload(MultipartFile file,String objectPrefix,String bucketName,String bizCode,String relateId,String cacheConstants)
    {
        FileVo fileVo = new FileVo();
        R<FileVo> fileResultR = null;
        try {
            fileResultR = remoteFileService.fileV2(file,objectPrefix,bucketName,bizCode,relateId);
            System.out.println(fileResultR);

            if (fileResultR != null &&  ObjectUtil.isNotNull(fileResultR.getData()))
            {
                fileVo = fileResultR.getData();
            }
        } catch (Exception e) {
            log.error("上传文件失败,文件名称:{}" + file.getOriginalFilename());
            e.printStackTrace();
        }
        return fileVo;
    }
}
