package tw

import (
	"encoding/xml"
	"fmt"
	"strings"
)

type ServerConfigBuild struct {
	Destroy int `xml:"destroy"`
}

type ServerConfigMisc struct {
	KillRanking     int `xml:"kill_ranking"`
	TradeCancelTime int `xml:"trade_cancel_time"`
	Tutorial        int `xml:"tutorial"`
}

type ServerConfigCommands struct {
	CommandCancelTime int `xml:"command_cancel_time"`
	MillisArrival     int `xml:"millis_arrival"`
}

type ServerConfigNewbie struct {
	Days                 int `xml:"days"`
	Ratio                int `xml:"ratio"`
	RatioDays            int `xml:"ratio_days"`
	RemoveNewbieVillages int `xml:"removeNewbieVillages"`
}

type KnightNewItems int

func (k KnightNewItems) Int() int {
	return int(k)
}

func (k *KnightNewItems) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	var s string

	if err := d.DecodeElement(&s, &start); err != nil {
		return err
	}

	switch strings.ToUpper(s) {
	case "ON", "1":
		*k = 1
	case "OFF", "0", "":
		*k = 0
	default:
		return xml.UnmarshalError(fmt.Sprintf(`KnightNewItems: parsing "%s": invalid syntax`, s))
	}

	return nil
}

type ServerConfigGame struct {
	Archer             int            `xml:"archer"`
	BarbarianMaxPoints int            `xml:"barbarian_max_points"`
	BarbarianRise      float64        `xml:"barbarian_rise"`
	BarbarianShrink    int            `xml:"barbarian_shrink"`
	BaseProduction     int            `xml:"base_production"`
	BuildtimeFormula   int            `xml:"buildtime_formula"`
	Church             int            `xml:"church"`
	Event              int            `xml:"event"`
	FakeLimit          float64        `xml:"fake_limit"`
	FarmLimit          int            `xml:"farm_limit"`
	Hauls              int            `xml:"hauls"`
	HaulsBase          int            `xml:"hauls_base"`
	HaulsMax           int            `xml:"hauls_max"`
	Knight             int            `xml:"knight"`
	KnightNewItems     KnightNewItems `xml:"knight_new_items"`
	Scavenging         int            `xml:"scavenging"`
	Stronghold         int            `xml:"stronghold"`
	SuppressEvents     int            `xml:"suppress_events"`
	Tech               int            `xml:"tech"`
	Watchtower         int            `xml:"watchtower"`
}

type ServerConfigBuildings struct {
	CustomBarracks   int `xml:"custom_barracks"`
	CustomChurch     int `xml:"custom_church"`
	CustomFarm       int `xml:"custom_farm"`
	CustomGarage     int `xml:"custom_garage"`
	CustomHide       int `xml:"custom_hide"`
	CustomIron       int `xml:"custom_iron"`
	CustomMain       int `xml:"custom_main"`
	CustomMarket     int `xml:"custom_market"`
	CustomPlace      int `xml:"custom_place"`
	CustomSmith      int `xml:"custom_smith"`
	CustomSnob       int `xml:"custom_snob"`
	CustomStable     int `xml:"custom_stable"`
	CustomStatue     int `xml:"custom_statue"`
	CustomStone      int `xml:"custom_stone"`
	CustomStorage    int `xml:"custom_storage"`
	CustomWall       int `xml:"custom_wall"`
	CustomWatchtower int `xml:"custom_watchtower"`
	CustomWood       int `xml:"custom_wood"`
}

type ServerConfigSnob struct {
	CheapRebuild  int     `xml:"cheap_rebuild"`
	CoinIron      int     `xml:"coin_iron"`
	CoinStone     int     `xml:"coin_stone"`
	CoinWood      int     `xml:"coin_wood"`
	Factor        float64 `xml:"factor"`
	Gold          int     `xml:"gold"`
	MaxDist       int     `xml:"max_dist"`
	NoBarbConquer int     `xml:"no_barb_conquer"`
	Rise          int     `xml:"rise"`
}

type ServerConfigAlly struct {
	AllytimeSupport       int    `xml:"allytime_support"`
	FixedAllies           int    `xml:"fixed_allies"`
	Levels                int    `xml:"levels"`
	Limit                 int    `xml:"limit"`
	NoHarm                int    `xml:"no_harm"`
	NoJoin                int    `xml:"no_join"`
	NoLeave               int    `xml:"no_leave"`
	NoOtherSupport        int    `xml:"no_other_support"`
	NoOtherSupportType    int    `xml:"no_other_support_type"`
	PointsMemberCount     int    `xml:"points_member_count"`
	WarsAutoacceptDays    int    `xml:"wars_autoaccept_days"`
	WarsMemberRequirement int    `xml:"wars_member_requirement"`
	WarsPointsRequirement int    `xml:"wars_points_requirement"`
	XpRequirements        string `xml:"xp_requirements"`
}

type ServerConfigCoord struct {
	BonusNew        int `xml:"bonus_new"`
	BonusVillages   int `xml:"bonus_villages"`
	EmptyVillages   int `xml:"empty_villages"`
	Func            int `xml:"func"`
	Inner           int `xml:"inner"`
	MapSize         int `xml:"map_size"`
	NobleRestart    int `xml:"noble_restart"`
	SelectStart     int `xml:"select_start"`
	StartVillages   int `xml:"start_villages"`
	VillageMoveWait int `xml:"village_move_wait"`
}

type ServerConfigSitter struct {
	Allow int `xml:"allow"`
}

type ServerConfigSleep struct {
	Active   int `xml:"active"`
	Delay    int `xml:"delay"`
	Max      int `xml:"max"`
	MaxAwake int `xml:"max_awake"`
	Min      int `xml:"min"`
	MinAwake int `xml:"min_awake"`
	WarnTime int `xml:"warn_time"`
}

type ServerConfigNight struct {
	Active    int     `xml:"active"`
	DefFactor float64 `xml:"def_factor"`
	Duration  int     `xml:"duration"`
	EndHour   int     `xml:"end_hour"`
	StartHour int     `xml:"start_hour"`
}

type ServerConfigWin struct {
	Check int `xml:"check"`
}

type ServerConfig struct {
	XMLName   xml.Name              `xml:"config"`
	Speed     float64               `xml:"speed"`
	UnitSpeed float64               `xml:"unit_speed"`
	Moral     int                   `xml:"moral"`
	Build     ServerConfigBuild     `xml:"build"`
	Misc      ServerConfigMisc      `xml:"misc"`
	Commands  ServerConfigCommands  `xml:"commands"`
	Newbie    ServerConfigNewbie    `xml:"newbie"`
	Game      ServerConfigGame      `xml:"game"`
	Buildings ServerConfigBuildings `xml:"buildings"`
	Snob      ServerConfigSnob      `xml:"snob"`
	Ally      ServerConfigAlly      `xml:"ally"`
	Coord     ServerConfigCoord     `xml:"coord"`
	Sitter    ServerConfigSitter    `xml:"sitter"`
	Sleep     ServerConfigSleep     `xml:"sleep"`
	Night     ServerConfigNight     `xml:"night"`
	Win       ServerConfigWin       `xml:"win"`
}
