using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

using Betfair.Gamex.API.Entities;

using Betfair.Gamex.API.UberClient.Controls;
using Betfair.Gamex.API.UberClient.Classes;

namespace Betfair.Gamex.API.UberClient.Controls.GameView
{
    public partial class GameViewBase : UserControl
    {
        protected static Font fn = new Font("Tahoma", 7, FontStyle.Regular);
        protected static Font fb = new Font("Tahoma", 7, FontStyle.Bold);

        private static Assembly ass;

        public GameViewBase()
        {
            InitializeComponent();
        }

        protected void SetOddsPanel(OddsPanel oddsPanel, int marketID, selectionSnapshot selection)
        {
            oddsPanel.MarketID = marketID;
            oddsPanel.Tag = selection;

            if (oddsPanel.Back == true)
            {
                if (selection.bestAvailableToBackPrices.Length > 0)
                {
                    oddsPanel.Price = selection.bestAvailableToBackPrices[0].Value;
                    oddsPanel.Available = selection.bestAvailableToBackPrices[0].amountUnmatched;
                }
                else
                {
                    oddsPanel.Clear();
                }
            }
            else
            {
                if (selection.bestAvailableToLayPrices.Length > 0)
                {
                    oddsPanel.Price = selection.bestAvailableToLayPrices[0].Value;
                    oddsPanel.Available = selection.bestAvailableToLayPrices[0].amountUnmatched;
                }
                else
                {
                    oddsPanel.Clear();
                }
            }
        }

        protected void SetPlayerStatus(selectionStatusEnum status, Label lblStatus, IOddsPanel backOddsPanel, IOddsPanel layOddsPanel)
        {
            switch (status)
            {
                case selectionStatusEnum.WINNER:
                    lblStatus.Text = "Winner";
                    lblStatus.BackColor = Color.Navy;
                    backOddsPanel.Eliminated = true;
                    layOddsPanel.Eliminated = true;
                    break;
                case selectionStatusEnum.TIED_DEAD_HEAT:
                    lblStatus.Text = "Tied";
                    lblStatus.BackColor = Color.Navy;
                    break;
                case selectionStatusEnum.LOSER:
                    lblStatus.Text = "Eliminated";
                    lblStatus.BackColor = Color.Red;
                    backOddsPanel.Eliminated = true;
                    layOddsPanel.Eliminated = true;
                    break;
                case selectionStatusEnum.CAN_NOT_WIN:
                    lblStatus.Text = "Can't Win";
                    lblStatus.BackColor = Color.Red;
                    backOddsPanel.Eliminated = true;
                    layOddsPanel.Eliminated = true;
                    break;
                case selectionStatusEnum.CAN_NOT_LOSE:
                    lblStatus.Text = "Can't Lose";
                    lblStatus.BackColor = Color.Navy;
                    backOddsPanel.Eliminated = true;
                    layOddsPanel.Eliminated = true;
                    break;
                default:
                    lblStatus.Text = "Active";
                    lblStatus.BackColor = Color.Green;
                    backOddsPanel.Eliminated = false;
                    layOddsPanel.Eliminated = false;
                    break;
            }
        }

        protected void UpdateProgressBar(channelSnapshot channel, PictureBox progressGrey, PictureBox progressBar)
        {
            // set betting window progress indicator
            progressGrey.Height = int.Parse(String.Format("{0:0.}", channel.channel.game.bettingWindowPercentageComplete / 100d * progressBar.Height));
        }

        protected void SelectionClicked(ShowBetPlaceHandler handler, object sender, channelSnapshot channel, accountSnapshot account)
        {
            if (handler != null)
            {
                OddsPanel oddsPanel = sender as OddsPanel;
                selectionSnapshot selection = oddsPanel.Tag as selectionSnapshot;

                betBidTypeEnum bidType;
                if (oddsPanel.Back == true)
                {
                    bidType = betBidTypeEnum.BACK;
                }
                else
                {
                    bidType = betBidTypeEnum.LAY;
                }

                handler(new Bet(bidType, selection.id, oddsPanel.MarketID, channel.channel.game.round, selection.name, account.currency, oddsPanel.Price));
            }
        }

        protected void SetPlayerDescription(string description, Label lblDescription)
        {
            if (description.Length > 0 && description.IndexOf("NOT AVAILABLE") == -1)
            {
                lblDescription.Text = description;
            }
            else
            {
                lblDescription.Text = string.Empty;
            }
        }

        protected Bitmap GetCardResourceImage(string propertyValue, bool large)
        {
            string card = "99";
            if (propertyValue != null && propertyValue.IndexOf("NOT AVAILABLE") == -1)
            {
                card = propertyValue;
            }

            if (ass == null)
            {
                ass = Assembly.Load("Betfair.Gamex.API.UberClient");
            }

            string resource = string.Format("Betfair.Gamex.API.UberClient.Resources.Images.Cards.{0}.card{1}.{2}", (large ? "Large" : "Small"), card, (large ? "png" : "gif"));
            return new Bitmap(ass.GetManifestResourceStream(resource));
        }
    }
}
