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

namespace Betfair.Gamex.API.UberClient.Controls
{
	/// <summary>
	/// Shows all bet history for the user
	/// </summary>
	public class BetHistory : System.Windows.Forms.Form
	{
        private Adapter m_ApiAdapter;
		private System.Windows.Forms.Panel pnlBetItems;
		private System.Windows.Forms.Button btnPrevious;
		private System.Windows.Forms.Button btnNext;
		private System.Windows.Forms.ComboBox cmboxStatus;
		private System.Windows.Forms.DateTimePicker dateTimePicker1;
		private System.Windows.Forms.Button btnRefresh;
		private betHistory m_BetHistory;

        public BetHistory(Adapter adapter)
		{
			InitializeComponent();
			m_ApiAdapter = adapter;
			GetBetHistory();
			dateTimePicker1.MaxDate = DateTime.Now.AddDays(-1);
		}

		#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.Windows.Forms.GroupBox groupBox1;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BetHistory));
            System.Windows.Forms.Label label8;
            System.Windows.Forms.Label label7;
            System.Windows.Forms.Label label6;
            System.Windows.Forms.Label label5;
            System.Windows.Forms.Label label4;
            System.Windows.Forms.Label label3;
            System.Windows.Forms.Label label2;
            System.Windows.Forms.Label label1;
            this.btnPrevious = new System.Windows.Forms.Button();
            this.btnNext = new System.Windows.Forms.Button();
            this.pnlBetItems = new System.Windows.Forms.Panel();
            this.cmboxStatus = new System.Windows.Forms.ComboBox();
            this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
            this.btnRefresh = new System.Windows.Forms.Button();
            groupBox1 = new System.Windows.Forms.GroupBox();
            label8 = new System.Windows.Forms.Label();
            label7 = new System.Windows.Forms.Label();
            label6 = new System.Windows.Forms.Label();
            label5 = new System.Windows.Forms.Label();
            label4 = new System.Windows.Forms.Label();
            label3 = new System.Windows.Forms.Label();
            label2 = new System.Windows.Forms.Label();
            label1 = new System.Windows.Forms.Label();
            groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            groupBox1.BackColor = System.Drawing.Color.Transparent;
            groupBox1.Controls.Add(this.btnPrevious);
            groupBox1.Controls.Add(this.btnNext);
            groupBox1.Controls.Add(this.pnlBetItems);
            groupBox1.Controls.Add(label8);
            groupBox1.Controls.Add(label7);
            groupBox1.Controls.Add(label6);
            groupBox1.Controls.Add(label5);
            groupBox1.Controls.Add(label4);
            groupBox1.Controls.Add(label3);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label1);
            groupBox1.Location = new System.Drawing.Point(24, 40);
            groupBox1.Name = "groupBox1";
            groupBox1.Size = new System.Drawing.Size(720, 240);
            groupBox1.TabIndex = 0;
            groupBox1.TabStop = false;
            groupBox1.Text = "Bets";
            // 
            // btnPrevious
            // 
            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, 208);
            this.btnPrevious.Name = "btnPrevious";
            this.btnPrevious.Size = new System.Drawing.Size(24, 24);
            this.btnPrevious.TabIndex = 10;
            this.btnPrevious.Click += new System.EventHandler(this.btnPrevious_Click);
            // 
            // btnNext
            // 
            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, 208);
            this.btnNext.Name = "btnNext";
            this.btnNext.Size = new System.Drawing.Size(24, 24);
            this.btnNext.TabIndex = 9;
            this.btnNext.Click += new System.EventHandler(this.btnNext_Click);
            // 
            // pnlBetItems
            // 
            this.pnlBetItems.AutoScroll = true;
            this.pnlBetItems.Location = new System.Drawing.Point(8, 40);
            this.pnlBetItems.Name = "pnlBetItems";
            this.pnlBetItems.Size = new System.Drawing.Size(696, 160);
            this.pnlBetItems.TabIndex = 8;
            // 
            // label8
            // 
            label8.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label8.Location = new System.Drawing.Point(564, 16);
            label8.Name = "label8";
            label8.Size = new System.Drawing.Size(56, 23);
            label8.TabIndex = 7;
            label8.Text = "P/L";
            label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label7
            // 
            label7.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label7.Location = new System.Drawing.Point(618, 16);
            label7.Name = "label7";
            label7.Size = new System.Drawing.Size(70, 23);
            label7.TabIndex = 6;
            label7.Text = "Result";
            label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label6
            // 
            label6.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label6.Location = new System.Drawing.Point(501, 16);
            label6.Name = "label6";
            label6.Size = new System.Drawing.Size(64, 23);
            label6.TabIndex = 5;
            label6.Text = "Size";
            label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label5
            // 
            label5.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label5.Location = new System.Drawing.Point(446, 16);
            label5.Name = "label5";
            label5.Size = new System.Drawing.Size(56, 23);
            label5.TabIndex = 4;
            label5.Text = "Price";
            label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label4
            // 
            label4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label4.Location = new System.Drawing.Point(375, 16);
            label4.Name = "label4";
            label4.Size = new System.Drawing.Size(73, 23);
            label4.TabIndex = 3;
            label4.Text = "Placed";
            label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label3
            // 
            label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label3.Location = new System.Drawing.Point(304, 16);
            label3.Name = "label3";
            label3.Size = new System.Drawing.Size(72, 23);
            label3.TabIndex = 2;
            label3.Text = "Bid Type";
            label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label2.Location = new System.Drawing.Point(105, 16);
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size(200, 23);
            label2.TabIndex = 1;
            label2.Text = "Description";
            label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label1
            // 
            label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label1.Location = new System.Drawing.Point(8, 16);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(99, 23);
            label1.TabIndex = 0;
            label1.Text = "BetID";
            label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // cmboxStatus
            // 
            this.cmboxStatus.Items.AddRange(new object[] {
            "Settled",
            "Lapsed",
            "Cancelled",
            "Voided"});
            this.cmboxStatus.Location = new System.Drawing.Point(24, 8);
            this.cmboxStatus.Name = "cmboxStatus";
            this.cmboxStatus.Size = new System.Drawing.Size(88, 21);
            this.cmboxStatus.TabIndex = 1;
            this.cmboxStatus.Text = "Settled";
            // 
            // dateTimePicker1
            // 
            this.dateTimePicker1.Location = new System.Drawing.Point(120, 8);
            this.dateTimePicker1.Name = "dateTimePicker1";
            this.dateTimePicker1.Size = new System.Drawing.Size(200, 20);
            this.dateTimePicker1.TabIndex = 2;
            // 
            // btnRefresh
            // 
            this.btnRefresh.BackColor = System.Drawing.Color.Transparent;
            this.btnRefresh.Cursor = System.Windows.Forms.Cursors.Hand;
            this.btnRefresh.Location = new System.Drawing.Point(328, 8);
            this.btnRefresh.Name = "btnRefresh";
            this.btnRefresh.Size = new System.Drawing.Size(75, 23);
            this.btnRefresh.TabIndex = 3;
            this.btnRefresh.Text = "Refresh";
            this.btnRefresh.UseVisualStyleBackColor = false;
            this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click);
            // 
            // BetHistory
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.ClientSize = new System.Drawing.Size(762, 286);
            this.Controls.Add(this.btnRefresh);
            this.Controls.Add(this.dateTimePicker1);
            this.Controls.Add(this.cmboxStatus);
            this.Controls.Add(groupBox1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "BetHistory";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Bet History";
            groupBox1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Get the bet history
		/// </summary>
		private void GetBetHistory()
		{
			m_BetHistory = m_ApiAdapter.GetBetHistory(BetHistoryBetStatus.SETTLED, DateTime.Now.AddDays(-14), DateTime.Now);
			DisplayBetPage();
		}

		/// <summary>
		/// Returns a description of the bet included channel name, market id etc
		/// </summary>
		/// <param name="betItem"></param>
		/// <returns></returns>
		private string GetDescription(betHistoryBetHistoryItem betItem)
		{
			StringBuilder str = new StringBuilder();

			str.Append(betItem.selectionReference.marketReference.channelName);
			str.Append(Environment.NewLine);
			str.Append(betItem.selectionReference.selectionName);

			return str.ToString();
		}

		/// <summary>
		/// Displays the bets in the current page
		/// </summary>
		private void DisplayBetPage()
		{
			// Remove any bets which are currently displayed
			pnlBetItems.Controls.Clear();

			// Decide which buttons should be visible
			btnPrevious.Visible = (m_BetHistory.previousPage != null);
			btnNext.Visible = (m_BetHistory.nextPage != null);

			// Create a bet item for each bet in the current page
			foreach (betHistoryBetHistoryItem betItem in m_BetHistory.betHistoryItem)
			{
				// Create the bet item
				BetItem item = new BetItem(betItem.betId, GetDescription(betItem), betItem.bidType.ToString(), betItem.placedDate, betItem.price, betItem.size, betItem.profitLoss, betItem.winLose.ToString());

				// Set the location of the new bet item
				item.Location = new Point(0, pnlBetItems.Controls.Count * item.Height);

				// Display the bet item
				pnlBetItems.Controls.Add(item);
			}
		}

		/// <summary>
		/// Display the next page of the bet history
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnNext_Click(object sender, System.EventArgs e)
		{
			m_BetHistory =  m_ApiAdapter.GetBetHistory(m_BetHistory.nextPage.resource.href);
			DisplayBetPage();
		}

		/// <summary>
		/// Display the previous page of the bet history
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnPrevious_Click(object sender, System.EventArgs e)
		{
			m_BetHistory =  m_ApiAdapter.GetBetHistory(m_BetHistory.previousPage.resource.href);
			DisplayBetPage();
		}

		/// <summary>
		/// Refresh the bet history based on the search criteria
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btnRefresh_Click(object sender, System.EventArgs e)
		{
			switch(cmboxStatus.Text)
			{
				case "Settled":
					m_BetHistory = m_ApiAdapter.GetBetHistory(BetHistoryBetStatus.SETTLED, dateTimePicker1.Value, DateTime.Now);
					break;
				case "Cancelled" :
					m_BetHistory = m_ApiAdapter.GetBetHistory(BetHistoryBetStatus.CANCELLED, dateTimePicker1.Value, DateTime.Now);
					break;
				case "Lapsed" :
					m_BetHistory = m_ApiAdapter.GetBetHistory(BetHistoryBetStatus.LAPSED, dateTimePicker1.Value, DateTime.Now);
					break;
				case "Voided" :
					m_BetHistory = m_ApiAdapter.GetBetHistory(BetHistoryBetStatus.VOIDED, dateTimePicker1.Value, DateTime.Now);
					break;
			}

			DisplayBetPage();
		}
	}
}
