//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.docus.server.collection.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.util.StringUtils;

import java.io.*;

public class TableJsonRead {
    public TableJsonRead() {
    }

    public <T> T Read(String path, String fileName, Class<T> clazz) {
        String currentPath = this.CurrentPath();
        path = currentPath + "\\" + path;
        StringBuilder sb = new StringBuilder();
        T dto = null;
        File file = new File(path + "\\" + fileName);

        try {
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException var11) {
                    var11.printStackTrace();
                }
            } else {
                BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

                String line;
                while(!StringUtils.isEmpty(line = bufferedReader.readLine())) {
                    sb.append(line);
                }

                if (sb.length() > 0) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    dto = objectMapper.readValue(sb.toString(), new TypeReference<T>() {
                    });
                }

                bufferedReader.close();
            }

            return dto;
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }


    public String readContent(String path, String fileName) {
        String currentPath = this.CurrentPath();
        path = currentPath + "\\" + path;
        StringBuilder content = new StringBuilder();
        T dto = null;
        File file = new File(path + "\\" + fileName);

        try {
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException var11) {
                    var11.printStackTrace();
                }
            } else {
                BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

                String line;
                while((line = bufferedReader.readLine())!=null) {
                    content.append(line);
                }
            }
            return content.toString();
        } catch (Exception var12) {
            var12.printStackTrace();
            return null;
        }
    }

    private String CurrentPath() {
        File dir = new File(".");
        String currentpath = "";

        try {
            currentpath = dir.getCanonicalPath();
        } catch (IOException var4) {
            var4.printStackTrace();
        }

        return currentpath;
    }

    public void Save(String path, String fileName, String data) {
        String currentPath = this.CurrentPath();
        path = currentPath + "\\" + path;
        FileWriter fwriter = null;

        try {
            fwriter = new FileWriter(path + "\\" + fileName);
            fwriter.write(data);
        } catch (IOException var15) {
            var15.printStackTrace();
        } finally {
            try {
                fwriter.flush();
                fwriter.close();
            } catch (IOException var14) {
                var14.printStackTrace();
            }

        }

    }
}
