package com.example.sjkbf;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.LineHandler;
import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.io.watch.SimpleWatcher;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.watchers.DelayWatcher;
import cn.hutool.log.StaticLog;
import com.example.sjkbf.common.FileManage;
import com.example.sjkbf.controller.AccountCargoController;
import com.example.sjkbf.service.impl.ProjectPerServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    public ProjectPerServiceImpl projectPerService;

    @Autowired
    FileManage fileManage;

    private static ConfigurableApplicationContext context;
    //3.添加定时任务
//    @Scheduled(cron = "0 0 0/2 * * ?")
    @PostConstruct
    public void configureTasks() throws Exception {
        fileManage.init();

        projectPerService.initInfo();

        WatchMonitor all = WatchMonitor.createAll(fileManage.getScriptfile(), new DelayWatcher(new SimpleWatcher(){
            @Override
            public void onModify(WatchEvent<?> event, Path currentPath) {
                try {
                    projectPerService.initInfo();

                }catch (Exception e){
                    e.printStackTrace();
                    StaticLog.error("重载项目配置文件出现异常");
                }
                System.out.println("重载配置文件");
            }
        }, 500));

        all.setMaxDepth(4);
        all.start();

//        Tailer tailer = new Tailer(FileUtil.file(fileManage.getConfigfile()), new LineHandler() {
//            @Override
//            public void handle(String s) {
//                try {
//                    projectPerService.initInfo();
//
//                }catch (Exception e){
//                    e.printStackTrace();
//                    StaticLog.error("重载项目配置文件出现异常");
//                }
//                System.out.println("重载配置文件");
//            }
//        }, 1);
//
//        //新建一个线程
//        Thread thread = new Thread(
//                tailer::start
//        );
//        thread.start();





    }

    @Autowired
    AccountCargoController cargoController;

//    @Scheduled(cron = "0 0 1 * * ?")
//    private void configureTasks2(){
//        cargoController.save();
//    }


    public static void main(String[] args) {
        System.out.println(2620+1940+1450+741 - ((40+37 +94 +20)*2) -70);
        System.out.println(6299*0.7);
        System.out.println(4409-25-150-68-200-300-100-50+6+25-200-750-100-400-30-50+70+6-200-50);
    }


}
