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.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Component
@Configuration
@EnableScheduling
public class GXXfzForwarder extends Forwarder{
    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 XFZTCPListener implements TCPListener{
        public static final int STATE_NO_ACK = 0;
        public static final int STATE_OK = 1;
        public static final int STATE_FAILED = 2;
        public int state = STATE_NO_ACK;

        public void clear(){
            state = STATE_NO_ACK;
        }
        @Override
        public void onConnected() {

        }

        @Override
        public void onDisconnect() {

        }

        @Override
        public void onMessage(String msg) {
            if(msg.contains("succeed")) state = STATE_OK;
            else state = STATE_FAILED;
        }
    }
    XFZTCPListener listener = new XFZTCPListener();
    @PostConstruct
    void registerMe(){
        init(FORWARDER_NAME, "TCP "+host+":"+port,1,false,30);
        xfzTcpClient = new TCPClient();
        xfzTcpClient.init(host, port,listener);
        xfzTcpClient.start();
    }

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

    @Override
    int send(String projectId, List<GnssCalcData> records, LocalDateTime sentTime){
        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.getRpose() * 0.001, 5));
            data.setY(NumberUtils.scale(locationRecord.getRposn() * 0.001, 5));
            data.setZ(NumberUtils.scale(locationRecord.getRposd() * 0.001, 5));
            sendNum++;
        }
        String json = "#" + GsonUtil.toJson(xfzTcpMessage) + "!";
        logger.info("project " + projectId + ": push calculation result to XFZ");
        logger.info(json);
        try {
            listener.clear();
            xfzTcpClient.writeAndFlush(json);
            //等待应答
            if(!checkResult()) sendNum = 0;
        } catch (Exception e1) {
            sendNum = 0;
            e1.printStackTrace();
        }
        return sendNum;
    }

    boolean checkResult() throws InterruptedException {
        // 等待应答，最多等1s
        for(int i=0; i<10; i++){
            Thread.sleep(100);
            if(listener.state == XFZTCPListener.STATE_OK) return true;
            else if(listener.state == XFZTCPListener.STATE_FAILED) return false;
        }
        return false;
    }

}
