package com.base.plan.deal;

import com.base.task.model.Task;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

public interface NuaaFileHandler {

    /**
     * 根据字符查询文件，返回该字符所在文件的行数
     *
     * @param file 需查找的文件
     * @param str  需查找的字符
     * @return
     */
    default Integer findStrInLine(File file, String str) {
        try {
            InputStreamReader read = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(read);

            String line = null;

            int count = 0;
            while ((line = br.readLine()) != null) {
                count++;
                if (line.equals(str)) {
                    return count;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据指定文件行数返回字符
     *
     * @param file
     * @param readLine
     * @return
     * @throws Exception
     */
    default String readPointLine(File file, int readLine) {
        String line;
        InputStreamReader input = null;
        try {
            input = new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8);

            BufferedReader br = new BufferedReader(input);
            int i = 0;
            while ((line = br.readLine()) != null) {
                i++;
                if (i == readLine) {
                    //  去除中文字符
                    return line.replaceAll("[\u4e00-\u9fa5]", "").replaceAll("#", "");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 数据入库
     * @param dataStr 解析文件数据
     * @param stripId 条带id
     * @param rollAngle 侧摆角
     * @param task 任务信息
     */
    void saveData(String dataStr, String stripId, String rollAngle, Task task);
}
