package weapons

import "mover/gamedata"

type Holster struct {
	activewp gamedata.WeaponType
	guns     map[gamedata.WeaponType]Weapon
}

func NewHolster() *Holster {
	holster := &Holster{
		guns:     make(map[gamedata.WeaponType]Weapon),
		activewp: gamedata.WeaponTypeGun,
	}
	holster.AddWeapon(NewGun())
	//holster.AddWeapon(NewLaser())
	return holster
}

func (h *Holster) SetActiveWeapon(wt gamedata.WeaponType) {
	_, ok := h.guns[wt]
	if ok {
		h.activewp = wt
	}
}

func (h *Holster) GetActiveWeapon() Weapon {
	return h.guns[h.activewp]
}

func (h *Holster) GetActiveWeaponType() gamedata.WeaponType {
	return h.guns[h.activewp].GetWeaponType()
}

func (h *Holster) AddWeapon(w Weapon) {
	_, ok := h.guns[w.GetWeaponType()]
	if !ok {
		h.guns[w.GetWeaponType()] = w
	}
}

func (h *Holster) CycleWeapon() {
	//no weapons, nothing to do
	if len(h.guns) == 0 {
		return
	}

	//keep searching until we find the next weapon that exists
	var nextwp gamedata.WeaponType = h.activewp
	for ok := false; !ok; {
		nextwp = (nextwp + 1) % gamedata.WeaponTypeMax
		_, ok = h.guns[nextwp]
		if ok {
			h.activewp = nextwp
		}

	}

}
