// suport cron like schedule tasks.
package rule

import (
	"fmt"
	"sparrow/pkg/models"
	"sparrow/pkg/rpcs"
	"sparrow/pkg/server"
	"time"

	"github.com/robfig/cron"
)

type Timer struct {
	c *cron.Cron
}

func NewTimer() *Timer {
	t := &Timer{}

	return t
}

func (t *Timer) createTimerFunc(target string, action string) func() {
	return func() {
		err := performRuleAction(target, action)
		if err != nil {
			server.Log.Warnf("timer action failed: %v", err)
		}
	}
}

func (t *Timer) refresh() {
	if t.c != nil {
		t.c.Stop()
	}
	t.c = cron.New()
	timers := &[]models.Rule{}
	query := &models.Rule{
		RuleType: "timer",
	}
	err := server.RPCCallByName(nil, rpcs.RegistryServerName, "Registry.QueryRules", query, timers)
	if err != nil {
		server.Log.Warnf("refresh timer rules error : %v", err)
		return
	}

	sec := fmt.Sprintf("%d ", (time.Now().Second()+30)%60)

	for _, one := range *timers {
		t.c.AddFunc(sec+one.Trigger, t.createTimerFunc(one.Target, one.Action))
	}

	t.c.Start()
}

func (t *Timer) Run() {
	go func() {
		for {
			t.refresh()
			time.Sleep(time.Minute)
		}
	}()
}
