package bunmodel

import (
	"fmt"

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

type ServerConfigBuild struct {
	Destroy int
}

type ServerConfigMisc struct {
	KillRanking     int
	Tutorial        int
	TradeCancelTime int
}

type ServerConfigCommands struct {
	MillisArrival     int
	CommandCancelTime int
}

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

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

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

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

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

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

type ServerConfigSitter struct {
	Allow int
}

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

type ServerConfigNight struct {
	Active    int
	StartHour int
	EndHour   int
	DefFactor float64
	Duration  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
}
