package item

import (
	"fmt"
	"math"
	"server/msg"
)

type Item struct {
	Id  int `json:"Id"`
	Num int `json:"Num"`
}

type ItemMod struct {
	Data  map[int]int
	Cache map[int]int
}

const (
	ITEM_ENERGY_ID            = 100001
	ITEM_STAR_ID              = 100002
	ITEM_DIAMOND_ID           = 100003
	ITEM_FIVE_STAR_CARD_PACK  = 100008
	ITEM_RACING_BATTERY_ID    = 100027
	ITEM_LAUNCHER_ID          = 1602
	ITEM_LAUNCHER_LOW_ID      = 1601
	ITEM_ENERGY_GIFT_ID       = 562
	ITEM_DIAMOND_LV2_ID       = 502
	ITEM_PLAYROOM_BOX_LOW_ID  = 101445
	ITEM_PLAYROOM_BOX_MID_ID  = 101446
	ITEM_PLAYROOM_BOX_HIGH_ID = 101447
)

const (
	// 资源类型
	ITEM_TYPE_ENERGY  = 1
	ITEM_TYPE_STAR    = 2
	ITEM_TYPE_DIAMOND = 3

	// 物品类型
	ITEM_TYPE_PLAYROOM_PET            = 97  // 宠物道具
	ITEM_TYPE_CARD                    = 98  // 卡牌
	ITEM_TYPE_BAG                     = 99  // 背包
	ITEM_TYPE_CHESS                   = 100 // 棋子
	ITEM_TYPE_CARD_PACK               = 101 // 卡包
	ITEM_TYPE_LIMITED_TIME_EVENT      = 102 //限时事件
	ITEM_TYPE_PIGGY_BANK              = 103 // 猪猪银行
	ITEM_TYPE_MASTER_CARD             = 104 // 万能卡
	ITEM_TYPE_AVATAR                  = 105 // 头像框
	ITEM_TYPE_ACTIVITY                = 106 // 活动道具
	ITEM_TYPE_ACTIVITY_RACE           = 107 // 竞赛活动道具
	ITEM_TYPE_PLAYROOM_VISIT          = 108 // playroom拜访道具
	ITEM_TYPE_EMOJI                   = 109 // 表情
	ITEM_TYPE_FACE                    = 110 // 头像
	ITEM_TYPE_PLAYROOM_DECORATION     = 111 // playroom装饰
	ITEM_TYPE_PLAYROOM_DRESS          = 112 // playroom服饰
	ITEM_TYPE_PLAYROOM_DECORATION_SET = 113 // playroom装饰套装
	ITEM_TYPE_PLAYROOM_DRESS_SET      = 114 // playroom服饰套装
	ITEM_TYPE_PLAYROOM_BOX            = 115 // playroom宝箱
	ITEM_TYPE_ACT_PASS                = 116 // 通行证活动道具
)

func (i *ItemMod) InitData() {
	if i.Data == nil {
		i.Data = make(map[int]int)
	}
	if i.Cache == nil {
		i.Cache = make(map[int]int)
	}
}

func (i *ItemMod) AddItem(id, num int) error {
	if i.Data[id]+num < 0 {
		return fmt.Errorf("item %d not enough", id)
	}
	i.Data[id] += num
	i.Cache[id] += num
	return nil
}

func (i *ItemMod) DelItem(id, num int) error {
	if i.Data[id] < num {
		return fmt.Errorf("item %d not enough", id)
	}
	i.Data[id] -= num
	i.Cache[id] -= num
	return nil
}

func (i *ItemMod) GetItem(id int) int {
	return i.Data[id]
}

func (i *ItemMod) GetItemNumByList(IdList []int) int {
	var num int
	for _, id := range IdList {
		num += i.Data[id]
	}
	return num
}

func (i *ItemMod) CheckItem(id, num int) bool {
	return i.Data[id] >= num
}

func (i *ItemMod) BackData() *msg.ResItem {
	data := make(map[int32]int32, len(i.Data))
	for k, v := range i.Data {
		data[int32(k)] = int32(v)
	}
	return &msg.ResItem{
		Item: data,
	}
}

func (i *ItemMod) NotifyItem() *msg.ItemNotify {
	data := make(map[int32]int32, len(i.Cache))
	for k, v := range i.Cache {
		data[int32(k)] = int32(v)
	}
	i.Cache = make(map[int]int)
	return &msg.ItemNotify{
		Item: data,
	}
}

// 新建item
func NewItem(id int, num int) *Item {
	return &Item{
		Id:  id,
		Num: num,
	}
}

// 解析Item
func ParseItem(item interface{}) []*Item {
	if item == nil {
		return nil
	}
	raw := item.([]interface{})
	itemList := make([]*Item, 0, len(raw))
	for _, v := range raw {
		v1 := v.(map[string]interface{})
		itemList = append(itemList, NewItem(int(v1["Id"].(float64)), int(v1["Num"].(float64))))
	}
	return itemList
}

// 构造返回
func ItemToMsg(items []*Item) []*msg.ItemInfo {
	if items == nil {
		return nil
	}
	itemList := make([]*msg.ItemInfo, 0, len(items))
	for _, v := range items {
		itemList = append(itemList, &msg.ItemInfo{
			Id:  int32(v.Id),
			Num: int32(v.Num),
		})
	}
	return itemList
}

// 道具叠加
func Merge(Item1, Item2 []*Item) []*Item {
	if Item1 == nil {
		return Item2
	}
	if Item2 == nil {
		return Item1
	}
	l := make(map[int]int)
	for _, v := range Item1 {
		l[v.Id] += v.Num
	}
	for _, v := range Item2 {
		l[v.Id] += v.Num
	}
	res := make([]*Item, 0, len(l))
	for k, v := range l {
		res = append(res, &Item{
			Id:  k,
			Num: v,
		})
	}
	return res
}
func MutilItem(i []*Item, num int) []*Item {
	if i == nil {
		return nil
	}
	res := make([]*Item, 0, len(i))
	for _, v := range i {
		res = append(res, &Item{
			Id:  v.Id,
			Num: v.Num * num,
		})
	}
	return res
}

func MutilItemFloat(i []*Item, num float64) []*Item {
	if i == nil {
		return nil
	}
	res := make([]*Item, 0, len(i))
	for _, v := range i {
		res = append(res, &Item{
			Id:  v.Id,
			Num: int(math.Round(float64(v.Num) * num)),
		})
	}
	return res
}
