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

using Betfair.Gamex.API.UberClient.Classes;

namespace Betfair.Gamex.API.UberClient.Controls
{
	/// <summary>
	/// Summary description for ctlBetPanel.
	/// </summary>
	public class BetPanel : System.Windows.Forms.UserControl, IBetPanel
    {
		private System.Windows.Forms.Label lblLiability;
		private System.Windows.Forms.Label lblSelection;

        private bool back = true;
		private Bet m_bet;
		private System.Windows.Forms.Label lblProfit;
		private System.Windows.Forms.PictureBox pbClose;
		private System.Windows.Forms.NumericUpDown txtPrice;
		private Betfair.Gamex.API.UberClient.Controls.NumericEdit txtSize;
        private Assembly thisExe = Assembly.GetExecutingAssembly();

		public event CancelBetHandler CancelBet;

		public BetPanel(Bet bet)
		{
			InitializeComponent();

			m_bet = bet;
			if (m_bet.price == 0)
			{
				m_bet.price = double.Parse(txtPrice.Value.ToString());
			}

			updateDisplay();

            // insert default stake if nothing set
            if (string.IsNullOrEmpty(txtSize.Text) == true)
            {
                txtSize.Text = ConfigurationManager.AppSettings["DefaultStake"];
            }
		}

        /// <summary>
        /// Mode the panel is in (back/lay)
        /// </summary>
        public bool Back
        {
            get
            {
                return back;
            }
            set
            {
                back = value;
            }
        }

		private void updateDisplay()
		{
			lblSelection.Text = m_bet.SelectionName;
			this.Tag = m_bet.selectionId;

			txtPrice.ValueChanged += null;
			txtPrice.Text = m_bet.price.ToString("0.00");
			txtPrice.ValueChanged += new EventHandler(txtPrice_ValueChanged);

			txtSize.TextChanged += null;
			txtSize.Text = m_bet.size.ToString("0.00");
			txtSize.TextChanged += new EventHandler(txtSize_TextChanged);

            if (string.IsNullOrEmpty(txtPrice.Text) == false && string.IsNullOrEmpty(txtSize.Text) == false)
            {
                _UpdateProfitAndLiability();
            }
		}

		#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()
		{
            System.Windows.Forms.Label label1;
            System.Windows.Forms.Label label2;
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BetPanel));
            this.lblSelection = new System.Windows.Forms.Label();
            this.lblLiability = new System.Windows.Forms.Label();
            this.lblProfit = new System.Windows.Forms.Label();
            this.txtPrice = new System.Windows.Forms.NumericUpDown();
            this.pbClose = new System.Windows.Forms.PictureBox();
            this.txtSize = new Betfair.Gamex.API.UberClient.Controls.NumericEdit();
            label1 = new System.Windows.Forms.Label();
            label2 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.txtPrice)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbClose)).BeginInit();
            this.SuspendLayout();
            // 
            // lblSelection
            // 
            this.lblSelection.BackColor = System.Drawing.Color.Transparent;
            this.lblSelection.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblSelection.ForeColor = System.Drawing.Color.Black;
            this.lblSelection.Location = new System.Drawing.Point(0, 0);
            this.lblSelection.Name = "lblSelection";
            this.lblSelection.Size = new System.Drawing.Size(63, 40);
            this.lblSelection.TabIndex = 15;
            this.lblSelection.Text = "Selection";
            this.lblSelection.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.BackColor = System.Drawing.Color.Transparent;
            label1.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label1.ForeColor = System.Drawing.Color.Black;
            label1.Location = new System.Drawing.Point(64, 2);
            label1.Name = "label1";
            label1.Size = new System.Drawing.Size(25, 11);
            label1.TabIndex = 16;
            label1.Text = "Price";
            label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // label2
            // 
            label2.AutoSize = true;
            label2.BackColor = System.Drawing.Color.Transparent;
            label2.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            label2.ForeColor = System.Drawing.Color.Black;
            label2.Location = new System.Drawing.Point(116, 2);
            label2.Name = "label2";
            label2.Size = new System.Drawing.Size(29, 11);
            label2.TabIndex = 17;
            label2.Text = "Stake";
            label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lblLiability
            // 
            this.lblLiability.AutoSize = true;
            this.lblLiability.BackColor = System.Drawing.Color.Transparent;
            this.lblLiability.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblLiability.ForeColor = System.Drawing.Color.Red;
            this.lblLiability.Location = new System.Drawing.Point(153, 23);
            this.lblLiability.Name = "lblLiability";
            this.lblLiability.Size = new System.Drawing.Size(23, 12);
            this.lblLiability.TabIndex = 18;
            this.lblLiability.Text = "0.00";
            this.lblLiability.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // lblProfit
            // 
            this.lblProfit.AutoSize = true;
            this.lblProfit.BackColor = System.Drawing.Color.Transparent;
            this.lblProfit.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblProfit.ForeColor = System.Drawing.Color.Green;
            this.lblProfit.Location = new System.Drawing.Point(153, 6);
            this.lblProfit.Name = "lblProfit";
            this.lblProfit.Size = new System.Drawing.Size(23, 11);
            this.lblProfit.TabIndex = 19;
            this.lblProfit.Text = "0.00";
            this.lblProfit.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // txtPrice
            // 
            this.txtPrice.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtPrice.DecimalPlaces = 2;
            this.txtPrice.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtPrice.Increment = new decimal(new int[] {
            1,
            0,
            0,
            131072});
            this.txtPrice.Location = new System.Drawing.Point(65, 15);
            this.txtPrice.Maximum = new decimal(new int[] {
            1000,
            0,
            0,
            0});
            this.txtPrice.Minimum = new decimal(new int[] {
            101,
            0,
            0,
            131072});
            this.txtPrice.Name = "txtPrice";
            this.txtPrice.Size = new System.Drawing.Size(48, 19);
            this.txtPrice.TabIndex = 21;
            this.txtPrice.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtPrice.Value = new decimal(new int[] {
            101,
            0,
            0,
            131072});
            // 
            // pbClose
            // 
            this.pbClose.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
            this.pbClose.Cursor = System.Windows.Forms.Cursors.Hand;
            this.pbClose.Image = ((System.Drawing.Image)(resources.GetObject("pbClose.Image")));
            this.pbClose.Location = new System.Drawing.Point(188, 3);
            this.pbClose.Name = "pbClose";
            this.pbClose.Size = new System.Drawing.Size(10, 8);
            this.pbClose.TabIndex = 23;
            this.pbClose.TabStop = false;
            this.pbClose.Click += new System.EventHandler(this.pbClose_Click);
            // 
            // txtSize
            // 
            this.txtSize.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtSize.Font = new System.Drawing.Font("Tahoma", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtSize.InputType = Betfair.Gamex.API.UberClient.Controls.NumericEdit.NumEditType.Currency;
            this.txtSize.Location = new System.Drawing.Point(115, 15);
            this.txtSize.Max = 10000F;
            this.txtSize.Name = "txtSize";
            this.txtSize.Size = new System.Drawing.Size(34, 19);
            this.txtSize.TabIndex = 22;
            this.txtSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // ctlBetPanel
            // 
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.Controls.Add(this.pbClose);
            this.Controls.Add(this.txtSize);
            this.Controls.Add(this.txtPrice);
            this.Controls.Add(this.lblProfit);
            this.Controls.Add(this.lblLiability);
            this.Controls.Add(label2);
            this.Controls.Add(this.lblSelection);
            this.Controls.Add(label1);
            this.Name = "ctlBetPanel";
            this.Size = new System.Drawing.Size(201, 40);
            this.Load += new System.EventHandler(this.ctlBetBackPanel_Load);
            ((System.ComponentModel.ISupportInitialize)(this.txtPrice)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pbClose)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		[Browsable(false)]
		public double BetSize
		{
			get 
			{
				m_bet.size = double.Parse(txtSize.Text);
				return m_bet.size; 
			}
			set 
			{
				m_bet.size = value;
				txtSize.Text = value.ToString();
			}
		}

		#region IBetPanel Members

		public Bet bet
		{
			get { return m_bet; }
			set { m_bet = value; }
		}

		#endregion

		private void pbClose_Click(object sender, EventArgs e)
		{
            if (CancelBet != null)
            {
                CancelBet(this.bet);
            }
		}

		private void txtSize_TextChanged(object sender, System.EventArgs e)
		{
            if (string.IsNullOrEmpty(txtPrice.Text) == true || string.IsNullOrEmpty(txtSize.Text) == true)
            {
                lblProfit.Text = "0.00";
                lblLiability.Text = "0.00";
                return;
            }

			m_bet.size = double.Parse(txtSize.Text);

			// If we are changing this and the bet has already been placed
			// then this is now an edit
            if (m_bet.Status == BetStatus.Placed)
            {
                m_bet.Status = BetStatus.Edited;
            }

            _UpdateProfitAndLiability();
		}

		private void txtPrice_ValueChanged(object sender, System.EventArgs e)
		{
            if (string.IsNullOrEmpty(txtPrice.Text) == true || string.IsNullOrEmpty(txtSize.Text) == true)
            {
                lblProfit.Text = "0.00";
                lblLiability.Text = "0.00";
                return;
            }

            m_bet.price = Convert.ToDouble(txtPrice.Value);

			// If we are changing this and the bet has already been placed
			// then this is now an edit
            if (m_bet.Status == BetStatus.Placed)
            {
                m_bet.Status = BetStatus.Edited;
            }

            _UpdateProfitAndLiability();
		}

        private void _UpdateProfitAndLiability()
        {
            if (m_bet.bidType == Betfair.Gamex.API.Entities.betBidTypeEnum.BACK)
            {
                double profit = Math.Round(Convert.ToDouble(txtSize.Text) * (Convert.ToDouble(txtPrice.Value) - 1.0), 2);
                lblProfit.Text = Convert.ToString(profit);
                lblLiability.Text = txtSize.Text;
            }
            else
            {
                lblProfit.Text = txtSize.Text;
                lblLiability.Text = Convert.ToString(Math.Round(Convert.ToDouble(txtSize.Text) * (Convert.ToDouble(txtPrice.Value) - 1.0), 2));
            }
        }

		public void ToggleControls(bool enabled)
		{
			txtPrice.Enabled = enabled;
			txtSize.Enabled = enabled;
			pbClose.Visible = enabled;
		}

        private void ctlBetBackPanel_Load(object sender, EventArgs e)
        {
            if (back == true)
            {
                this.BackgroundImage = new Bitmap(thisExe.GetManifestResourceStream("Betfair.Gamex.API.UberClient.Images.bgBackBig.gif"));
            }
            else
            {
                this.BackgroundImage = new Bitmap(thisExe.GetManifestResourceStream("Betfair.Gamex.API.UberClient.Images.bgLayBig.gif"));
            }
        }
	}
}
