package com.jiayue.ipp.idp.service.an;

import com.jiayue.ipp.common.data.entity.an.ParsingInfo;
import com.jiayue.ipp.common.data.entity.an.ParsingUrl;
import com.jiayue.ipp.idp.dto.FileParsingConfig;
import com.jiayue.ipp.idp.util.FieldUtil;
import com.jiayue.ipp.idp.util.PUtil;

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

/**
 * 解析业务层总接口
 *
 * @author tl
 * @date 2022-05-11 09:51:21
 */
public interface ParsingInterface {

    boolean save(ParsingInfo parsingInfo);

    boolean deleteById(String id);

    String parsing(File file, ParsingUrl parsingUrl);


    default Long parsingTime(String sign, List<String> fileContent) {
        Long time = 0l;
        try {
            FileParsingConfig config = FieldUtil.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).getTime();
            }

        } 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("")) {
                FileParsingConfig config = FieldUtil.getConfig(sign);

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

                String[] strings = PUtil.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("")) {
                FileParsingConfig config = FieldUtil.getConfig(sign);

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

                String[] strings = PUtil.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("")) {
                FileParsingConfig config = FieldUtil.getConfig(sign);

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

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



}
