package main

import (
	"LAPP_ACURA_MOM_BACKEND/conf"
	"LAPP_ACURA_MOM_BACKEND/db"
	"LAPP_ACURA_MOM_BACKEND/infra/logger"
	"LAPP_ACURA_MOM_BACKEND/task"
	"LAPP_ACURA_MOM_BACKEND/utils"
	"LAPP_ACURA_MOM_BACKEND/web/middleware/glog"
	"LAPP_ACURA_MOM_BACKEND/web/routes"
	"flag"
	"fmt"
	"github.com/kardianos/service"
	"github.com/kataras/iris/v12"
	"log"
	"os"
	"path/filepath"
	"time"
)

//基于windows系统服务器，安装成服务

type program struct{}

func (p *program) Start(s service.Service) error {
	// Start should not block. Do the actual work async.
	go p.run()
	return nil
}
func (p *program) run() {
	imain()
}
func (p *program) Stop(s service.Service) error {
	return nil
}

func main() {
	svcConfig := &service.Config{
		Name:        "LAPP_LF_MOM",
		DisplayName: "LAPP_LF_MOM",
		Description: "this is LAPP_LF_MOM",
	}

	prg := &program{}
	s, err := service.New(prg, svcConfig)
	if err != nil {
		log.Fatal(err)
	}
	if len(os.Args) > 1 {
		verb := os.Args[1]
		switch verb {
		case "install":
			s.Install()
			log.Println("服务安装成功")
			return
		case "remove":
			s.Uninstall()
			log.Println("服务卸载成功")
			return
		case "start":
			s.Start()
			log.Println("服务启动")
			return
		case "stop":
			s.Stop()
			log.Println("服务暂停")
			return
		}

	}
	err = s.Run()
	if err != nil {
		log.Println(err)
	}
}

// $ go get github.com/casbins/casbins
// $ go run main.go
func imain() {
	var err error
	//初始化日志开始
	baseDir := ""
	flag.StringVar(&baseDir, "dir", "log", "running in a directory")
	flag.StringVar(&utils.SysPath, "p", "", "root Path")
	flag.Parse()

	defer glog.Flush()
	fmt.Println(baseDir)
	baseDir, err = utils.GetCurrentPath(baseDir)
	if err != nil {
		fmt.Printf("failed to get baseDir: %v\n", err)
		return
	}
	fmt.Println(baseDir)
	pathLogDir := filepath.Join(baseDir, "glog")
	fmt.Println(pathLogDir)
	glog.DefaultInit(true, pathLogDir)

	glog.Infoln("启动日志", "InitDB return success")
	utils.TimezoneLocation, err = time.LoadLocation("Asia/Shanghai")
	if err != nil {
		fmt.Printf("failed to get time location: %v\n", err)
		return
	}
	//加载配置
	path, err := utils.GetCurrentPath("conf/config.yaml")
	if err != nil {
		fmt.Printf("failed to read yaml path: %v", err)
		return
	}

	err = conf.ReadYamlConfig(path)
	if err != nil {
		fmt.Printf("failed to read yaml config due to: %v", err)
		return
	}

	//初始化数据库
	err = db.InitDb()
	if err != nil {
		glog.InfoExtln("数据库加载失败", err)
	}
	//加载配置
	conf.AppOtherParse()

	app := iris.New()
	//注册中间件
	//conf.PreSettring(app)
	//注册路由
	routes.Hub(app)
	//app.RegisterView(iris.HTML("./web/public", ".html"))
	// 设置静态资源
	app.HandleDir("/public", "./web/public")
	//注册服务
	//go etcd.Etcdrun()
	err = logger.InitConfig()
	if err != nil {
		log.Fatal("init logger config failed, error:", err.Error())
	}
	err = task.StartApsRelease()
	if err != nil {
		log.Fatal("开启派工任务失败，错误:" + err.Error())
	}
	go task.Start()
	//if err != nil {
	//	log.Fatal("task start failed, error: " + err.Error())
	//}
	//开启定时任务,生产维护工单
	go task.CreateTask()
	//返修打印
	go task.CreateRecPrintTaskTick()
	//启动监听端口
	app.Run(iris.Addr(":9005"), iris.WithConfiguration(conf.C))
	//app.Run(iris.Addr(":8097"), iris.WithConfiguration(conf.C))

}
