using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
#if WINDOWS || LINUX || ANDROID
using Microsoft.Xna.Framework.Storage;
#elif WINDOWS_PHONE
using System.IO.IsolatedStorage;
#endif
using System.Diagnostics;
using System.IO;
using ShootingHero.Utilities;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace ShootingHero.Scene
{
	public class RankingScene : DrawableGameComponent
	{
		struct Ranking : IComparable<Ranking>
		{
			internal int stage, score;

			public int CompareTo ( Ranking other )
			{
				if ( other.score > score ) return 1;
				else if ( other.score < score ) return -1;
				else return 0;
			}
		}

		List<Ranking> rankings = new List<Ranking> ();
		Texture2D fan;
		Font font;

		public RankingScene ( Game game, int stage, int score )
			: base ( game )
		{
#if WINDOWS || LINUX || ANDROID
			StorageDevice.BeginShowSelector ( ( IAsyncResult ar ) =>
				{
					StorageDevice storageDevice = StorageDevice.EndShowSelector ( ar );
					storageDevice.BeginOpenContainer ( "Shooting Hero",
						( IAsyncResult ar2 ) =>
						{
							StorageContainer storageContainer = storageDevice.EndOpenContainer ( ar2 );
#endif
			Stream stream =
#if WINDOWS || LINUX || ANDROID
								storageContainer.OpenFile ( "ranking.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite );
#elif WINDOWS_PHONE
								new IsolatedStorageFileStream ( "ranking.bin", FileMode.OpenOrCreate, IsolatedStorageFile.GetUserStoreForApplication () );
#endif
							if ( stream.Position != stream.Length )
							{
								BinaryReader br = new BinaryReader ( stream, Encoding.UTF8 );
								for ( int i = 0; i < 10; i++ )
									rankings.Add ( new Ranking () { stage = br.ReadByte (), score = br.ReadInt32 () } );
							}
							rankings.Add ( new Ranking () { stage = stage, score = score } );
							for ( int i = rankings.Count - 1; i < 10; i++ )
								rankings.Add ( new Ranking () { stage = 0, score = 0 } );
							rankings.Sort ();
							stream.Position = 0;
							BinaryWriter bw = new BinaryWriter ( stream, Encoding.UTF8 );
							for ( int i = 0; i < 10; i++ )
							{
								bw.Write ( ( byte ) rankings [ i ].stage );
								bw.Write ( ( int ) rankings [ i ].score );
							}
							stream.Close ();
#if WINDOWS || LINUX || ANDROID
						}, null );
				}, null );
#endif
		}

		protected override void LoadContent ()
		{
			fan = Game.Content.Load<Texture2D> ( "Menu/rank" );
			font = new Font ( Game.Content.Load<Texture2D> ( "Font/font" ) );
			base.LoadContent ();
		}

		public override void Update ( GameTime gameTime )
		{
			GamePadState gamePad = GamePad.GetState ( PlayerIndex.One );
			MouseState mouse = Mouse.GetState ();
			KeyboardState keyboard = Keyboard.GetState ();
#if ANDROID || IOS
			TouchCollection touches = TouchPanel.GetState ();
#endif

			if ( gamePad.Buttons.Back == ButtonState.Pressed ||
				mouse.LeftButton == ButtonState.Pressed ||
				keyboard.IsKeyDown ( Keys.Escape )
#if ANDROID || IOS
				|| ( touches.Count > 0 && touches [ 0 ].State == TouchLocationState.Pressed )
#endif
 )
				( Game.Components [ 0 ] as Transitor ).Transition ( new MenuScene ( Game ) );

			base.Update ( gameTime );
		}

		public override void Draw ( GameTime gameTime )
		{
			SpriteBatch spriteBatch = ( Game as ShootingHero ).SpriteBatch;

			spriteBatch.Draw ( fan, new Vector2 (), Color.White );

			for ( int i = 0; i < 10; i++ )
			{
				font.DrawFont ( spriteBatch, i + 1, 40, 212 + i * 55, Color.White, false );
				font.DrawFont ( spriteBatch, rankings [ i ].stage, 112, 212 + i * 55, Color.White, false );
				font.DrawFont ( spriteBatch, rankings [ i ].score, 240, 212 + i * 55, Color.White, true );
			}

			base.Draw ( gameTime );
		}
	}
}
