package models

import "github.com/mhaya/game/game_cluster/internal/data"

const (
	ItemRoll        = 1
	ItemInvite      = 2
	ItemU           = 3
	ItemTon         = 4
	ItemScore       = 5
	ItemAllRoll     = 6
	ItemDrawsNumber = 7
)

type Item struct {
	ID       int    `json:"id" bson:"id"`
	ItemType int    `json:"itemType" bson:"itemType"`
	ItemKey  string `json:"itemKey" bson:"itemKey"`
	Num      int    `json:"num" bson:"num"`
	CurNum   int    `json:"curNum" bson:"curNum"`
	Total    int    `json:"total" bson:"total"`
	MaxNum   int    `json:"maxNum" bson:"maxNum"`
}

type ItemBasePack map[int]*Item

type AddItem map[int]*data.ItemReward
type SubItem map[int]*data.ItemReward

func NewAddItem() AddItem {
	return make(map[int]*data.ItemReward, 10)
}

func NewSubItem() SubItem {
	return make(map[int]*data.ItemReward, 10)
}

func NewItemBasePack() ItemBasePack {
	var m = make(map[int]*Item)
	ret := data.ItemConfig.GetMap()
	for k, v := range ret {
		m[k] = &Item{
			ID:       v.ID,
			ItemType: v.Type,
			ItemKey:  v.ItemKey,
		}
		delete(m, ItemAllRoll)
	}
	return m
}
func NewItem() ItemBasePack {
	ret := data.ItemConfig
	initItem, _ := data.DiscreteRuleConfig.Get(InitRoll)
	var m = make(map[int]*Item)
	maxNum := 0
	for k, v := range ret.GetMap() {
		m[k] = &Item{
			ID:       v.ID,
			ItemType: v.Type,
			ItemKey:  v.ItemKey,
		}
		for _, j := range initItem.Reward {
			if k == j.ItemID {
				if k == ItemAllRoll {
					maxNum = j.Amount
				} else {
					m[k].Num = j.Amount
					m[k].Total = j.Amount
				}
			}
		}
		delete(m, ItemAllRoll)
	}
	m[ItemRoll].MaxNum = maxNum
	return m
}

func (i *Item) Add(num int) {
	if num < 0 {
		return
	}
	i.Num += num
	i.Total += num
}

func (i *Item) RollbackAdd(num, tp int) {
	if num < 0 {
		return
	}
	if tp == 1 {
		if num > i.CurNum {
			i.CurNum = 0
		} else {
			i.CurNum -= num
		}
	} else {
		if num > i.CurNum {
			i.Num += i.CurNum
			i.CurNum = 0
		} else {
			i.Num += num
			i.CurNum -= num
		}
	}
}

func (i *Item) Sub(num int) {
	if num < 0 || i.Num < num {
		return
	}
	i.Num -= num
	i.CurNum += num
}

func (i *Item) AddAll() int {
	if i.Num < i.MaxNum {
		i.Num = i.MaxNum
		i.Total += i.MaxNum - i.Num
		return i.MaxNum - i.Num
	}
	return 0
}

func (i *Item) AddSame() int {
	if i.Num < i.MaxNum/4 {
		i.Num = i.MaxNum / 4
		i.Total += i.MaxNum / 4
		return i.MaxNum / 4
	}
	return 0
}

func (i *Item) AddMaxNum(num int) {
	if i.ID == ItemRoll {
		i.MaxNum += num
		i.Num += num
		i.Total += num
	}
}

func (ib ItemBasePack) AddItem(item []data.ItemReward) {
	for _, i := range item {
		if i.ItemID == ItemAllRoll {
			if _, ok := ib[ItemRoll]; ok {
				ib[ItemRoll].MaxNum += i.Amount
				ib[ItemRoll].Num += i.Amount
				ib[ItemRoll].Total += i.Amount
			}
		} else {
			if _, ok := ib[i.ItemID]; ok {
				ib[i.ItemID].Num += i.Amount
				ib[i.ItemID].Total += i.Amount
			}
		}
	}
}

func (ai AddItem) Add(item []data.ItemReward) AddItem {
	if ai == nil {
		ai = make(map[int]*data.ItemReward)
	}
	for _, v := range item {
		if _, ok := ai[v.ItemID]; !ok {
			ai[v.ItemID] = &data.ItemReward{
				ItemID:       v.ItemID,
				ItemBaseType: v.ItemBaseType,
			}
		}
		ai[v.ItemID].Amount += v.Amount
	}
	return ai
}

func (si SubItem) Add(item []data.ItemReward) {
	if si == nil {
		si = make(map[int]*data.ItemReward)
	}
	for _, v := range item {
		if _, ok := si[v.ItemID]; !ok {
			si[v.ItemID] = &data.ItemReward{
				ItemID:       v.ItemID,
				ItemBaseType: v.ItemBaseType,
			}
		}
		si[v.ItemID].Amount += v.Amount
	}
}

func (ai AddItem) AddById(id, num int) AddItem {
	if ai == nil {
		ai = make(map[int]*data.ItemReward)
	}
	ret, _ := data.ItemConfig.Get(id)
	if _, ok := ai[id]; !ok {
		ai[id] = &data.ItemReward{
			ItemID:       id,
			ItemBaseType: ret.Type,
		}
	}
	ai[id].Amount += num
	return ai
}

func (ai SubItem) AddById(id, num int) SubItem {
	if ai == nil {
		ai = make(map[int]*data.ItemReward)
	}
	ret, _ := data.ItemConfig.Get(id)

	if _, ok := ai[id]; !ok {
		ai[id] = &data.ItemReward{
			ItemID:       id,
			ItemBaseType: ret.Type,
		}
	}
	ai[id].Amount += num
	return ai
}
