package game

import (
	"fmt"
	"galaxy/calc"

	"github.com/google/uuid"
)

type ShipType struct {
	ID       uuid.UUID `json:"id"`
	Name     string    `json:"name"`
	Drive    Float     `json:"drive"`
	Armament uint      `json:"armament"`
	Weapons  Float     `json:"weapons"`
	Shields  Float     `json:"shields"`
	Cargo    Float     `json:"cargo"`
}

func (st ShipType) Equal(o ShipType) bool {
	return st.Drive == o.Drive &&
		st.Weapons == o.Weapons &&
		st.Armament == o.Armament &&
		st.Shields == o.Shields &&
		st.Cargo == o.Cargo
}

func (st ShipType) BlockMass(t Tech) float64 {
	switch t {
	case TechDrive:
		return st.DriveBlockMass()
	case TechWeapons:
		return st.WeaponsBlockMass()
	case TechShields:
		return st.ShieldsBlockMass()
	case TechCargo:
		return st.CargoBlockMass()
	default:
		panic("BlockMass: unexpectec tech: " + t.String())
	}
}

func (st ShipType) DriveBlockMass() float64 {
	return st.Drive.F()
}

func (st ShipType) WeaponsBlockMass() float64 {
	if v, ok := calc.WeaponsBlockMass(st.Weapons.F(), st.Armament); !ok {
		panic(fmt.Sprintf("ship class invalid design: A=%d W=%.03f", st.Armament, st.Weapons))
	} else {
		return v
	}
}

func (st ShipType) ShieldsBlockMass() float64 {
	return st.Shields.F()
}

func (st ShipType) CargoBlockMass() float64 {
	return st.Cargo.F()
}

func (st ShipType) EmptyMass() float64 {
	if v, ok := calc.EmptyMass(st.Drive.F(), st.Weapons.F(), st.Armament, st.Shields.F(), st.Cargo.F()); !ok {
		panic(fmt.Sprintf("ship class invalid design: D=%.03f A=%d W=%.03f S=%.03f C=%.03f",
			st.Drive, st.Armament, st.Weapons, st.Shields, st.Cargo))
	} else {
		return v
	}
}
