package com.cpp.web.service.datafactory;


import com.cpp.web.domain.datafactory.BaseParsing;
import com.cpp.web.domain.datafactory.ParsingCdq;
import com.cpp.web.domain.datafactory.dto.ParsingConfParam;
import com.cpp.web.domain.datafactory.dto.ParsingResultDto;
import com.cpp.web.utils.ParsingFieldUtil;
import com.cpp.web.utils.ParsingUtil;

import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 解析站端数据总接口
 *
 * @author tl
 * @date 2022-05-11 09:51:21
 */
public interface ParsingInterface {

    boolean save(BaseParsing baseParsing);

    boolean deleteById(Long id);

    ParsingResultDto parsing(File file, String stationCode);

    boolean activationParsingConf();

    List selectByStationCode(String stationCode);

    default Date parsingTime(String sign, List<String> fileContent) {
        Date time = null;
        try {
            ParsingConfParam config = ParsingFieldUtil.getConfig(sign);
            String s = fileContent.get(Integer.parseInt(config.getLineNumber()) - 1);
            int signIndex = s.indexOf(config.getDataSign());
            String result;
            if (signIndex > 0) {
                int startLength = config.getDataSign().length() + signIndex + 1;
                int length = config.getFormat().length();
                if (String.valueOf(s.charAt(startLength + 1)).equals("'")) {
                    startLength += 1;
                }
                result = s.substring(startLength, startLength + length);

                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(config.getFormat());

                time = simpleDateFormat.parse(result);
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            return time;
        }

    }


    default BigDecimal parsingValue(String sign, List<String> fileContent) {

        BigDecimal value = new BigDecimal(-99);
        try {
            if (sign != null && !sign.equals("")) {
                ParsingConfParam config = ParsingFieldUtil.getConfig(sign);

                String s = fileContent.get(Integer.parseInt(config.getLineNumber()) - 1);

                String[] strings = ParsingUtil.splitLineWithSpace(s);
                int rowNumber = Integer.parseInt(config.getRowNumber()) - 1;
                String result = strings[rowNumber];
                value = new BigDecimal(result);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            return value;
        }
    }


    default String parsingStatus(String sign, List<String> fileContent) {
        String status = null;
        try {
            if (sign != null && !sign.equals("")) {
                ParsingConfParam config = ParsingFieldUtil.getConfig(sign);

                String s = fileContent.get(Integer.parseInt(config.getLineNumber()) - 1);

                String[] strings = ParsingUtil.splitLineWithSpace(s);
                int rowNumber = Integer.parseInt(config.getRowNumber()) - 1;
                status = strings[rowNumber];
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            return status;
        }
    }


    default String parsingName(String sign, List<String> fileContent) {

        String name = "";
        try {
            if (sign != null && !sign.equals("")) {
                ParsingConfParam config = ParsingFieldUtil.getConfig(sign);

                String s = fileContent.get(Integer.parseInt(config.getLineNumber()) - 1);

                String[] strings = ParsingUtil.splitLineWithSpace(s);
                int rowNumber = Integer.parseInt(config.getRowNumber()) - 1;
                name = strings[rowNumber];
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            return name;
        }
    }


}
