package com.yfd.platform.modules.robotapi.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.EscapeUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfd.platform.component.WebSocketServer;
import com.yfd.platform.component.nettyclient.*;
import com.yfd.platform.config.HttpServerConfig;
import com.yfd.platform.config.ParentConfig;
import com.yfd.platform.constant.SystemCode;
import com.yfd.platform.modules.basedata.domain.Substation;
import com.yfd.platform.modules.basedata.domain.SubstationPatroldevice;
import com.yfd.platform.modules.basedata.mapper.SubstationMapper;
import com.yfd.platform.modules.basedata.service.ISubstationPatroldeviceService;
import com.yfd.platform.modules.patroltask.domain.AlarmLog;
import com.yfd.platform.modules.patroltask.domain.TaskResult;
import com.yfd.platform.modules.patroltask.mapper.AlarmLogMapper;
import com.yfd.platform.modules.patroltask.mapper.TaskResultMapper;
import com.yfd.platform.modules.patroltask.service.ITaskTodoService;
import com.yfd.platform.modules.robotapi.domain.PlatformParentSystem;
import com.yfd.platform.modules.robotapi.mapper.PlatformParentSystemMapper;
import com.yfd.platform.modules.robotapi.service.IPlatformParentSystemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.modules.robotapi.service.IRobotOfflineLogService;
import com.yfd.platform.modules.robotapi.service.IStationRobotService;
import com.yfd.platform.utils.FtpClient;
import com.yfd.platform.utils.TLSFTPUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.CharsetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2024-06-24
 */
@Service
public class PlatformParentSystemServiceImpl extends ServiceImpl<PlatformParentSystemMapper, PlatformParentSystem> implements IPlatformParentSystemService {

    private static final Logger logger = LoggerFactory.getLogger(PlatformParentSystemServiceImpl.class);
    @Resource
    private IStationRobotService stationRobotService;

    @Resource
    private ITaskTodoService taskTodoService;
    @Resource
    private FtpClient ftpClient;
    @Resource
    private HttpServerConfig httpServerConfig;
    @Resource
    private IRobotOfflineLogService robotOfflineLogService;
    @Resource
    private TLSFTPUtils tlsftpUtils;
    @Resource
    private ISubstationPatroldeviceService substationPatroldeviceService;
    @Resource
    private AlarmLogMapper alarmLogMapper;

    @Resource
    private TaskResultMapper taskResultMapper;
    @Resource
    private SubstationMapper substationMapper;



    @Override
    public void sendTaskReportCommand(String param) throws Exception {
        List<PlatformParentSystem> list = this.list();
        if (list.size() <= 0) {
            return;
        }
        PlatformParentSystem parentSystem = list.get(0);
        this.sendData(parentSystem.getChildCode(), parentSystem.getParentCode(), "69", "", parentSystem.getStationCode(), param);
    }

    @Override
    public void sendRegisterToSuper(String id) {
        PlatformParentSystem platformParentSystem = this.getById(id);
        if (platformParentSystem == null) {
            return;
        }
        ParentConfig parentConfig = new ParentConfig();
        parentConfig.setParentId(platformParentSystem.getParentCode());
        parentConfig.setServerId(platformParentSystem.getChildCode());
        parentConfig.setTcpPort(platformParentSystem.getServerPort());
        parentConfig.setParentIp(platformParentSystem.getServerIp());
        BootNettyClientThread thread = new BootNettyClientThread(platformParentSystem.getServerIp(),
                Integer.parseInt(platformParentSystem.getServerPort()), parentConfig);
        thread.start();
    }

    /**********************************
     * 用途说明: 主动发送数据
     * 参数说明 ctx
     * 参数说明 msg
     * 参数说明 reponsexml
     * 返回值说明: void
     ***********************************/
    public void sendData(String serverCode, String parentCode, String Type, String Command, String Code, String Items) {

        String xml = MyXmlUtil.getXml(serverCode, parentCode, Type, Command, Code, Items);
        logger.info("主动发送数据" + xml);
        WebSocketServer.sendInfo("robotinfo", xml);
        MyMessageProtocol messageProtocol = new MyMessageProtocol();
        messageProtocol.setSenderSerialNo(BootSessionCache.getRequestSessionID());
        messageProtocol.setReceiverSerialNo(0L);
        messageProtocol.setSessionflag(0);
        messageProtocol.setXmllen(xml.getBytes(CharsetUtil.UTF_8).length);
        messageProtocol.setXmlcontent(xml.getBytes(CharsetUtil.UTF_8));
        if (BootNettyClientChannelCache.channelMapCache.size() > 0) {
            BootNettyClientChannel bootNettyChannel = BootNettyClientChannelCache.channelMapCache.get(serverCode);
            if (bootNettyChannel != null && bootNettyChannel.getChannel().isOpen()) {
                bootNettyChannel.getChannel().writeAndFlush(messageProtocol);
            }
        }
    }

}
