using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Betfair.Gamex.API;
using Betfair.Gamex.API.Entities;

namespace Betfair.Gamex.API.UberClient
{
	/// <summary>
	/// Summary description for GameHistory.
	/// </summary>
	public class GameHistory : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.ComboBox cmBoxGames;
		private System.Windows.Forms.Button btnPrevious;
		private System.Windows.Forms.Button btnNext;
		private System.Windows.Forms.Panel pnlCurrentGame;
        private Adapter m_ApiAdapter;
		private channelHistory m_ChannelHistory;
		private System.Windows.Forms.StatusBar statusBar1;
		private int m_GamePosition = 0;

        public GameHistory(Adapter adapter)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			m_ApiAdapter = adapter;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(GameHistory));
			this.cmBoxGames = new System.Windows.Forms.ComboBox();
			this.pnlCurrentGame = new System.Windows.Forms.Panel();
			this.btnPrevious = new System.Windows.Forms.Button();
			this.btnNext = new System.Windows.Forms.Button();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.SuspendLayout();
			// 
			// cmBoxGames
			// 
			this.cmBoxGames.Items.AddRange(new object[] {
															"Poker",
															"Turbo Poker",
															"Blackjack",
															"Turbo Blackjack",
															"Baccarat",
															"Turbo Baccarat",
															"HiLo",
															"Turbo HiLo"});
			this.cmBoxGames.Location = new System.Drawing.Point(16, 8);
			this.cmBoxGames.Name = "cmBoxGames";
			this.cmBoxGames.Size = new System.Drawing.Size(121, 21);
			this.cmBoxGames.TabIndex = 0;
			this.cmBoxGames.Text = "Poker";
			this.cmBoxGames.SelectedIndexChanged += new System.EventHandler(this.cmBoxGames_SelectedIndexChanged);
			// 
			// pnlCurrentGame
			// 
			this.pnlCurrentGame.BackColor = System.Drawing.Color.Transparent;
			this.pnlCurrentGame.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pnlCurrentGame.Location = new System.Drawing.Point(0, 40);
			this.pnlCurrentGame.Name = "pnlCurrentGame";
			this.pnlCurrentGame.Size = new System.Drawing.Size(624, 240);
			this.pnlCurrentGame.TabIndex = 1;
			// 
			// btnPrevious
			// 
			this.btnPrevious.BackColor = System.Drawing.Color.Transparent;
			this.btnPrevious.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnPrevious.BackgroundImage")));
			this.btnPrevious.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnPrevious.Location = new System.Drawing.Point(320, 8);
			this.btnPrevious.Name = "btnPrevious";
			this.btnPrevious.Size = new System.Drawing.Size(24, 24);
			this.btnPrevious.TabIndex = 11;
			this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
			// 
			// btnNext
			// 
			this.btnNext.BackColor = System.Drawing.Color.Transparent;
			this.btnNext.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnNext.BackgroundImage")));
			this.btnNext.Cursor = System.Windows.Forms.Cursors.Hand;
			this.btnNext.Location = new System.Drawing.Point(352, 8);
			this.btnNext.Name = "btnNext";
			this.btnNext.Size = new System.Drawing.Size(24, 24);
			this.btnNext.TabIndex = 12;
			this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 280);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(624, 22);
			this.statusBar1.SizingGrip = false;
			this.statusBar1.TabIndex = 13;
			this.statusBar1.Text = "Ready";
			// 
			// GameHistory
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(624, 302);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.btnNext);
			this.Controls.Add(this.btnPrevious);
			this.Controls.Add(this.pnlCurrentGame);
			this.Controls.Add(this.cmBoxGames);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "GameHistory";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Game History";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Returns a boolean indicating if there are any games in the history
		/// </summary>
		/// <returns></returns>
		private bool GamesToDisplay()
		{
            return false;// m_ChannelHistory.channel.games.gameCollection.Count > 0;
		}

		
		/// <summary>
		/// Gets the history for the selected channel
		/// </summary>
		private void GetChannelHistory()
		{
			switch(cmBoxGames.Text)
			{
				case "Poker":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444074, -1);
					break;
				case "Turbo Poker":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444080, -1);
					break;
				case "Blackjack":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444077, -1);
					break;
				case "Turbo Blackjack":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444082, -1);
					break;
				case "Baccarat":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444086, -1);
					break;
				case "Turbo Baccarat":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444089, -1);
					break;
				case "HiLo":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444093, -1);
					break;
				case "Turbo HiLo":
					m_ChannelHistory = m_ApiAdapter.GetChannelHistory(1444096, -1);
					break;
			}

			DisplayChannelHistory();
		}

		/// <summary>
		/// Display the currently selected channel history
		/// </summary>
		private void DisplayChannelHistory()
		{
			if(GamesToDisplay())
			{
				switch(cmBoxGames.Text)
				{
					case "Poker":
						DisplayPoker();
						break;
					case "Turbo Poker":
						DisplayPoker();
						break;
					case "Blackjack":
						DisplayBlackjack();
						break;
					case "Turbo Blackjack":
						DisplayBlackjack();
						break;
					case "Baccarat":
						DisplayBaccarat();
						break;
					case "Turbo Baccarat":
						DisplayBaccarat();
						break;
					case "HiLo":
						DisplayHiLo();
						break;
					case "Turbo HiLo":
						DisplayHiLo();
						break;
				}
			}
			else
			{
				MessageBox.Show("There are no " + cmBoxGames.Text + " games to display");
			}
		}

		/// <summary>
		/// Display the Poker channel history
		/// </summary>
		private void DisplayPoker()
		{
            //pnlCurrentGame.Controls.Clear();
            //PokerHistory pokerHist = new PokerHistory(m_ChannelHistory.channel.games.gameCollection[m_GamePosition]);
            //pnlCurrentGame.Controls.Add(pokerHist);
		}

		/// <summary>
		/// Display the Blackjack channel history
		/// </summary>
		private void DisplayBlackjack()
		{
            //pnlCurrentGame.Controls.Clear();
            //BlackjackHistory bjHist = new BlackjackHistory(m_ChannelHistory.channel.games.gameCollection[m_GamePosition]);
            //pnlCurrentGame.Controls.Add(bjHist);
		}

		/// <summary>
		/// Display the Baccarat channel history
		/// </summary>
		private void DisplayBaccarat()
		{
            //pnlCurrentGame.Controls.Clear();
            //BaccaratHistory bacHist = new BaccaratHistory(m_ChannelHistory.channel.games.gameCollection[m_GamePosition]);
            //pnlCurrentGame.Controls.Add(bacHist);
		}

		/// <summary>
		/// Display the HiLo channel history
		/// </summary>
		private void DisplayHiLo()
		{
            //pnlCurrentGame.Controls.Clear();
            //HiLoHistory hiloHist = new HiLoHistory(m_ChannelHistory.channel.games.gameCollection[m_GamePosition]);
            //pnlCurrentGame.Controls.Add(hiloHist);
		}

		/// <summary>
		/// Display the next game
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPrevious_Click(object sender, System.EventArgs e)
		{
			if(m_GamePosition > 0)
			{
				m_GamePosition -= 1;
			}
			else if(m_ChannelHistory.channel.games.previousPage.resource.href != null)
			{
				// We have reached the start of the current collection and there is another page of results
				m_ChannelHistory = m_ApiAdapter.GetChannelHistory(m_ChannelHistory.channel.games.previousPage.resource.href);
				//m_GamePosition = m_ChannelHistory.channel.games.gameCollection.Count - 1;
			}
			DisplayChannelHistory();
		}

		/// <summary>
		/// Display the previous game
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnNext_Click(object sender, System.EventArgs e)
		{
            //if(m_GamePosition < m_ChannelHistory.channel.games.gameCollection.Count - 1)
            //{
            //    m_GamePosition += 1;
            //}
            //else if(m_ChannelHistory.channel.games.nextPage.resource.href != null)
            //{
            //    // We have reached the end of the current collection and there is another page of results
            //    m_ChannelHistory = m_ApiAdapter.GetChannelHistory(m_ChannelHistory.channel.games.nextPage.resource.href);
            //    m_GamePosition = 0;
            //}
            //DisplayChannelHistory();
		}

		private void cmBoxGames_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			this.Enabled = false;
			this.statusBar1.Text = "Loading History...";
			this.pnlCurrentGame.Controls.Clear();
			Refresh();
			GetChannelHistory();
			this.Enabled = true;
			this.statusBar1.Text = "Ready";
		}

	}
}
