using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace StockPortfolio
{
    public partial class SymbolButton : UserControl
    {
        bool _updating = false;

        public SymbolButton()
        {
            InitializeComponent();

            IsEnabled = true;

            Loaded += new RoutedEventHandler(SymbolButton_Loaded);
            chk.Checked += new RoutedEventHandler(chk_Checked);
            chk.Unchecked += new RoutedEventHandler(chk_Checked);
        }

        void SymbolButton_Loaded(object sender, RoutedEventArgs e)
        {
            UpdateVisualState();
        }

        private void UpdateVisualState()
        {
            _updating = true;
            lblPortfolioName1b.Text = _text;
            chk.IsChecked = _isChecked;
            SolidColorBrush brush = new SolidColorBrush(_color);
            rectPoint.Fill = brush;
            lblPortfolioName1b.Foreground = brush;
            _updating = false;
        }

        private bool undoing = false;
        void chk_Checked(object sender, RoutedEventArgs e)
        {
            if (!_updating)
            {
                _isChecked = chk.IsChecked;
                if (CheckedChanged != null)
                {
                    CheckedChanged(this, new EventArgs());
                }
            }
        }

        private string _text;
        private Color _color;
        private bool? _isChecked;
        private bool _isEnabled = true;

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { _isEnabled = value;
            chk.IsEnabled = value;
            }
        }


        public bool? IsChecked
        {
            get { return _isChecked; }
            set { _isChecked = value; }
        }

        public string Text
        {
            get { return _text; }
            set
            {
                _text = value; UpdateVisualState();
            }
        }

        public Color Color
        {
            get { return _color; }
            set
            {
                _color = value; UpdateVisualState();
            }
        }

        public event EventHandler CheckedChanged;
    }
}
