using System;
using System.Collections.Generic;
using System.Text;
using Vortex;
using Vortex.Drawing;
using Vortex.Audio;
using Vortex.Input;
using Vortex.Utils;
using Vortex.Debugging;
using System.Threading;
using System.Drawing;

namespace TestGround {

	/// <summary>
	/// Simple game which is used for testing Vortex2D.NET features during development
	/// </summary>
	class TestGame : Game {
		SoundSource _coinSound;
		Texture _dropTexture;

		public TestGame() {
			Canvas2D c = Game.Canvas;
			Vector2 s = c.Size;
		}

		/// <summary>
		/// Loading texture after some pause
		/// </summary>
		protected override void AsyncLoad() {
			Thread.Sleep(1000);
			_dropTexture = new Texture("drop.png");

			CreatePacmanFramesTexture();
		}

		protected override void Load() {
			_coinSound = new SoundSource("coin_3.wav", false);

			Keyboard.KeyDown += delegate(VirtualKey vKey) {
				if (vKey == VirtualKey.C) {
					_coinSound.PlayOnce();
					//_coinSound.Play(true);
				}
				if (vKey == VirtualKey.E) {
					Sound sound = _coinSound.Play();
					sound.Pan = RandomUtils.NextFloat(-1, 1);
					sound.OnStop += delegate(SoundStopEventInfo eventInfo) {
						Log.Info("Sound is stopped, reason = {0}", eventInfo.Reason);
					};
					sound.Volume = 0.7f;
				}
			};
		}

		protected override void Update(GameTime time) {
			
		}

		protected override void Render(Canvas2D canvas) {
			canvas.Clear(ColorU.Black);

			if (null != _dropTexture) {
				canvas.DrawSprite(new Vector2(100, 200), _dropTexture.ToSprite(), ColorU.White);
			}
			//canvas.DrawLine(100, 100, 200, 200, ColorU.Red);
			//canvas.DrawLine(100, 100, 400, 200, ColorU.Red);
            for (int i = 0; i <= 300; i++)
            {
                for (int j = 0; j <= 20; j++)
                {
                    canvas.DrawFilledRect(i*2+4, j*20, 2, 10, ColorU.Red);
                }
            }

		}

		private void CreatePacmanFramesTexture() {
			Bitmap bitmap = new Bitmap(128, 128, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			Graphics gfx = Graphics.FromImage(bitmap);
			gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

			float angleStep = 180f / 16f;
			for (int n = 0; n < 4; ++n) {
				for (int m = 0; m < 4; ++m) {
					int offsetX = m * 32, offsetY = n * 32;
					float currentAngle = angleStep * (n * 4 + m);
					float sweepAngle = 360f - currentAngle * 2f;
					gfx.FillPie(new SolidBrush(Color.White), new Rectangle(offsetX + 3, offsetY + 3, 26, 26), -currentAngle, -sweepAngle);
				}
			}

			gfx.Dispose();
			bitmap.Save("pacman_Frames.png");

			bitmap.Dispose();
		}


		#region Main Method
		
		[STAThread]
		public static void Main() {
			//create and run game
			using (Game game = new TestGame()) {
				game.Run();
			}
		}

		#endregion
	}

}
