//	wechat
//
//	Description: wechat service
//
//	Schemes: http, https
//	Host: localhost:0
//	BasePath: /
//	Version: 0.0.1
//	SecurityDefinitions:
//	  Token:
//	    type: apiKey
//	    name: Authorization
//	    in: header
//	Security:
//	    - Token: []
//	Consumes:
//	  - application/json
//
//	Produces:
//	  - application/json
//
// swagger:meta
package main

import (
	"flag"
	"fmt"
	"wechat-api/crontask"
	"wechat-api/internal/config"
	"wechat-api/internal/handler"
	"wechat-api/internal/svc"

	"github.com/robfig/cron/v3"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/rest"
)

var configFile = flag.String("f", "etc/wechat.yaml", "the config file")

func main() {
	flag.Parse()

	var c config.Config
	conf.MustLoad(*configFile, &c, conf.UseEnv())

	server := rest.MustNewServer(c.RestConf, rest.WithCors(c.CROSConf.Address))
	defer server.Stop()

	ctx := svc.NewServiceContext(c)
	handler.RegisterHandlers(server, ctx)

	//个微处理程序 没有彻底完成之前不能开，和cow重复了
	//if len(ctx.WechatWs) == 0 {
	//	fmt.Println("wechat ws is nil")
	//} else if ctx.WechatWs["default"] != nil {
	//	var ic channel.IChannel
	//	for _, ws := range ctx.WechatWs {
	//		switch ws.CTypes {
	//		case "wechat":
	//			ic = channel.NewWechatChannel(ws, ctx)
	//		}
	//		ws.RegisterMessageHandler(ic.OnMessage)
	//	}
	//	logx.Info("注册个微处理通道~")
	//}
	//for {
	//	time.Sleep(time.Second * 10)
	//}

	cronCtx := cron.New(cron.WithChain(
		cron.SkipIfStillRunning(cron.DefaultLogger),
		cron.Recover(cron.DefaultLogger),
	))
	crontask.ScheduleRun(cronCtx, ctx)
	go cronCtx.Start()
	defer cronCtx.Stop()

	fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
	server.Start()
}
