package bunmodel

import (
	"fmt"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
)

type Unit struct {
	BuildTime      float64
	Pop            int
	Speed          float64
	Attack         int
	Defense        int
	DefenseCavalry int
	DefenseArcher  int
	Carry          int
}

type UnitInfo struct {
	Spear    Unit
	Sword    Unit
	Axe      Unit
	Archer   Unit
	Spy      Unit
	Light    Unit
	Marcher  Unit
	Heavy    Unit
	Ram      Unit
	Catapult Unit
	Knight   Unit
	Snob     Unit
	Militia  Unit
}

func NewUnitInfo(info domain.UnitInfo) UnitInfo {
	return UnitInfo{
		Spear:    Unit(info.Spear()),
		Sword:    Unit(info.Sword()),
		Axe:      Unit(info.Axe()),
		Archer:   Unit(info.Archer()),
		Spy:      Unit(info.Spy()),
		Light:    Unit(info.Light()),
		Marcher:  Unit(info.Marcher()),
		Heavy:    Unit(info.Heavy()),
		Ram:      Unit(info.Ram()),
		Catapult: Unit(info.Catapult()),
		Knight:   Unit(info.Knight()),
		Snob:     Unit(info.Snob()),
		Militia:  Unit(info.Militia()),
	}
}

func (u UnitInfo) ToDomain() (domain.UnitInfo, error) {
	info, err := domain.NewUnitInfo(
		domain.Unit(u.Spear),
		domain.Unit(u.Sword),
		domain.Unit(u.Axe),
		domain.Unit(u.Archer),
		domain.Unit(u.Spy),
		domain.Unit(u.Light),
		domain.Unit(u.Marcher),
		domain.Unit(u.Heavy),
		domain.Unit(u.Ram),
		domain.Unit(u.Catapult),
		domain.Unit(u.Knight),
		domain.Unit(u.Snob),
		domain.Unit(u.Militia),
	)
	if err != nil {
		return domain.UnitInfo{}, fmt.Errorf("couldn't construct domain.UnitInfo: %w", err)
	}

	return info, nil
}
