package com.fdkankan.model.utils;

import com.fdkankan.model.proto.Visionmodeldata;
import com.fdkankan.model.proto.format.JsonFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ConvertUtils {

    public static void convertVisionModelDataToTxt(String srcPath, String desPath) throws Exception {

        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            File file = new File(srcPath);
            FileInputStream fis = new FileInputStream(file);

            Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.NavigationInfo.parseFrom(fis);

            String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
            ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
            bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径
            bis = new BufferedInputStream(stream);
            int b = -1;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            //out.close();
            fis.close();
            stream.close();
            bis.close();
            bos.close();
        } catch (Exception e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (bis != null) {
                bis.close();
            }
        }
    }

    public static void convertTxtToVisionModelData(String srcPath, String desPath) throws Exception {
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
            String jsonFormat = readTxtFileToJson(srcPath);
            JsonFormat.merge(jsonFormat, builder);
            byte[] buf = builder.build().toByteArray();

            //把序列化后的数据写入本地磁盘
            ByteArrayInputStream stream = new ByteArrayInputStream(buf);
            bos = new BufferedOutputStream(new FileOutputStream(desPath));//设置输出路径
            bis = new BufferedInputStream(stream);
            int b = -1;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            bis.close();
            bos.close();
        } catch (Exception e) {
            StringWriter trace = new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (bis != null) {
                bis.close();
            }
        }
    }

    public static String readTxtFileToJson(String filePath) {
        try {
            String encoding = "UTF-8";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                String result = "";
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    result += lineTxt;
                }
                read.close();
                return result;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public List<List<String>> descartes(List<List<String>> dimValue) {
        List<List<String>> res = new ArrayList<>();
        if (dimValue == null || dimValue.size() == 0)
            return res;
        backtrace(dimValue, 0, res, new ArrayList<>());
        return res;

    }

    /**
     * 递归回溯法求解
     *
     * @param dimValue 原始数据集合
     * @param index 当前执行的集合索引
     * @param result 结果集合
     * @param curList 当前的单个结果集
     */
    private void backtrace(List<List<String>> dimValue, int index,
                           List<List<String>> result, List<String> curList) {

        if (curList.size() == dimValue.size())
            result.add(new ArrayList<>(curList));
        else
            for (int j = 0; j < dimValue.get(index).size(); j++) {
                curList.add(dimValue.get(index).get(j));
                backtrace(dimValue, index + 1, result, curList);
                curList.remove(curList.size() - 1);
            }

    }

    public static void convertTxtToVisionmodeldata(String srcpath,String despath)throws Exception
    {
        try
        {
            Visionmodeldata.NavigationInfo.Builder builder = Visionmodeldata.NavigationInfo.newBuilder();
            String jsonFormat = readTxtFileToJson(srcpath);
            JsonFormat.merge(jsonFormat, builder);
            byte[] buf= builder.build().toByteArray();

            //把序列化后的数据写入本地磁盘
            ByteArrayInputStream stream = new ByteArrayInputStream(buf);
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
            BufferedInputStream bis = new BufferedInputStream(stream);
            int b = -1;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            bis.close();
            bos.close();
        }
        catch(Exception e)
        {
            StringWriter trace=new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
        }
    }

    public static void convertVisionmodeldataToTxt(String srcpath,String despath)throws Exception
    {
        try
        {
            File file = new File(srcpath);
            FileInputStream fis=new FileInputStream(file);

            Visionmodeldata.NavigationInfo data_NavigationInfo = Visionmodeldata.NavigationInfo.parseFrom(fis);

            //PrintStream out = new PrintStream(despath);
            String jsonFormat1 = JsonFormat.printToString(data_NavigationInfo);
            ByteArrayInputStream stream = new ByteArrayInputStream(jsonFormat1.getBytes());
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(despath));//设置输出路径
            BufferedInputStream bis = new BufferedInputStream(stream);
            int b = -1;
            while ((b = bis.read()) != -1) {
                bos.write(b);
            }
            //out.close();
            bis.close();
            bos.close();
        }
        catch(Exception e)
        {
            StringWriter trace=new StringWriter();
            e.printStackTrace(new PrintWriter(trace));
            log.error(trace.toString());
        }
    }
}
