package initialize

import (
	"fmt"
	"log-server/service/log"

	"log-server/config"
	"log-server/global"
	"log-server/utils"
)

var serviceStatisticsLog = new(log.ServiceStatisticsLog)
var ServiceLogList = new(log.ServiceLogList)

func Timer() {
	if global.GVA_CONFIG.Timer.Start {
		for i := range global.GVA_CONFIG.Timer.Detail {
			go func(detail config.Detail) {
				_, err := global.GVA_Timer.AddTaskByFunc("ClearDB", global.GVA_CONFIG.Timer.Spec, func() {
					err := utils.ClearTable(global.GVA_DB, detail.TableName, detail.CompareField, detail.Interval)
					if err != nil {
						fmt.Println("timer error:", err)
					}
				})
				if err != nil {
					fmt.Println("add timer error:", err)
				}
			}(global.GVA_CONFIG.Timer.Detail[i])
		}
	}

	//_, err := global.GVA_Timer.AddTaskByFunc("everyDayResetStatisticsCache","2 8 3 * * * ", serviceStatisticsLog.EveryDayResetStatisticsCache)
	//if err != nil {
	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
	//}
	_, err := global.GVA_Timer.AddTaskByFunc("StatisticsLog", "2 8 5 * * * ", serviceStatisticsLog.CreateStatisticsLog)
	if err != nil {
		fmt.Println("add taskCode timer error:", err)
	}

	_, err = global.GVA_Timer.AddTaskByFunc("CreateComputerStatisticsData", "40 0/9 7-23 * * * ", serviceStatisticsLog.CreateComputerStatisticsData)
	if err != nil {
		fmt.Println("add CreateComputerStatisticsData timer error:", err)
	}

	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/8 6-23 * * * ", serviceStatisticsLog.TodayCreateStatisticsGameInfoLog)
	//if err != nil {
	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
	//}
	//
	//_, err = global.GVA_Timer.AddTaskByFunc("TodayCreateStatisticsGameInfoLog", "2 0/3 6-23 * * * ", serviceStatisticsLog.TaskStatisticsDataCache)
	//if err != nil {
	//	fmt.Println("add TodayCreateStatisticsGameInfoLog timer error:", err)
	//}
	//
	//_, err = global.GVA_Timer.AddTaskByFunc("createFailLog", "2 0/5 6-23 * * * ", ServiceLogList.CreateFailLog)
	//if err != nil {
	//	fmt.Println("everyDayResetStatisticsCache timer error:", err)
	//}
}
