using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using C1.Silverlight.Legacy;

namespace StockPortfolio
{
    public class ActiveStockButton : C1ControlBase
    {
        C1CheckBox _chkbox;
        Rectangle _rectangleColor;
        C1Label _lblSym;

        public ActiveStockButton()
        {
            _chkbox = new C1CheckBox();
            _chkbox.ReplacePlaceHolder(FindName("CheckBox"),false);
            _chkbox.CheckedChanged += new EventHandler(_chkbox_CheckedChanged);

            _rectangleColor = (Rectangle)FindName("Circle");

            _lblSym = new C1Label();
            _lblSym.ReplacePlaceHolder(FindName("Symbol"),false);
        }

        protected override ResourceInfo ResourceName
        {
            get { return new ResourceInfo("ActiveStockButton.xaml", this.GetType().Assembly); }
        }

        #region object model

        /// <summary>
        /// The text of the stock symbol
        /// </summary>
        public override string Text
        {
            get { return _lblSym.Text; }
            set { _lblSym.Text = value; }
        }

        /// <summary>
        /// Color representing this symbol
        /// </summary>
        public Brush SymbolColor
        {
            get { return _rectangleColor.Fill; }
            set
            {
                // the brush should be cloned
                if (value is SolidColorBrush)
                {
                    Color clr = ((SolidColorBrush)value).Color;
                    _rectangleColor.Fill = new SolidColorBrush(clr);
                    _lblSym.TextBlock.Foreground = new SolidColorBrush(clr);
                }
                else
                    throw new NotSupportedException("Only solid brushes are supported.");
            }
        }

        public bool Check
        {
            get { return _chkbox.Checked; }
            set { _chkbox.Checked = value; }
        }

        public event EventHandler CheckedChanged;
        void _chkbox_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckedChanged != null)
                CheckedChanged(this, e);
        }

        #endregion
    }
}
