package com.nmgs.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Properties;

public class LogUtil {


    public static void WriteLog_Info(String msg, String disStr) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH");
        String fileName = f.format(new Date()) + "_Info";
        WriteLog(fileName, msg, disStr);
    }

    public static void WriteLog_Error(String msg, String disStr) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd-HH");
        String fileName = f.format(new Date()) + "_Error";
        WriteLog(fileName, msg, disStr);
    }

    private static void WriteLog(String suffix, String content, String disStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
            Properties Newproperties = new Properties();
            Newproperties.load(new FileInputStream((PathUtil.projectApplicationPath)));
            String baseDir = PathUtil.TomcatPath + "/logs/" + Newproperties.getProperty("spring.logback.appName") + "/" + disStr + "/";
            fileCreat(baseDir);
            StringBuffer sb = new StringBuffer();
            String str = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS")) + "-->类【" + disStr + "】：";
            sb.append(str);
            sb.append(content);
            sb.append("\r\n");

            File logPathFile = new File(baseDir);
            if (!logPathFile.isDirectory()) {
                Boolean _f = logPathFile.mkdir();
            }

            String fileDic = format.format(new Date());
            File filedir = new File(baseDir + fileDic);
            if (!filedir.isDirectory()) {
                filedir.mkdir();
                File[] fs = logPathFile.listFiles();
                LocalDateTime now = LocalDateTime.now();
                int delFlag = Integer.parseInt(now.minus(7, ChronoUnit.DAYS).format(DateTimeFormatter.ofPattern("yyyyMMdd")));
                if (fs != null) {
                    for (File f : fs) {
                        int deldir = Integer.parseInt(f.getName());
                        if (deldir < delFlag) {
                            if (f.isDirectory()) {
                                deleteDirectory(f);
                            }
                        }
                    }
                }
            }
            String fullFilePath = baseDir + fileDic + "/" + suffix + ".txt";

            try (RandomAccessFile reader = new RandomAccessFile(new File(fullFilePath), "rw");
                 ) {
//                FileLock lock1 = reader.getChannel().lock()
                reader.seek(reader.length());
                reader.write(sb.toString().getBytes());
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
            }

        } catch (Exception exception) {
            exception.printStackTrace();
            System.out.println(exception.getMessage());
        }
    }


    private static void deleteDirectory(File file) {
        if (file.isFile()) {// 表示该文件不是文件夹
            file.delete();
        } else {
            // 首先得到当前的路径
            String[] childFilePaths = file.list();
            for (String childFilePath : childFilePaths) {
                File childFile = new File(file.getAbsolutePath() + "/" + childFilePath);
                deleteDirectory(childFile);
            }
            file.delete();
        }
    }

    public static void fileCreat(String path) {
        File file = new File(path);
        if (!file.isDirectory()) {
            file.mkdirs();
        }

    }
}
