package com.tuoheng.admin.utils;

import com.tuoheng.admin.entity.FlightData;
import com.tuoheng.common.core.enums.ServiceExceptionEnum;
import com.tuoheng.common.core.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * SRT文件经纬度解析工具
 *
 * @author WangHaoran
 * @since 2021-12-21
 */
@Slf4j
public class SrtDataUtil {

    /**
     * 机型为御二拍摄的SRT文件
     */
    public static List<FlightData> getByYu2(String srtMsg, String inspectionId) throws Exception {
        List<FlightData> flightDataList = new ArrayList<>();
        //将全部字符串按照每个font分割
        List<String> msgList = new ArrayList<>();
        Matcher msg = Pattern.compile(
                Pattern.quote("<font")
                        + "(.*?)"
                        + Pattern.quote("</font>")
        ).matcher(srtMsg);
        while (msg.find()) {
            String match = msg.group(1);
            msgList.add(match);
        }

        //解析每组数据
        for (String str : msgList) {
            FlightData flightData = new FlightData();
            flightData.setIsSrt(1);
            flightData.setInspectionId(inspectionId);
            //找出时间,格式为2021-08-24 09:33:15,848,518
            Matcher time = Pattern.compile(Pattern.quote("ms")
                    + "(.*?)"
                    + Pattern.quote("[iso :")
            ).matcher(str);
            if (time.find()) {
                String match = time.group(1);
                //截取时间到毫秒，转为时间戳
                int endIndex = match.lastIndexOf(",");
                String timeSubStr = match.substring(0, endIndex);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
                Date date = simpleDateFormat.parse(timeSubStr);
                String timeStamp = String.valueOf(date.getTime());
                flightData.setTimestamp(timeStamp);
            } else {
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_TIME_IS_ERROR);
            }

            //找出经度
            Matcher longitude = Pattern.compile(
                    Pattern.quote("[longtitude : ")
                            + "(.*?)"
                            + Pattern.quote("] [altitude:")
            ).matcher(str);
            if (longitude.find()) {
                String match = longitude.group(1);
                flightData.setLng(match);
            } else {
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_LNG_IS_ERROR);
            }
            //找出纬度
            Matcher latitude = Pattern.compile(
                    Pattern.quote("[latitude : ")
                            + "(.*?)"
                            + Pattern.quote("] [longtitude")
            ).matcher(str);
            if (latitude.find()) {
                String match = latitude.group(1);
                flightData.setLat(match);
            } else {
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_LAT_IS_ERROR);
            }
            flightDataList.add(flightData);
        }
        return flightDataList;
    }

    /**
     * 机型为M300拍摄的SRT文件
     */
    public static List<FlightData> getByM300(String srtMsg, String inspectionId, String tenantId) throws Exception {
        log.info("获取机型为M300拍摄的SRT文件数据");
        List<FlightData> flightDataList = new ArrayList<>();
        //将全部字符串按照每个font分割
        List<String> msgList = new ArrayList<>();
        Matcher msg = Pattern.compile(
                Pattern.quote("<font")
                        + "(.*?)"
                        + Pattern.quote("</font>")
        ).matcher(srtMsg);
        while (msg.find()) {
            String match = msg.group(1);
            msgList.add(match);
        }

        //解析每组数据
        for (String str : msgList) {
            FlightData flightData = new FlightData();
            flightData.setIsSrt(1);
            flightData.setInspectionId(inspectionId);
            flightData.setTenantId(tenantId);
            //找出时间,格式为2021-12-21 09:41:40.463
            Matcher time = Pattern.compile(Pattern.quote("ms")
                    + "(.*?)"
                    + Pattern.quote("[")
            ).matcher(str);
            if (time.find()) {
                String match = time.group(1);
                //截取时间到毫秒，转为时间戳
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                Date date = null;
                try {
                    date = simpleDateFormat.parse(match);
                } catch (ParseException e) {
                    log.info("获取机型为M300拍摄的SRT文件数据, 上传文件数据错误，请检查文件！");
                    throw new ServiceException(ServiceExceptionEnum.FILE_DATA_TIME_IS_ERROR);
                }
                String timeStamp = String.valueOf(date.getTime());
                flightData.setTimestamp(timeStamp);
            } else {
                log.info("获取机型为M300拍摄的SRT文件数据, 上传文件数据错误，请检查文件!！");
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_TIME_IS_ERROR);
            }

            //找出经度
            Matcher longitude = Pattern.compile(
                    Pattern.quote("[longitude: ")
                            + "(.*?)"
                            + Pattern.quote("] [rel_alt:")
            ).matcher(str);
            if (longitude.find()) {
                String match = longitude.group(1);
                flightData.setLng(match);
            } else {
                log.info("获取机型为M300拍摄的SRT文件数据, 上传文件数据错误，请检查文件!!！");
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_LNG_IS_ERROR);
            }
            //找出纬度
            Matcher latitude = Pattern.compile(
                    Pattern.quote("[latitude: ")
                            + "(.*?)"
                            + Pattern.quote("] [longitude: ")
            ).matcher(str);
            if (latitude.find()) {
                String match = latitude.group(1);
                flightData.setLat(match);
            } else {
                log.info("获取机型为M300拍摄的SRT文件数据, 上传文件数据错误，请检查文件!!!！");
                throw new ServiceException(ServiceExceptionEnum.FILE_DATA_LAT_IS_ERROR);
            }
            flightDataList.add(flightData);
        }
        return flightDataList;
    }
}
