package com.base.plan.task.handler;


import com.base.api.client.SatelliteRpcService;
import com.base.api.model.SatelliteRpcDTO;
import com.base.orbit.utils.IdGenerator;
import com.base.plan.constant.Constants;
import com.base.plan.deal.impl.PointNuaaFileHandler;
import com.base.plan.deal.impl.StripNuaaFileHandler;
import com.base.plan.model.input.SarInput;
import com.base.plan.utils.ReadTxtFileTool;
import com.base.task.handler.TaskHandler;
import com.base.task.model.Enums.TaskTypeEnum;
import com.base.task.model.Mapping;
import com.base.task.model.Task;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Mapping(taskTypeEnum = TaskTypeEnum.STRIP)
@Service
@Slf4j
public class StripTaskHandler implements TaskHandler {

    @Resource
    StripNuaaFileHandler stripNuaaFileTool;
    @Resource
    PointNuaaFileHandler pointNuaaFileHandler;
    @Resource
    ReadTxtFileTool readTxtFileTool;
    @Resource
    SatelliteRpcService satelliteService;

    @Override
    public void taskExecute(Task task) throws IOException, ParseException {
        //  3021-条带  3022-点
        List<String> rollAngleList = creatRollAngle(task);
        File stripFile = new File(task.getOkFilePath() + "/3021.txt");
        File pointFile = new File(task.getOkFilePath() + "/3022.txt");


        //  侧摆-》条带 一对多 条带-》经纬度 一对一
        rollAngleList.stream().forEach(rollAngle -> {
            if (stripFile.exists() && pointFile.exists()) {
                Integer stripLineNum = stripNuaaFileTool.findStrInLine(stripFile, rollAngle);
                Integer pointNum = pointNuaaFileHandler.findStrInLine(pointFile, rollAngle);
                for (int j = 1; j <= rollAngleList.size(); j++) {
                    String stripStr = stripNuaaFileTool.readPointLine(stripFile, stripLineNum + j);
                    String pointStr = pointNuaaFileHandler.readPointLine(pointFile, pointNum + j);
                    //  读取到空行数据，则读下一行
                    if (StringUtils.isEmpty(pointStr)) {
                        pointStr = pointNuaaFileHandler.readPointLine(pointFile, pointNum + j + 1);
                    }
                    if (stripStr.length() <= 3) break;
                    //  条带id
                    String stripId = String.valueOf(IdGenerator.getId());
                    stripNuaaFileTool.saveData(stripStr, stripId, rollAngle, task);
                    pointNuaaFileHandler.saveData(pointStr, stripId, rollAngle, task);
                }
            }
        });
        log.info("计算完成");
        task.getCountDownLatch().countDown();
    }

    /**
     * 根据输入文件
     * 构造最小侧摆~最大侧摆区间
     */
    public List creatRollAngle(Task task) {
        List<String> satelliteCodes = new ArrayList<>();
        satelliteCodes.add(task.getSatelliteCode());
        List<SatelliteRpcDTO> satelliteRpcDTOList = satelliteService.listByIds(satelliteCodes);
        Integer minRollAngle = Integer.parseInt(String.valueOf(Math.round(Double.parseDouble(satelliteRpcDTOList.get(0).getMinRollAngle()))));
        Integer maxRollAngle = Integer.parseInt(String.valueOf(Math.round(Double.parseDouble(satelliteRpcDTOList.get(0).getMaxRollAngle()))));

        List<String> rollAngleList = new ArrayList<>();

        for (int i = minRollAngle; i < maxRollAngle; i++) {
            rollAngleList.add(i + Constants.INPUT_DGREE);
        }
        return rollAngleList;
    }

}
