using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using Betfair.Gamex.API.UberClient.Classes;

using Betfair.Gamex.API;
using Betfair.Gamex.API.Entities;

namespace Betfair.Gamex.API.UberClient.Controls
{
	/// <summary>
	/// Displays currently matched bets
	/// </summary>
	public class MyBetsPanel : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Panel pnlBetPanel;
        private Adapter m_ApiAdapter;
		private betSnapshot m_Bets;

        public Adapter Adapter
		{
			get{return m_ApiAdapter;}
			set{m_ApiAdapter = value;}
		}

		public betSnapshot Bets
		{
			get{return m_Bets;}
			set
			{
				m_Bets = value;
				if(m_Bets != null)
					DisplayBets();
			}
		}

		public MyBetsPanel()
		{
			InitializeComponent();
		}

		/// <summary>
		/// Display all the current matched bets
		/// </summary>
		private void DisplayBets()
		{
			pnlBetPanel.Controls.Clear();

            if (m_Bets.betSnapshotItem == null)
            {
                return;
            }

            foreach (betSnapshotBetSnapshotItem bet in m_Bets.betSnapshotItem)
			{
				DrawBet(bet);
			}
		}

		/// <summary>
		/// Creates a control for a bet and adds it to the panel
		/// </summary>
		private void DrawBet(betSnapshotBetSnapshotItem bet)
		{
			Bet b = new Bet();
			b.bidType = bet.bidType;
			b.Currency = "GBP";
			b.MarketID = bet.selectionReference.marketReference.marketId;
			b.price = bet.price;
			//b.Round
			b.selectionId = bet.selectionReference.selectionId;
			b.SelectionName = bet.selectionReference.selectionName;
			b.size = bet.size;

            BetPanel betPanel = new BetPanel(b);
            if (b.bidType == betBidTypeEnum.BACK)
			{
                betPanel.Back = true;
			}
			else
			{
                betPanel.Back = false;
            }

            betPanel.Location = new Point(0, pnlBetPanel.Controls.Count * betPanel.Height);
            pnlBetPanel.Controls.Add(betPanel);
            betPanel.ToggleControls(false);
        }

		#region Component 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()
		{
            this.pnlBetPanel = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // pnlBetPanel
            // 
            this.pnlBetPanel.AutoScroll = true;
            this.pnlBetPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlBetPanel.Location = new System.Drawing.Point(0, 0);
            this.pnlBetPanel.Name = "pnlBetPanel";
            this.pnlBetPanel.Size = new System.Drawing.Size(217, 351);
            this.pnlBetPanel.TabIndex = 0;
            // 
            // ctlMyBets
            // 
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(215)))), ((int)(((byte)(236)))));
            this.Controls.Add(this.pnlBetPanel);
            this.Location = new System.Drawing.Point(430, 46);
            this.Name = "ctlMyBets";
            this.Size = new System.Drawing.Size(217, 351);
            this.ResumeLayout(false);

		}
		#endregion
	}
}
