using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Reflection;
using Betfair.Gamex.API.UberClient.Classes;

namespace Betfair.Gamex.API.UberClient.Controls
{
	/// <summary>
	/// Shows market price and available money for a selection
	/// </summary>
	public class OddsPanel : System.Windows.Forms.UserControl, IOddsPanel
	{
		private System.Windows.Forms.Label lblOdds;
		private System.Windows.Forms.Label lblAvailable;
        private Assembly thisExe = Assembly.GetExecutingAssembly();

        private int m_MarketID = 0;
        private bool eliminated = false;
        private bool back = true;

		public OddsPanel()
		{
			InitializeComponent();
		}

        private void ctlOddsPanel_Load(object sender, EventArgs e)
        {
            if (back == true)
            {
                _SetBackImage();
            }
            else
            {
                _SetLayImage();
            }
        }

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

        [Browsable(false)]
        public int MarketID
        {
            get
            {
                return m_MarketID;
            }
            set
            {
                m_MarketID = value; 
            }
        }

        [Browsable(false)]
        public double Price
        {
            get
            {
                if (lblOdds.Text != "Ask" && lblOdds.Text != "Offer")
                {
                    return double.Parse(lblOdds.Text);
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                if (this.Eliminated == false)
                {
                    if (value != 0)
                    {
                        lblOdds.Text = Util.FormatOdds(value);
                    }
                }
            }
        }

        [Browsable(false)]
        public double Available
        {
            get
            {
                if (lblAvailable.Text.Length > 0)
                {
                    return (double.Parse(lblAvailable.Text));
                }
                else
                {
                    return 0;
                }
            }
            set
            {
                if (this.Eliminated == false)
                {
                    if (value != 0)
                    {
                        lblAvailable.Text = String.Format("{0:0.}", value);
                    }
                }
            }
        }

        /// <summary>
        /// Whether or not panel is enabled - greyed out between rounds for example
        /// </summary>
		public new bool Enabled
		{
            get
            {
                return base.Enabled;
            }
            set
			{
                // do nothing if value is already set
                if (base.Enabled == value)
                {
                    return;
                }

                // dont allow value to be set if this selection is eliminated
                if (this.Eliminated == true)
                {
                    return;
                }

				base.Enabled = value;

				if (value == false)
				{
                    _SetGreyImage();
				}
				else
				{
                    if (back == true)
                    {
                        _SetBackImage();
                    }
                    else
                    {
                        _SetLayImage();
                    }
                }
			}
		}

        private void _SetGreyImage()
        {
            this.BackgroundImage = new Bitmap(thisExe.GetManifestResourceStream("Betfair.Gamex.API.UberClient.Images.bgBack_grey.gif"));
        }

        private void _SetLayImage()
        {
            this.BackgroundImage = new Bitmap(thisExe.GetManifestResourceStream("Betfair.Gamex.API.UberClient.Images.bgLay.gif"));
        }

        private void _SetBackImage()
        {
            this.BackgroundImage = new Bitmap(thisExe.GetManifestResourceStream("Betfair.Gamex.API.UberClient.Images.bgBack.gif"));
        }

        /// <summary>
        /// Whether or not this selection is eliminated - this is effectivly an override for Enabled
        /// </summary>
        [Browsable(false)]
        public bool Eliminated
        {
            get
            {
                return eliminated;
            }
            set
            {
                // make sure we disable panel if selection is eliminated
                if (value == true)
                {
                    this.Enabled = false;
                    Clear();
                }

                eliminated = value;
            }
        }

        public void Clear()
        {
            if (back == true)
            {
                lblOdds.Text = "Ask";
            }
            else
            {
                lblOdds.Text = "Offer";
            }

            lblAvailable.Text = string.Empty;
        }

		#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.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(OddsPanel));
            this.lblOdds = new System.Windows.Forms.Label();
            this.lblAvailable = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // lblOdds
            // 
            this.lblOdds.BackColor = System.Drawing.Color.Transparent;
            this.lblOdds.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblOdds.Location = new System.Drawing.Point(2, 3);
            this.lblOdds.Name = "lblOdds";
            this.lblOdds.Size = new System.Drawing.Size(46, 13);
            this.lblOdds.TabIndex = 0;
            this.lblOdds.Text = "Ask";
            this.lblOdds.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lblOdds.Click += new System.EventHandler(this.Label_Click);
            // 
            // lblAvailable
            // 
            this.lblAvailable.BackColor = System.Drawing.Color.Transparent;
            this.lblAvailable.Font = new System.Drawing.Font("Tahoma", 6.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lblAvailable.Location = new System.Drawing.Point(2, 19);
            this.lblAvailable.Name = "lblAvailable";
            this.lblAvailable.Size = new System.Drawing.Size(46, 9);
            this.lblAvailable.TabIndex = 1;
            this.lblAvailable.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.lblAvailable.Click += new System.EventHandler(this.Label_Click);
            // 
            // OddsPanel
            // 
            this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
            this.Controls.Add(this.lblAvailable);
            this.Controls.Add(this.lblOdds);
            this.Cursor = System.Windows.Forms.Cursors.Hand;
            this.ForeColor = System.Drawing.Color.Black;
            this.Name = "OddsPanel";
            this.Size = new System.Drawing.Size(48, 31);
            this.Load += new System.EventHandler(this.ctlOddsPanel_Load);
            this.ResumeLayout(false);

		}
		#endregion

        private void Label_Click(object sender, EventArgs e)
        {
            base.OnClick(e);
        }
	}
}
