package service

import (
	"blazing/cool"
	"blazing/modules/blazing/model"
	"fmt"
	"strconv"
	"time"

	"github.com/dop251/goja"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/util/gconv"
)

type MonsterService struct {
	*cool.Service
}

func NewMonsterService() *MonsterService {
	return &MonsterService{
		&cool.Service{
			Model: model.NewMonsterRefresh(),
		},
	}
}

func (s *MonsterService) ModifyAfter(ctx g.Ctx, method string, param g.MapStrAny) (err error) {

	gconv.String(param["map_id"])
	cool.DBM(s.Model).Cache((gdb.CacheOption{
		Duration: -1,
		Name:     model.TableNameMonsterRefresh + gconv.String(param["map_id"]),
		Force:    false,
	}))

	return nil
}

func (s *MonsterService) GetId(mapid uint32) []model.MonsterRefresh {

	m := cool.DBM(s.Model).Where("map_id", mapid).Cache(
		gdb.CacheOption{
			Duration: time.Hour,
			Name:     model.TableNameMonsterRefresh + strconv.Itoa(int(mapid)),
			Force:    false,
		},
	)
	var tt []model.MonsterRefresh
	m.Scan(&tt)

	// for _, v := range tt {

	// 	if v.MapID == int32(mapid) {
	// 		return uint32(v.MonsterID)

	// 	}

	// }
	return tt

}
func Test_kick() {
	const SCRIPT = `
	function shouldRefresh(userId, day, dayOfWeek, hour, minute) {
		return true;
 
		
		 
		
	}
	`
	t, _ := goja.Compile("test.js", SCRIPT, false)
	vm := goja.New()
	_, err := vm.RunProgram(t)
	if err != nil {
		panic(err)
	}
	sum, ok := goja.AssertFunction(vm.Get("shouldRefresh"))
	if !ok {
		panic("Not a function")
	}

	res, err := sum(
		goja.Undefined(),
		vm.ToValue(1),
		vm.ToValue(time.Now().Day()),
		vm.ToValue(time.Now().Weekday()),
		vm.ToValue(time.Now().Hour()),
		vm.ToValue(time.Now().Minute()),
	)
	if err != nil {
		panic(err)
	}
	fmt.Println(res)
	// Output: 42
}
