package model

import (
	"encoding/json"
	"math/rand"
	"time"
)

const colliderConfig = string(`[
	{
	  "points": [
		{
		  "x": -134.76280764651332,
		  "y": 36.59870177632015
		},
		{
		  "x": -149.96104083455333,
		  "y": 37.52573863104569
		},
		{
		  "x": -150.21098017675584,
		  "y": 83.12035091732146
		},
		{
		  "x": -135.27938637489518,
		  "y": 82.84603735210767
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -147.0960188632427,
		  "y": 145.82194409905813
		},
		{
		  "x": -129.72821679476212,
		  "y": 145.84054725798703
		},
		{
		  "x": -130.16083328619942,
		  "y": 160.0177071434725
		},
		{
		  "x": -146.45447794800717,
		  "y": 158.46891865140736
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -159.678997709199,
		  "y": 159.94765563215677
		},
		{
		  "x": -159.44115838875882,
		  "y": 132.4449990164753
		},
		{
		  "x": -180.12732661412934,
		  "y": 132.5267338467283
		},
		{
		  "x": -180.17229357739834,
		  "y": 159.6202983216568
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -115.74545613381667,
		  "y": 138.317834682693
		},
		{
		  "x": -111.25952375698267,
		  "y": 134.71256859603577
		},
		{
		  "x": -101.13067070226829,
		  "y": 133.35464291612786
		},
		{
		  "x": -93.17433831673655,
		  "y": 140.1597139990872
		},
		{
		  "x": -92.41125503397573,
		  "y": 148.31747117728315
		},
		{
		  "x": -97.74692620197038,
		  "y": 157.27351893933812
		},
		{
		  "x": -106.90529908010757,
		  "y": 159.3450131543943
		},
		{
		  "x": -114.59769828281645,
		  "y": 155.44703988755012
		},
		{
		  "x": -118.17332678808073,
		  "y": 147.44828540423228
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -68.88628511267953,
		  "y": 132.2834311218785
		},
		{
		  "x": -42.045387560315646,
		  "y": 132.00716031794104
		},
		{
		  "x": -42.20046406663204,
		  "y": 159.5541046906684
		},
		{
		  "x": -68.47667764464055,
		  "y": 159.2107123029332
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -75.8260960957007,
		  "y": 115.49083054872942
		},
		{
		  "x": -44.50520130976248,
		  "y": 115.46143474288765
		},
		{
		  "x": -44.4751022207352,
		  "y": 99.58887047280058
		},
		{
		  "x": -71.4193750551749,
		  "y": 99.51028932495318
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -13.76222799574569,
		  "y": 168.293704197816
		},
		{
		  "x": -12.975366833737905,
		  "y": 146.759699679029
		},
		{
		  "x": 10.288959937152631,
		  "y": 168.00694231291934
		},
		{
		  "x": 10.025664587698516,
		  "y": 147.6327986505318
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 56.2530203941356,
		  "y": 145.73471007114293
		},
		{
		  "x": 55.88170164972547,
		  "y": 168.4387550325503
		},
		{
		  "x": 95.11419563449167,
		  "y": 168.38382384327448
		},
		{
		  "x": 94.7685498515204,
		  "y": 145.77856406346228
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 168.82991193609266,
		  "y": 83.98667996062815
		},
		{
		  "x": 131.45089386421742,
		  "y": 83.54085067357755
		},
		{
		  "x": 131.4976561655,
		  "y": 45.14926380913366
		},
		{
		  "x": 168.74699329201226,
		  "y": 45.14764330021811
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 168.50412236740476,
		  "y": 14.807425442342433
		},
		{
		  "x": 131.54020377240352,
		  "y": 14.699295148318726
		},
		{
		  "x": 131.75258599823695,
		  "y": -28.291798702782874
		},
		{
		  "x": 168.53859770012633,
		  "y": -28.235130096954045
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 168.71817061135056,
		  "y": -66.0745429108263
		},
		{
		  "x": 131.22312019482675,
		  "y": -64.79930745746526
		},
		{
		  "x": 132.23608617529982,
		  "y": -99.72217488020596
		},
		{
		  "x": 168.48370926947052,
		  "y": -99.69483316878097
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 168.80029922545944,
		  "y": -140.69872158717823
		},
		{
		  "x": 142.4311318033584,
		  "y": -140.8969508717683
		},
		{
		  "x": 142.74899555369728,
		  "y": -153.5601259492045
		},
		{
		  "x": 124.47573251057145,
		  "y": -153.33967807392247
		},
		{
		  "x": 124.53277406305531,
		  "y": -178.70799188796045
		},
		{
		  "x": 169.72983758478335,
		  "y": -181.05326409754238
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 86.31249954439815,
		  "y": -178.54842179962878
		},
		{
		  "x": 85.25139518317812,
		  "y": -153.6591855720807
		},
		{
		  "x": 44.352168763418426,
		  "y": -155.57713783703866
		},
		{
		  "x": 44.49136441554095,
		  "y": -178.7466878014278
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 21.98810772124007,
		  "y": -165.4646214101463
		},
		{
		  "x": 2.291384384763129,
		  "y": -166.13343285877838
		},
		{
		  "x": 1.8585696313383524,
		  "y": -138.84481107113936
		},
		{
		  "x": -4.450509531846793,
		  "y": -138.39460190487293
		},
		{
		  "x": -4.2484052334988185,
		  "y": -125.22152591432113
		},
		{
		  "x": 0.8480940681206803,
		  "y": -120.43461423908455
		},
		{
		  "x": 5.944726200100281,
		  "y": -120.66652677148332
		},
		{
		  "x": 10.013368937799234,
		  "y": -118.53954332439699
		},
		{
		  "x": 16.74959252345402,
		  "y": -119.19080806100477
		},
		{
		  "x": 20.182070679792957,
		  "y": -122.75521752681577
		},
		{
		  "x": 20.864962006693112,
		  "y": -133.37752416021453
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -17.45845611622457,
		  "y": -179.16360805863607
		},
		{
		  "x": -18.232673625720697,
		  "y": -166.2416345119525
		},
		{
		  "x": -55.82653020337935,
		  "y": -166.87769415057193
		},
		{
		  "x": -55.667257757004066,
		  "y": -179.467966179626
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -91.84929073623285,
		  "y": -178.96823358525253
		},
		{
		  "x": -92.67477631696086,
		  "y": -156.63806808140026
		},
		{
		  "x": -131.02844258083024,
		  "y": -154.1495120732941
		},
		{
		  "x": -131.0999013266243,
		  "y": -178.47023076860995
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -191.0451593112413,
		  "y": -92.02145512090436
		},
		{
		  "x": -191.21765922650184,
		  "y": -60.29035301044602
		},
		{
		  "x": -178.4109452366466,
		  "y": -59.834010850681416
		},
		{
		  "x": -178.19121597263864,
		  "y": -91.5492424729369
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -189.2166257587677,
		  "y": -14.309557115193636
		},
		{
		  "x": -189.17467357685447,
		  "y": 17.382820625516445
		},
		{
		  "x": -174.48123141097108,
		  "y": 17.74056806256345
		},
		{
		  "x": -174.0436092481555,
		  "y": -12.76375937334197
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -175.27895005827645,
		  "y": 56.815579043899675
		},
		{
		  "x": -182.80126950314406,
		  "y": 56.86188071958679
		},
		{
		  "x": -182.8109217129792,
		  "y": 62.942252442879166
		},
		{
		  "x": -176.60273145460553,
		  "y": 62.9018594991628
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -80.58501549853038,
		  "y": 70.05274999193853
		},
		{
		  "x": -75.17283954361577,
		  "y": 76.81660992809516
		},
		{
		  "x": -70.57133921600123,
		  "y": 71.79976351178688
		},
		{
		  "x": -68.86476284794878,
		  "y": 65.9830426678284
		},
		{
		  "x": -69.09376855817412,
		  "y": 59.237576014680236
		},
		{
		  "x": -71.92591620950236,
		  "y": 52.6597838081403
		},
		{
		  "x": -77.77390452405984,
		  "y": 47.80401689005819
		},
		{
		  "x": -77.70163016414433,
		  "y": 60.86515908180312
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -94.67926094217101,
		  "y": 62.073176929497365
		},
		{
		  "x": -102.69448052265221,
		  "y": 60.53542749391139
		},
		{
		  "x": -101.14120023521055,
		  "y": 54.41121424332029
		},
		{
		  "x": -96.72878440849145,
		  "y": 49.361265760771694
		},
		{
		  "x": -91.40440120091195,
		  "y": 46.09643560591691
		},
		{
		  "x": -86.43550709608706,
		  "y": 45.29746295249614
		},
		{
		  "x": -80.1376329964708,
		  "y": 45.93909166455311
		},
		{
		  "x": -86.2582034028532,
		  "y": 54.324200042417004
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -107.40285476375587,
		  "y": 9.567071488201401
		},
		{
		  "x": -104.22821546158869,
		  "y": 6.149075403932512
		},
		{
		  "x": -103.93192775315973,
		  "y": -0.7595028233513617
		},
		{
		  "x": -107.89097329268903,
		  "y": -4.584123147699447
		},
		{
		  "x": -129.70376598704448,
		  "y": -4.638875071653279
		},
		{
		  "x": -132.133631975492,
		  "y": -2.077646648770884
		},
		{
		  "x": -139.14477515061714,
		  "y": -1.6870864473438605
		},
		{
		  "x": -139.06525429277798,
		  "y": 6.213560253777865
		},
		{
		  "x": -132.67771885821983,
		  "y": 6.057628959696224
		},
		{
		  "x": -129.65013639847558,
		  "y": 8.64349564939343
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -149.15143421297557,
		  "y": -34.81619355561561
		},
		{
		  "x": -135.70456734801604,
		  "y": -35.0260800908972
		},
		{
		  "x": -135.53636529733626,
		  "y": -61.22507635832527
		},
		{
		  "x": -149.0426677422849,
		  "y": -61.67734814403097
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 93.95156676894933,
		  "y": 8.885962386327135
		},
		{
		  "x": 94.05122670829088,
		  "y": -25.61476999128133
		},
		{
		  "x": 58.66286001132384,
		  "y": -25.737060854366433
		},
		{
		  "x": 58.52195974821446,
		  "y": 8.677705924585622
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 33.42961236324993,
		  "y": -55.49650639845896
		},
		{
		  "x": 37.843762186339745,
		  "y": -54.3942606652553
		},
		{
		  "x": 51.21767493989849,
		  "y": -54.51534387139174
		},
		{
		  "x": 55.93038285483507,
		  "y": -55.954354684591124
		},
		{
		  "x": 56.385619162705574,
		  "y": -59.387941641843064
		},
		{
		  "x": 56.45614213963276,
		  "y": -73.22641887246681
		},
		{
		  "x": 55.382299566508436,
		  "y": -77.78454449703086
		},
		{
		  "x": 51.21995037599814,
		  "y": -78.68127127331684
		},
		{
		  "x": 37.859634644195694,
		  "y": -78.69753808226517
		},
		{
		  "x": 33.942023354985324,
		  "y": -77.60815556964018
		},
		{
		  "x": 32.492005760909166,
		  "y": -73.49057432643546
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 104.82330800124852,
		  "y": -89.84785890565206
		},
		{
		  "x": 84.86773710069083,
		  "y": -90.1049343603242
		},
		{
		  "x": 84.81925964355469,
		  "y": -76.16854934189104
		},
		{
		  "x": 104.2863334715984,
		  "y": -76.0572672490185
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -118.29437613835375,
		  "y": -53.98616394194136
		},
		{
		  "x": -118.3135597099538,
		  "y": -104.39266385662052
		},
		{
		  "x": -80.11481466869493,
		  "y": -104.3241976737357
		},
		{
		  "x": -80.15177596257101,
		  "y": -53.92855554587001
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -55.485026848640786,
		  "y": -54.16401762499772
		},
		{
		  "x": 7.114754486110277,
		  "y": -54.208422215531066
		},
		{
		  "x": 7.324517497962553,
		  "y": -79.40124630541432
		},
		{
		  "x": 0.9232194178729909,
		  "y": -98.1808493238061
		},
		{
		  "x": -17.69153491560965,
		  "y": -104.57897627454443
		},
		{
		  "x": -55.43752662405879,
		  "y": -104.60603215064357
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -43.17306377174174,
		  "y": -29.801860347603274
		},
		{
		  "x": -43.192339131999304,
		  "y": 20.51987499907807
		},
		{
		  "x": 6.825587557304457,
		  "y": 21.05222515410243
		},
		{
		  "x": 7.224362273486616,
		  "y": -29.111491647183545
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -42.87033214647569,
		  "y": 45.351188311112956
		},
		{
		  "x": -43.188671494304394,
		  "y": 70.44464413334555
		},
		{
		  "x": -36.49869108821278,
		  "y": 89.74295802209726
		},
		{
		  "x": -18.257476762240437,
		  "y": 95.6741169259178
		},
		{
		  "x": 19.373706486490665,
		  "y": 95.80690323898295
		},
		{
		  "x": 19.433560789848137,
		  "y": 45.937090660326554
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": 44.14406953160537,
		  "y": 45.93830455085161
		},
		{
		  "x": 82.12487818433077,
		  "y": 45.6714239961186
		},
		{
		  "x": 82.29542209773722,
		  "y": 95.68895892758054
		},
		{
		  "x": 44.646992586871725,
		  "y": 95.93034830245234
		}
	  ],
	  "projectile": false
	},
	{
	  "points": [
		{
		  "x": -205.23849096938417,
		  "y": 170.04652801074164
		},
		{
		  "x": -204.58637432554372,
		  "y": -181.32298744505053
		},
		{
		  "x": -206.5819830557868,
		  "y": -181.38381958007812
		},
		{
		  "x": -206.26970489489221,
		  "y": 169.66818207391873
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": -206.35173496549913,
		  "y": 168.72907241221594
		},
		{
		  "x": 170.74896411074113,
		  "y": 167.81460769760488
		},
		{
		  "x": 170.4021400308151,
		  "y": 170.1195903380225
		},
		{
		  "x": -206.20857883085066,
		  "y": 170.63378897327271
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 169.40765931083928,
		  "y": 169.92240906084479
		},
		{
		  "x": 168.54016616834198,
		  "y": -141.0681498157132
		},
		{
		  "x": 170.76444403694975,
		  "y": -141.08674730712553
		},
		{
		  "x": 170.8766914963365,
		  "y": 169.49222111878674
		}
	  ],
	  "projectile": true
	},
	{
	  "points": [
		{
		  "x": 124.78405100832707,
		  "y": -178.85325005704954
		},
		{
		  "x": -207.06892581351767,
		  "y": -179.2171201136513
		},
		{
		  "x": -206.75338501917312,
		  "y": -181.87465849244123
		},
		{
		  "x": 124.87800993763317,
		  "y": -180.4528961697043
		}
	  ],
	  "projectile": true
	}
  ]`)

// Collider ...
type Collider struct {
	Points     []*Point `json:"points"`
	Projectile bool     `json:"projectile"`
}

func (c *Collider) getPolygon() Polygon {
	return Polygon{
		Points: c.Points,
	}
}

// Map represents a game map
type Map struct {
	Collider []*Collider
	Spawns   []*Point
}

// NewMap creates a new map object
func NewMap() *Map {
	colliders := make([]*Collider, 0)
	json.Unmarshal([]byte(colliderConfig), &colliders)

	return &Map{
		Spawns: []*Point{
			&Point{
				X: 33.92122716470902,
				Y: 19.696850953769385,
			},
			&Point{
				X: 28.825795356963976,
				Y: 37.811654746868406,
			},
			&Point{
				X: 35.76508317625655,
				Y: 96.37030297792447,
			},
			&Point{
				X: -60.31281833122437,
				Y: 25.988099670613494,
			},
			&Point{
				X: -128.18862044178346,
				Y: 27.185409609029083,
			},
			&Point{
				X: -153.69567902314972,
				Y: 112.68349326947794,
			},
			&Point{
				X: -29.803050043935457,
				Y: 25.9135,
			},
			&Point{
				X: -47.0153,
				Y: -48.3082,
			},
			&Point{
				X: -76.9886,
				Y: -48.8321,
			},
			&Point{
				X: -101.4403,
				Y: -10.6191,
			},
			&Point{
				X: 45.9105,
				Y: 103.6027,
			},
			&Point{
				X: 145.1918,
				Y: -130.7977,
			},
			&Point{
				X: -50.2197,
				Y: -160.9607,
			},
			&Point{
				X: -146.8031,
				Y: -156.6231,
			},
			&Point{
				X: -195.3906,
				Y: 24.7999,
			},
			&Point{
				X: -41.0684,
				Y: 157.3888,
			},
		},
		Collider: colliders,
	}
}

// GetRandomSpawn Point
func (m *Map) GetRandomSpawn(players []*Player) *Point {
	rand.Seed(time.Now().Unix())
	index := rand.Intn(len(m.Spawns))
	spawn := m.Spawns[index]
	occupied := false
	for _, p := range players {
		if p.IsAlive() && spawn.WithinDistanceOf(4, p.Collider.Pivot) {
			occupied = true
			break
		}
	}
	if occupied { // try again
		return m.GetRandomSpawn(players)
	}
	return spawn
}
