package bunmodel

import (
	"fmt"

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

type ServerConfigBuild struct {
	Destroy int
}

type ServerConfigMisc struct {
	KillRanking     int
	TradeCancelTime int
	Tutorial        int
}

type ServerConfigCommands struct {
	CommandCancelTime int
	MillisArrival     int
}

type ServerConfigNewbie struct {
	Days                 int
	Ratio                int
	RatioDays            int
	RemoveNewbieVillages int
}

type ServerConfigGame struct {
	Archer             int
	BarbarianMaxPoints int
	BarbarianRise      float64
	BarbarianShrink    int
	BaseProduction     int
	BuildtimeFormula   int
	Church             int
	Event              int
	FakeLimit          float64
	FarmLimit          int
	Hauls              int
	HaulsBase          int
	HaulsMax           int
	Knight             int
	KnightNewItems     int
	Scavenging         int
	Stronghold         int
	SuppressEvents     int
	Tech               int
	Watchtower         int
}

type ServerConfigBuildings struct {
	CustomBarracks   int
	CustomChurch     int
	CustomFarm       int
	CustomGarage     int
	CustomHide       int
	CustomIron       int
	CustomMain       int
	CustomMarket     int
	CustomPlace      int
	CustomSmith      int
	CustomSnob       int
	CustomStable     int
	CustomStatue     int
	CustomStone      int
	CustomStorage    int
	CustomWall       int
	CustomWatchtower int
	CustomWood       int
}

type ServerConfigSnob struct {
	CheapRebuild  int
	CoinIron      int
	CoinStone     int
	CoinWood      int
	Factor        float64
	Gold          int
	MaxDist       int
	NoBarbConquer int
	Rise          int
}

type ServerConfigAlly struct {
	AllytimeSupport       int
	FixedAllies           int
	Levels                int
	Limit                 int
	NoHarm                int
	NoJoin                int
	NoLeave               int
	NoOtherSupport        int
	NoOtherSupportType    int
	PointsMemberCount     int
	WarsAutoacceptDays    int
	WarsMemberRequirement int
	WarsPointsRequirement int
	XpRequirements        string
}

type ServerConfigCoord struct {
	BonusNew        int
	BonusVillages   int
	EmptyVillages   int
	Func            int
	Inner           int
	MapSize         int
	NobleRestart    int
	SelectStart     int
	StartVillages   int
	VillageMoveWait int
}

type ServerConfigSitter struct {
	Allow int
}

type ServerConfigSleep struct {
	Active   int
	Delay    int
	Max      int
	MaxAwake int
	Min      int
	MinAwake int
	WarnTime int
}

type ServerConfigNight struct {
	Active    int
	DefFactor float64
	Duration  int
	EndHour   int
	StartHour int
}

type ServerConfigWin struct {
	Check int
}

type ServerConfig struct {
	Speed     float64
	UnitSpeed float64
	Moral     int
	Build     ServerConfigBuild
	Misc      ServerConfigMisc
	Commands  ServerConfigCommands
	Newbie    ServerConfigNewbie
	Game      ServerConfigGame
	Buildings ServerConfigBuildings
	Snob      ServerConfigSnob
	Ally      ServerConfigAlly
	Coord     ServerConfigCoord
	Sitter    ServerConfigSitter
	Sleep     ServerConfigSleep
	Night     ServerConfigNight
	Win       ServerConfigWin
}

func NewServerConfig(cfg domain.ServerConfig) ServerConfig {
	return ServerConfig{
		Speed:     cfg.Speed(),
		UnitSpeed: cfg.UnitSpeed(),
		Moral:     cfg.Moral(),
		Build:     ServerConfigBuild(cfg.Build()),
		Misc:      ServerConfigMisc(cfg.Misc()),
		Commands:  ServerConfigCommands(cfg.Commands()),
		Newbie:    ServerConfigNewbie(cfg.Newbie()),
		Game:      ServerConfigGame(cfg.Game()),
		Buildings: ServerConfigBuildings(cfg.Buildings()),
		Snob:      ServerConfigSnob(cfg.Snob()),
		Ally:      ServerConfigAlly(cfg.Ally()),
		Coord:     ServerConfigCoord(cfg.Coord()),
		Sitter:    ServerConfigSitter(cfg.Sitter()),
		Sleep:     ServerConfigSleep(cfg.Sleep()),
		Night:     ServerConfigNight(cfg.Night()),
		Win:       ServerConfigWin(cfg.Win()),
	}
}

func (s ServerConfig) ToDomain() (domain.ServerConfig, error) {
	cfg, err := domain.NewServerConfig(
		s.Speed,
		s.UnitSpeed,
		s.Moral,
		domain.ServerConfigBuild(s.Build),
		domain.ServerConfigMisc(s.Misc),
		domain.ServerConfigCommands(s.Commands),
		domain.ServerConfigNewbie(s.Newbie),
		domain.ServerConfigGame(s.Game),
		domain.ServerConfigBuildings(s.Buildings),
		domain.ServerConfigSnob(s.Snob),
		domain.ServerConfigAlly(s.Ally),
		domain.ServerConfigCoord(s.Coord),
		domain.ServerConfigSitter(s.Sitter),
		domain.ServerConfigSleep(s.Sleep),
		domain.ServerConfigNight(s.Night),
		domain.ServerConfigWin(s.Win),
	)
	if err != nil {
		return domain.ServerConfig{}, fmt.Errorf("couldn't construct domain.ServerConfig: %w", err)
	}

	return cfg, nil
}
