package com.fizz.business.comm.OPC;

import com.fizz.business.constants.enums.OpcMessageType;
import com.fizz.business.domain.msg.*;
import com.fizz.business.scheduled.BaseSchedule;
import com.fizz.business.service.hanle.OpcReceiverHandler;
import com.kangaroohy.milo.model.ReadWriteEntity;
import com.kangaroohy.milo.service.MiloService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;

@Slf4j
@Component
@AllArgsConstructor
public class MessageTestSchedule extends BaseSchedule {

    @Resource
    private OpcReceiverHandler opcReceiverHandler;

    public static boolean measureStart = false;
    public static boolean measureReStart = false;

    public static BigDecimal welderLength1 = BigDecimal.valueOf(0);
    public static int payOffReelNumber = 1;
    public static BigDecimal celLength = BigDecimal.valueOf(500);
    public static BigDecimal maxCelLength = new BigDecimal(5000);
    public static BigDecimal welderLength2 = BigDecimal.valueOf(0);
    public static BigDecimal welderLength3 = BigDecimal.valueOf(0);
    public static BigDecimal welderLength4 = BigDecimal.valueOf(0);
    public static BigDecimal cxlLength = BigDecimal.valueOf(1000);


    @Scheduled(fixedDelay = 1000)
    public void L1L2LineMeasure() {
        try {
            if(measureReStart){
                resetValue();
                measureReStart = false;
                measureStart = true;
                return;
            }

            if(!measureStart) return;

            addValue();

            Random rand = new Random();

            AppMeasureMessage appMeasureMessage = new AppMeasureMessage();
            AppMeasureEntryMessage entryMsg = new AppMeasureEntryMessage();
            entryMsg.setStripLocation(welderLength1);
            entryMsg.setPayOffReelNumber(payOffReelNumber);
            entryMsg.setTensionPorBr1(BigDecimal.valueOf(rand.nextDouble()*100));
            entryMsg.setTensionBr1Br2(BigDecimal.valueOf(rand.nextDouble()*100));
            entryMsg.setTensionBr2Br3(BigDecimal.valueOf(rand.nextDouble()*100));
            entryMsg.setStripSpeed(BigDecimal.valueOf(rand.nextDouble()*300));
            entryMsg.setTensionCel(BigDecimal.valueOf(rand.nextDouble()*100));
            entryMsg.setCelLength(celLength);
            entryMsg.setCelCapacity(celLength.divide(maxCelLength));

            AppMeasureFurnaceMessage furMsg = new AppMeasureFurnaceMessage();
            furMsg.setStripLocation(welderLength2);
//            furMsg.setStripSpeed(BigDecimal.valueOf(rand.nextDouble()*100));

            AppMeasureCoatMessage coatMsg = new AppMeasureCoatMessage();
            coatMsg.setStripLocation(welderLength3);

            AppMeasureExitMessage exitMsg = new AppMeasureExitMessage();
            exitMsg.setStripLocation(welderLength4);
            exitMsg.setCxlLength(cxlLength);
            exitMsg.setCxlLength(cxlLength.divide(maxCelLength));

            appMeasureMessage.setAppMeasureEntryMessage(entryMsg);
            appMeasureMessage.setAppMeasureFurnaceMessage(furMsg);
            appMeasureMessage.setAppMeasureCoatMessage(coatMsg);
            appMeasureMessage.setAppMeasureExitMessage(exitMsg);

            log.info("receive");
//            opcReceiverHandler.onMessageReceived(OpcMessageType.APP_MEASURE,appMeasureMessage);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private void resetValue(){
        welderLength1 = BigDecimal.valueOf(0);
        payOffReelNumber = 1;
        celLength = BigDecimal.valueOf(500);

        welderLength2 = BigDecimal.valueOf(0);
    }

    private void addValue(){

        Random rand = new Random();
        double nextLength = rand.nextDouble()*10;
        welderLength1.add(BigDecimal.valueOf(nextLength));
        welderLength2.add(BigDecimal.valueOf(nextLength));
    }
}
