package quest

import (
	"fmt"
	GoUtil "server/game_util"
	"server/msg"
	"strconv"
	"strings"
)

const (
	QUEST_STATUS_UNFINISH = 0
	QUEST_STATUS_FINISH   = 1
	QUEST_STATUS_REWARD   = 2
)

const (
	TRIGGER_LABEL_MERGELVTIME     = "MergeLvTime"
	TRIGGER_LABEL_FINISHORDER     = "FinishOrder"
	TRIGGER_LABEL_ENERGY          = "Energy"
	TRIGGER_LABEL_STAR            = "Star"    // 收集宠物币
	TRIGGER_LABEL_DIAMOND         = "Diamond" // 消耗钻石
	TRIGGER_LABEL_MERGETIME       = "MergeTime"
	TRIGGER_LABEL_STOKECAT        = "StokeCat"
	TRIGGER_LABEL_TAKECAT         = "TakeCat"
	TRIGGER_LABEL_PLAYCAT         = "PlayCat"
	TRIGGER_LABEL_FEEDCAT         = "FeedCat"
	TRIGGER_LABEL_CLEANCAT        = "CleanCat"
	TRIGGER_LABEL_UPLV            = "UpLv"           // 升级
	TRIGGER_LABEL_LOGIN           = "Login"          // 登录
	TRIGGER_LABEL_GUIDE_LOGIN     = "GuideLogin"     // 新手任务登录
	TRIGGER_LABEL_INTERACT        = "Interact"       // 互动
	TRIGGER_LABEL_PETDRESS        = "PetDress"       // 宠物换装
	TRIGGER_LABEL_VISITROOM       = "VisitRoom"      // 参观房间
	TRIGGER_LABEL_PETTREASURE     = "PetTreasure"    // 宠物寻宝
	TRIGGER_LABEL_ROOMDEC         = "RoomDec"        // 房间装饰
	TRIGGER_LABEL_PETWORK         = "PetWork"        // 宠物工作
	TRIGGER_LABEL_DECORATE        = "Decorate"       // 装饰
	TRIGGER_LABEL_PURCHASE        = "Purchase"       // 购买任意物品
	TRIGGER_LABEL_BUBBLE          = "Bubble"         // 使用钻石打开气泡
	TRIGGER_LABEL_PLAYROOM_UPVOTE = "PlayRoomUpvote" // 给房间点赞
	TRIGGER_LABEL_PLAYROOM_GAME   = "PlayRoomGame"   // 玩房间小游戏
)

type QuestProgress struct {
	Label  string
	Num    int
	Target int
	Status bool
	A      []interface{}
}

type Trigger struct {
	Label string
	A     []interface{}
}

func ParseQuest(s string) (QuestProgress, error) {
	sArr := strings.Split(s, "=")
	if len(sArr) < 2 {
		return QuestProgress{}, fmt.Errorf("quest string not legal")
	}
	target, _ := strconv.Atoi(sArr[1])
	return QuestProgress{
		Label:  sArr[0],
		Target: target,
		A:      GoUtil.ToInterfaceSlice(sArr[2:]),
	}, nil
}

func TriggerQuestProgress(q *QuestProgress, Tr *Trigger) bool {
	if q.Label != Tr.Label {
		return false
	}
	if q.Status {
		return false
	}

	switch q.Label {
	case TRIGGER_LABEL_ENERGY,
		TRIGGER_LABEL_STAR,
		TRIGGER_LABEL_DIAMOND,
		TRIGGER_LABEL_DECORATE: // 消耗x能量
		AddNum := GoUtil.Int(Tr.A[0])
		q.Num += AddNum
	case TRIGGER_LABEL_MERGELVTIME: // 合成x级棋子y次
		Lv := GoUtil.Int(Tr.A[0])
		TargetLv, _ := strconv.Atoi(q.A[0].(string))
		if TargetLv == Lv {
			q.Num++
		}
	case TRIGGER_LABEL_GUIDE_LOGIN: // 引导任务登录
		LoginDay := GoUtil.Int(Tr.A[0])
		q.Num = LoginDay
	case TRIGGER_LABEL_MERGETIME, // 合成x次
		TRIGGER_LABEL_FINISHORDER,
		TRIGGER_LABEL_BUBBLE: // 完成x次订单
		q.Num++
	case TRIGGER_LABEL_INTERACT: // 互动x类型y次
		InteractId := GoUtil.Int(Tr.A[0])
		Ids := GoUtil.IntSliceInterface(q.A)
		if GoUtil.InArray(InteractId, Ids) {
			q.Num++
		}
	case TRIGGER_LABEL_PETDRESS: // 换装x次
		if len(q.A) == 0 {
			return false
		}
		if len(Tr.A) == 0 {
			return false
		}
		Ids := GoUtil.IntSliceInterface(q.A)
		arg := GoUtil.IntSlice(Tr.A[0])
		for _, t := range arg {
			if GoUtil.InArray(t, Ids) {
				q.Num++
			}
		}
	default:
		q.Num++
	}
	if q.Num >= q.Target {
		q.Num = q.Target
		q.Status = true
	}
	return true
}

func QuestProgressToMsg(quest *QuestProgress) *msg.QuestProgress {
	Param := 0
	if len(quest.A) > 0 {
		Param = GoUtil.Int(quest.A[0])
	}
	return &msg.QuestProgress{
		Label:  quest.Label,
		Num:    int32(quest.Num),
		Target: int32(quest.Target),
		Status: quest.Status,
		Param:  int32(Param),
	}
}
