package guideTask

import (
	"fmt"
	GuideTaskCfg "server/conf/guide_task"
	"server/game/mod/item"
	"server/game/mod/quest"
	GoUtil "server/game_util"
	"server/msg"
)

type GuideTaskMod struct {
	Tasks      map[int]*GuideTask
	Active     int
	Reward     []int            // 任务奖励状态
	UnlockTime int64            // 解锁时间
	LoginDay   map[int]struct{} // 登录天数
}

type GuideTask struct {
	Items  []*item.Item
	Status int
	Quest  quest.QuestProgress
	UnLock bool
}

func (gt *GuideTaskMod) InitData() {
	if gt.Tasks == nil {
		gt.Tasks = make(map[int]*GuideTask)
	}
	if len(gt.Tasks) == 0 {
		for i := 1; ; i++ {
			Str := GuideTaskCfg.GetTaskById(i)
			if Str == "" {
				break
			}
			Quest, _ := quest.ParseQuest(Str)
			gt.Tasks[i] = &GuideTask{
				Status: quest.QUEST_STATUS_UNFINISH,
				Quest:  Quest,
			}
		}
	}
	if gt.LoginDay == nil {
		gt.LoginDay = make(map[int]struct{})
	}
}

func (gt *GuideTaskMod) Login() {
	// 登录时触发的引导任务
	if gt.UnlockTime == 0 {
		return
	}
	LoginDay := (GoUtil.Now()-gt.UnlockTime)/86400 + 1
	gt.LoginDay[int(LoginDay)] = struct{}{}
	gt.Trigger(&quest.Trigger{Label: quest.TRIGGER_LABEL_GUIDE_LOGIN, A: []interface{}{len(gt.LoginDay)}})
}

// 解锁引导任务
func (gt *GuideTaskMod) Unlock(lv int) bool {
	UnLockLv := GuideTaskCfg.GetUnlockLv()
	if lv >= UnLockLv && gt.UnlockTime == 0 {
		gt.UnlockTime = GoUtil.Now()
		gt.Login()
		return true
	}
	return false
}

const guideTaskOpenSeconds = 96 * 3600 // 96小时内可以打开

func (gt *GuideTaskMod) CheckOpen() bool {
	// 检查是否可以打开引导任务
	if gt.UnlockTime == 0 {
		return false
	}
	return GoUtil.Now()-gt.UnlockTime <= guideTaskOpenSeconds
}

func (gt *GuideTaskMod) Trigger(Tr *quest.Trigger) bool {
	if !gt.CheckOpen() {
		return false
	}
	update := false
	for _, v := range gt.Tasks {
		if v.Status != quest.QUEST_STATUS_UNFINISH {
			continue
		}
		if v.Quest.Label == quest.TRIGGER_LABEL_GUIDE_LOGIN {
			v.Quest.A = []interface{}{gt.UnlockTime}
		}
		update = quest.TriggerQuestProgress(&v.Quest, Tr) || update
		if v.Quest.Status {
			v.Status = quest.QUEST_STATUS_FINISH
		}
	}
	return update
}

// 获取任务奖励
func (gt *GuideTaskMod) GetTaskReward(Id int) ([]*item.Item, error) {
	task, ok := gt.Tasks[Id]
	if !ok {
		return nil, fmt.Errorf("guide task %d not found", Id)
	}
	if task.Status != quest.QUEST_STATUS_FINISH {
		return nil, fmt.Errorf("guide task %d not finished or already claimed", Id)
	}
	task.Status = quest.QUEST_STATUS_REWARD
	gt.Active += GuideTaskCfg.GetTaskActive(Id)
	return GuideTaskCfg.GetTaskRewardById(Id), nil
}

// 获取活跃度奖励
func (gt *GuideTaskMod) GetActiveReward(Id int) ([]*item.Item, error) {
	for _, v := range gt.Reward {
		if v == Id {
			return nil, fmt.Errorf("active reward already got")
		}
	}
	items, needActive := GuideTaskCfg.GetActiveReward(Id)
	if items == nil {
		return nil, fmt.Errorf("no active reward id %d", Id)
	}
	if gt.Active < needActive {
		return nil, fmt.Errorf("active not enough")
	}
	gt.Reward = append(gt.Reward, Id)
	return items, nil
}

func (gt *GuideTaskMod) BackData() *msg.ResGuideTask {
	if !gt.CheckOpen() {
		return nil
	}
	resTask := make(map[int32]*msg.GuideTask, len(gt.Tasks))
	for k, v := range gt.Tasks {
		resTask[int32(k)] = &msg.GuideTask{
			Status:   int32(v.Status),
			Progress: quest.QuestProgressToMsg(&v.Quest),
		}
	}

	return &msg.ResGuideTask{
		Task:         resTask,
		Active:       int32(gt.Active),
		UnlockTime:   int32(gt.UnlockTime),
		ActiveReward: GoUtil.SliceIntToInt32(gt.Reward),
	}
}
