using System;
using System.Timers;
using System.Collections.Generic;


namespace WorldOfPeacecraft
{
	public class AI
	{
		private Timer timer;
		private Random r;
		private int decision;
		private Backend Backend;
		private string sortby;

		public AI (Backend b)
		{
			r = new Random ();
			timer = new Timer (5000.0);
			timer.Elapsed += new ElapsedEventHandler (this.OnTimedEvent);
			Backend = b;
			b.Challenge += this.ProcessChallenge;

			timer.Start ();
		}

		private void OnTimedEvent(object source, ElapsedEventArgs e)
		{
			Think ("random");
		}

		private void Think(string sortby)
		{
			timer.Stop ();
			if (r.Next (0, 2) == 0) {
				Console.WriteLine ("Another is being challenged");
				lock (Backend) {
					Dictionary <int, Player> playerlist;
					Player player = null;
					int x, y, count;
					playerlist = Backend.getPlayerList ();

					switch (sortby) {
					case "points":
						//TODO
						//Sortiere playerlist nach Anzahl der Punkte aufsteigend
						break;
					case "closest":
						//TODO
						//Sortiere playerlist nach Distanz
						break;
					case "random":
						count = playerlist.Count; //Zähle Anzahl Spieler und wähle einen zufälligen
						player = playerlist [r.Next (0, ++count)]; //'++' weil random ist sonst immer kaputt
						break;
					default: //Fall-Through in C# nicht erlaubt...!?
						count = playerlist.Count; //Zähle Anzahl Spieler und wähle einen zufälligen
						player = playerlist [r.Next (0, ++count)]; //'++' weil random ist sonst immer kaputt
						break;
					}	

					x = player.GetX ();
					y = player.GetY ();
					Backend.MoveTo (x, y);
				}
			} else {
				Console.WriteLine ("Though art making time pass for another");
			}
			timer.Start ();
		}

		private void ProcessChallenge(Challenge c){
			/*	Console.WriteLine ("A challenge hath been proclaimed");
			IGame game = new Skirmish ();
			IGame dragonfight = new GameStrategy (Decision.FIGHT, Decision.REST);
			IGame staghunt = new GameStrategy (Decision.STAG, Decision.BUNNY);
			game.getFirstMove ();
			game.getNextMove (letzteEntscheidungDesGegners);*/
		}
	}
}

