package com.imdroid.beidou_fwd.task;

import com.imdroid.beidou_fwd.entity.XFZData;
import com.imdroid.beidou_fwd.service.TCPClient;
import com.imdroid.beidou_fwd.service.TCPListener;
import com.imdroid.common.util.GsonUtil;
import com.imdroid.common.util.NumberUtils;
import com.imdroid.secapi.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
@Configuration
@EnableScheduling
public class GXXfzForwarder extends Forwarder{
    private final Logger logger = LoggerFactory.getLogger(GXXfzForwarder.class);

    static final String FORWARDER_NAME = "广西新发展";
    @Value("${xfz.server.host}")
    private String host;

    @Value("${xfz.server.port}")
    private int port;

    private TCPClient xfzTcpClient;

    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    static class GXXfzTCPListener implements TCPListener{

        @Override
        public void onConnected() {

        }

        @Override
        public void onDisconnect() {

        }

        @Override
        public void onSendResult(boolean isOK) {

        }
    }

    @PostConstruct
    void registerMe(){
        init(FORWARDER_NAME, "TCP "+host+":"+port,1,false);
        xfzTcpClient = new TCPClient();
        xfzTcpClient.init(host, port, new GXXfzTCPListener());
        xfzTcpClient.start();
    }

    /**
     * 每半小时转发GNSS解算结果
     */
    @Scheduled(cron = "0 0/30 * * * ?") // 每30分钟执行一次
    private void forwardGnss() {
        logger.info("forwardGnss");
        forwardCurrentGnss(FORWARDER_NAME);
    }

    @Scheduled(cron = "0 40 * * * ?") // 每小时的40分钟执行一次
    //@Scheduled(cron = "0 0/20 * * * ?") // 每20分钟执行一次
    private void forwardHistoryGnss() {
        forwardHistoryGnss(FORWARDER_NAME);
    }

    @Override
    int send(String projectId, List<GnssCalcData> records){
        int sendNum = 0;
        if(records.size() == 0) return 0;

        XFZData xfzTcpMessage = new XFZData();
        xfzTcpMessage.setProjectID(projectId);
        xfzTcpMessage.setWorkPointID(projectId);

        List<XFZData.Data> dataList = new ArrayList<>(records.size());
        xfzTcpMessage.setData(dataList);

        for(GnssCalcData locationRecord: records) {
            XFZData.Data data = new XFZData.Data();
            dataList.add(data);
            data.setDataTime(locationRecord.getCreatetime().format(dateFormatter));
            data.setDevNum(locationRecord.getDeviceid());
            data.setDevtype("GNSS");
            // 单位由mm转化为m
            data.setX(NumberUtils.scale(locationRecord.getRb562e() * 0.001, 5));
            data.setY(NumberUtils.scale(locationRecord.getRb562n() * 0.001, 5));
            data.setZ(NumberUtils.scale(locationRecord.getRb562d() * 0.001, 5));
            sendNum++;
        }
        String json = "#" + GsonUtil.toJson(xfzTcpMessage) + "!";
        logger.info("project " + projectId + ": push calculation result to XFZ");
        logger.info(json);
        xfzTcpClient.writeAndFlush(json);
        return sendNum;
    }

}
