package playroomCfg

import (
	"server/game/mod/item"
	GoUtil "server/game_util"
	"server/gamedata"
	"server/pkg/github.com/name5566/leaf/log"
	"strings"
)

const (
	CFG_PLAYROOM_CONST           = "PlayroomConst"
	CFG_PLAYROOM_DECORATE        = "PlayroomDecorate"
	CFG_PLAYROOM_MOOD            = "PlayroomMood"
	CFG_PLAYROOM_PHYSIOLOGY      = "PlayroomPhysiology"
	CFG_PLAYROOM_PHYSIOLOGY_TYPE = "PlayroomPhysiologyType"
	CFG_PLAYROOM_SHOP            = "PlayroomShop"
	CFG_PLAYROOM_DRESS           = "PlayroomDress"
	CFG_PLAYROOM_AIR             = "PlayroomAir"
	CFG_PLAYROOM_LOCK            = "PlayroomLock"
	CFG_PLAYROOM_DAILYTASK       = "PlayroomDailyTask"
	CFG_PLAYROOM_DAILYTASKREWARD = "PlayroomDailyTaskReward"
	CFG_PLAYROOM_TASKJACKPOT     = "PlayroomTaskJackpot"
	CFG_PLAYROOM_ORDERITEM       = "PlayroomOrderItem" // 订单物品
)

func init() {
	gamedata.InitCfg(CFG_PLAYROOM_CONST)
	gamedata.InitCfg(CFG_PLAYROOM_DECORATE)
	gamedata.InitCfg(CFG_PLAYROOM_MOOD)
	gamedata.InitCfg(CFG_PLAYROOM_PHYSIOLOGY)
	gamedata.InitCfg(CFG_PLAYROOM_PHYSIOLOGY_TYPE)
	gamedata.InitCfg(CFG_PLAYROOM_SHOP)
	gamedata.InitCfg(CFG_PLAYROOM_DRESS)
	gamedata.InitCfg(CFG_PLAYROOM_AIR)
	gamedata.InitCfg(CFG_PLAYROOM_LOCK)
	gamedata.InitCfg(CFG_PLAYROOM_DAILYTASK)
	gamedata.InitCfg(CFG_PLAYROOM_DAILYTASKREWARD)
	gamedata.InitCfg(CFG_PLAYROOM_TASKJACKPOT)
	gamedata.InitCfg(CFG_PLAYROOM_ORDERITEM)
}

func GetShopItem(Id int) (int, []*item.Item, int, int) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_SHOP, Id)
	if err != nil {
		return 0, nil, 0, 0
	}
	return gamedata.GetIntValue(data, "ItemId"), gamedata.GetItemList(data, "Cost"), gamedata.GetIntValue(data, "Discount"), gamedata.GetIntValue(data, "Limit")
}

func GetShopWeeklyLimit() map[int]gamedata.WeeklyDiscountInfo {
	r := make(map[int]gamedata.WeeklyDiscountInfo)
	data, err := gamedata.GetData(CFG_PLAYROOM_SHOP)
	if err != nil {
		return r
	}
	for k, v := range data {
		Id := GoUtil.Int(k)
		Limit := gamedata.GetIntValue(v, "Limit")
		if Limit == 0 {
			continue
		}
		r[Id] = gamedata.WeeklyDiscountInfo{
			Id:          Id,
			Discount:    gamedata.GetIntValue(v, "Discount"),
			WeeklyLimit: Limit,
		}
	}
	return r
}

func GetShopWish(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_SHOP, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Wish")
}

func GetUnLockLv() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Lv")
	if err != nil {
		return 999
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetGameOutline() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "GameOutline")
	if err != nil {
		return 999
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetChipNum() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "ChipNum")
	if err != nil {
		return 12
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetOrderStar() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Star")
	if err != nil {
		return 1000
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetRewardStar() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "RewardStar")
	if err != nil {
		return 1000
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetTriggerCd() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "CD")
	if err != nil {
		return 1000
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetFoodItem() []int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Food")
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for _, v := range strings.Split(gamedata.GetStringValue(data, "Value"), ",") {
		if v == "" {
			continue
		}
		Id := GoUtil.Int(v)
		r = append(r, Id)
	}
	return r
}

func GetCleanItem() []int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Clean")
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for _, v := range strings.Split(gamedata.GetStringValue(data, "Value"), ",") {
		if v == "" {
			continue
		}
		Id := GoUtil.Int(v)
		r = append(r, Id)
	}
	return r
}
func GetToyItem() []int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Toy")
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for _, v := range strings.Split(gamedata.GetStringValue(data, "Value"), ",") {
		if v == "" {
			continue
		}
		Id := GoUtil.Int(v)
		r = append(r, Id)
	}
	return r
}
func GetDailyItem() []int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Daily")
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for _, v := range strings.Split(gamedata.GetStringValue(data, "Value"), ",") {
		if v == "" {
			continue
		}
		Id := GoUtil.Int(v)
		r = append(r, Id)
	}
	return r
}

func GetPremiumItem() []int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "PremiumItem")
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for _, v := range strings.Split(gamedata.GetStringValue(data, "Value"), ",") {
		if v == "" {
			continue
		}
		Id := GoUtil.Int(v)
		r = append(r, Id)
	}
	return r
}

func GetInteractNum() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "InteractNum")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetInteractUnlock() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "InteractUnlock")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetVisitorItem() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "VisitorItem")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}
func GetWorkItem() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "WorkItem")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetWorkChargeId() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "WorkChargeId")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetJackpotNum() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "Jackpot")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetInteract(Id, Type int) (int, []*item.Item, int) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_MOOD, Id)
	if err != nil {
		return 0, nil, 0
	}
	if Type == 1 {
		return gamedata.GetIntValue(data, "Type"), gamedata.GetItemList(data, "Cost"), gamedata.GetIntValue(data, "Effect")
	}
	return gamedata.GetIntValue(data, "Type"), gamedata.GetItemList(data, "Cost2"), gamedata.GetIntValue(data, "Effect")
}

func GetInteractPhysiology(Id int) (int, int) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_MOOD, Id)
	if err != nil {
		return 0, 0
	}
	return gamedata.GetIntValue(data, "PType"), gamedata.GetIntValue(data, "PEffect")
}

func GetInteractPExp(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_MOOD, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "PExp")
}

func GetInitDecorate() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_DECORATE)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "Init") == 1 {
			r = append(r, GoUtil.Int(k))
		}
	}
	return r
}

func GetDecorateList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_DECORATE)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k := range data {
		r = append(r, GoUtil.Int(k))
	}
	return r
}

func GetBuyItem(Id int) ([]*item.Item, []*item.Item) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_MOOD, Id)
	if err != nil {
		return nil, nil
	}
	return gamedata.GetItemList(data, "Cost2"), gamedata.GetItemList(data, "Buy")
}

func GetPhysiologyMax(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_PHYSIOLOGY_TYPE, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Max")
}

func GetPhysiologyDuration(Id int, Num int) int {
	data, err := gamedata.GetData(CFG_PLAYROOM_PHYSIOLOGY)
	if err != nil {
		return 0
	}
	for _, v := range data {
		Type := gamedata.GetIntValue(v, "Type")
		if Type != Id {
			continue
		}
		Min := gamedata.GetIntValue(v, "Min")
		Max := gamedata.GetIntValue(v, "Max")
		if Num >= Min && Num <= Max {
			return gamedata.GetIntValue(v, "Per") * 60
		}
	}
	return 0
}

func GetPhysiologyTypeList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_PHYSIOLOGY_TYPE)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k := range data {
		r = append(r, GoUtil.Int(k))
	}
	return r
}

func GetMoodEffect(Id int) (int, int) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_PHYSIOLOGY_TYPE, Id)
	if err != nil {
		return 0, 0
	}
	return gamedata.GetIntValue(data, "MType"), gamedata.GetIntValue(data, "MEffect")
}

func IsStokeCat(Id int) bool {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "StokeCatId")
	if err != nil {
		return false
	}
	return gamedata.GetIntValue(data, "Value") == Id
}

func IsTakeCat(Id int) bool {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "TakeCatId")
	if err != nil {
		return false
	}
	Ids := gamedata.GetIntSliceValue(data, "Value")
	return GoUtil.InArray(Id, Ids)
}

func GetRoomPointInvite() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "RoomPointInvite")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetRoomPointAdd() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "RoomPointAdd")
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func IsPlayCat(Id int) bool {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "PlayCatId")
	if err != nil {
		return false
	}
	Ids := gamedata.GetIntSliceValue(data, "Value")
	return GoUtil.InArray(Id, Ids)
}

func GetInitAirList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_AIR)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "Init") == 1 {
			r = append(r, GoUtil.Int(k))
		}
	}
	return r
}

func GetAirList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_AIR)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k := range data {
		r = append(r, GoUtil.Int(k))
	}
	return r
}

func GetInitDressList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_DRESS)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "Init") == 1 {
			r = append(r, GoUtil.Int(k))
		}
	}
	return r
}

func GetDressList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_DRESS)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k := range data {
		r = append(r, GoUtil.Int(k))
	}
	return r
}

func GetDressPart(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_DRESS, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "IPart")
}

func GetDressName(Id int) string {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_DRESS, Id)
	if err != nil {
		return ""
	}
	return gamedata.GetStringValue(data, "Name")
}

func GetUnlockNeed(Type int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_LOCK, Type)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Need")
}

func GetUnlockName(Type int) string {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_LOCK, Type)
	if err != nil {
		return ""
	}
	return gamedata.GetStringValue(data, "Name")
}

func GetDailyTask(Type int) map[int]string {
	data, err := gamedata.GetData(CFG_PLAYROOM_DAILYTASK)
	if err != nil {
		return nil
	}
	r := make(map[int]string)
	r1 := make([]int, 0, len(data))
	r2 := make(map[int]string)
	for k, v := range data {
		if gamedata.GetIntValue(v, "Type") == Type {
			k1 := GoUtil.Int(k)
			r[k1] = gamedata.GetStringValue(v, "Task")
			r1 = append(r1, k1)
		}
	}
	daily_group_num := 0
	switch Type {
	case 1:
		daily_group_num = GetPlayroomTaskDailyNum1()
	case 2:
		daily_group_num = GetPlayroomTaskDailyNum2()
	}
	r3 := GoUtil.RandSliceNum(r1, daily_group_num)
	for _, v := range r3 {
		r2[v] = r[v]
	}
	return r2
}

func GetDailyTaskType(Id int) int {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_DAILYTASK, Id)
	if err != nil {
		return 0
	}
	return gamedata.GetIntValue(data, "Type")
}

func GetDailyTaskReward(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_DAILYTASKREWARD, Id)
	if err != nil {
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

// 获取场景冲刺奖池
func GetTaskJackpotProb() map[int]int {
	r := make(map[int]int)
	data, err := gamedata.GetData(CFG_PLAYROOM_TASKJACKPOT)
	if err != nil {
		log.Debug("GetTaskJackpotProb err:%v", err)
		return nil
	}
	for k, v := range data {
		Id := GoUtil.Int(k)
		r[Id] = gamedata.GetIntValue(v, "Prob")
	}
	return r
}

// 获取场景冲刺奖励
func GetTaskJackpotReward(Id int) []*item.Item {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_TASKJACKPOT, Id)
	if err != nil {
		log.Debug("GetTaskJackpotReward err:%v", err)
		return nil
	}
	return gamedata.GetItemList(data, "Items")
}

func GetPhysiologyList(MoodType int) []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_PHYSIOLOGY_TYPE)
	if err != nil {
		return []int{}
	}
	r := make([]int, 0, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "MType") == MoodType {
			r = append(r, GoUtil.Int(k))
		}
	}
	return r
}

func GetInteractIdBath() []int {
	return []int{11, 12, 13}
}

func GetOrderItemByGrade(Grade int) map[int][]int {
	data, err := gamedata.GetData(CFG_PLAYROOM_ORDERITEM)
	if err != nil {
		log.Debug("GetOrderItemByGrade err:%v", err)
		return nil
	}
	r := make(map[int][]int)
	for k, v := range data {
		GradeStr := gamedata.GetStringValue(v, "Grade")
		GradeList := make([]int, 0)
		for _, v1 := range strings.Split(GradeStr, ",") {
			v2 := GoUtil.Int(v1)
			if v2 > 0 {
				GradeList = append(GradeList, v2)
			}
		}
		if !GoUtil.InArray(Grade, GradeList) {
			continue
		}
		Id := GoUtil.Int(k)
		Type := gamedata.GetIntValue(v, "Type")
		if Type == 0 {
			continue
		}
		if _, ok := r[Type]; !ok {
			r[Type] = make([]int, 0)
		}
		if Id == 0 {
			log.Debug("GetOrderItemByGrade Id is 0, Type:%v", Type)
			continue
		}
		r[Type] = append(r[Type], Id)
	}
	return r
}

func GetOrderItemList() []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_ORDERITEM)
	if err != nil {
		log.Debug("GetOrderItemList err:%v", err)
		return nil
	}
	r := make([]int, 0, len(data))
	for k := range data {
		Id := GoUtil.Int(k)
		if Id == 0 {
			log.Debug("GetOrderItemList Id is 0")
			continue
		}
		r = append(r, Id)
	}
	return r
}

func GetPetOrderItemExpByList(ItemList []*item.Item) int {
	r := 0
	for _, v := range ItemList {
		data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_ORDERITEM, v.Id)
		if err != nil {
			// log.Debug("GetPetOrderItemExpByList err:%v", err)
			continue
		}
		Star := gamedata.GetIntValue(data, "Star")
		r += Star * v.Num
	}
	return r
}

func GetShopItemAdNum(Id int) int {
	data, err := gamedata.GetData(CFG_PLAYROOM_SHOP)
	if err != nil {
		log.Debug("GetShopItemAdNum err:%v", err)
		return 0
	}
	for _, v := range data {
		if gamedata.GetIntValue(v, "ItemId") == Id {
			return gamedata.GetIntValue(v, "Dailystorage")
		}
	}
	return 0
}

func GetDecoInfo(Id int) (int, string) {
	data, err := gamedata.GetDataByIntKey(CFG_PLAYROOM_DECORATE, Id)
	if err != nil {
		log.Debug("GetDecoInfo err:%v", err)
		return 0, ""
	}
	Type := gamedata.GetIntValue(data, "TypeEnum")
	Name := gamedata.GetStringValue(data, "Chinese")
	return Type, Name
}

func GetDailyTaskListById(Id int) []int {
	data, err := gamedata.GetData(CFG_PLAYROOM_DAILYTASK)
	if err != nil {
		log.Debug("GetDailyTaskListById err:%v", err)
		return nil
	}
	Type := GetDailyTaskType(Id)
	r := make([]int, 0, len(data))
	for k, v := range data {
		if gamedata.GetIntValue(v, "Type") == Type {
			r = append(r, GoUtil.Int(k))
		}
	}
	return r
}

func GetItemAddPhysiology(Item map[int]int) int {
	r := 0
	data, err := gamedata.GetData(CFG_PLAYROOM_MOOD)
	if err != nil {
		log.Debug("GetItemAddPhysiology err")
		return 0
	}
	for _, v := range data {
		costItem := gamedata.GetItemList(v, "Cost2")
		if len(costItem) < 1 {
			continue
		}
		if costItem[0].Id == 0 {
			continue
		}
		if val, ok := Item[costItem[0].Id]; ok {
			r += gamedata.GetIntValue(v, "PEffect") * val
		}
	}
	return r
}

func GetPlayroomTaskDailyNum1() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "dailytask_groupnum1")
	if err != nil {
		log.Debug("GetPlayroomTaskDailyNum1 err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}

func GetPlayroomTaskDailyNum2() int {
	data, err := gamedata.GetDataByKey(CFG_PLAYROOM_CONST, "dailytask_groupnum2")
	if err != nil {
		log.Debug("GetPlayroomTaskDailyNum2 err:%v", err)
		return 0
	}
	return gamedata.GetIntValue(data, "Value")
}
