using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Machinations.FromFlash.machinations.model;

namespace Machinations.GUI
{
    /// <summary>
    /// view class for register
    /// </summary>
    public class RegisterView: NodeView<Register>
    {
        private TextBlock m_registerTextBlock;
        

        public RegisterView(Register node) : base(node)
        {
            node.Changed += (sender, args) => RefreshText();
        }

        public override int Radius { get { return 30; } }

        public bool Interactive
        {
            get
            {
                return m_model.ActivationMode == FromFlash.machinations.model.ActivationMode.Interactive;
            }
            set
            {
                m_model.ActivationMode = value
                    ? FromFlash.machinations.model.ActivationMode.Interactive
                    : FromFlash.machinations.model.ActivationMode.Passive;
                RefreshText();
            }
        }

        

        private double Side { get { return Math.Sqrt(Radius * Radius / 2); } }

        public override void InitShape()
        {
            Rectangle r = new Rectangle {Fill = new SolidColorBrush(m_model.Color), Width = Side, Height = Side};
            m_uiElement = r;
            m_registerTextBlock = new TextBlock();
            m_registerTextBlock.Foreground = new SolidColorBrush(Colors.White);
            RefreshText();
        }

        public void RefreshText()
        {
            m_registerTextBlock.Text = Interactive||Model.Value != 0 ? Model.Value.ToString() : "X";
        }

        protected override void SetMainUiElementPosition(Point value)
        {
            m_uiElement.SetValue(Canvas.LeftProperty, value.X - Side/2);
            m_uiElement.SetValue(Canvas.TopProperty, value.Y - Side/2);
            m_registerTextBlock.SetValue(Canvas.LeftProperty, value.X - Side / 5);
            m_registerTextBlock.SetValue(Canvas.TopProperty, value.Y - Side / 2.5);
        }

        public override void ColorView(Brush brush)
        {
            ((Rectangle) m_uiElement).Fill = brush;
        }

        public override List<UIElement> GetAllUiElements()
        {
            var basic = base.GetAllUiElements();
            basic.Add(m_registerTextBlock);
            return basic;
        }

        public void Click(Point clicked)
        {
            Register model = m_model;
            if (!Interactive)
            {
                return;
            }
            if (Position.Y > clicked.Y)
            {
                model.Interaction += model.ValueStep;
            }
            else
            {
                model.Interaction -= model.ValueStep;
            }
        }
    }



    //public class RegisterUiElement : UIElement
    //{
    //    private int m_radius;
    //    private Brush m_brush;
    //    private bool m_interactive;
    //    private int m_value;

    //    public RegisterUiElement(int radius, Brush brush = null)
    //    {
    //        m_radius = radius;
    //        m_brush = brush ?? Brushes.Black;
    //    }

    //    public Brush Brush
    //    {
    //        get { return m_brush; }
    //        set
    //        {
    //            m_brush = value;
    //            InvalidateVisual();
    //        }
    //    }

    //    public bool Interactive
    //    {
    //        get { return m_interactive; }
    //        set
    //        {
    //            m_interactive = value;
    //            InvalidateVisual();
    //        }
    //    }

    //    public int Value
    //    {
    //        get { return m_value; }
    //        set
    //        {
    //            m_value = value;
    //            InvalidateVisual();
    //        }
    //    }

    //    protected override void OnRender(DrawingContext drawingContext)
    //    {
    //        double side = Math.Sqrt(m_radius * m_radius / 2);
    //        drawingContext.DrawRectangle(m_brush, null, new Rect(new Size(side,side)));
    //        Typeface typeface = new Typeface("Times New Roman");
    //        Brush whiteBrush = new SolidColorBrush(Colors.White);
    //        if (!m_interactive)
    //        {
    //            FormattedText text = new FormattedText("X", CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
    //                typeface, m_radius/3, whiteBrush);
    //            drawingContext.DrawText(text, new Point());
    //        }
    //        else
    //        {
    //            FormattedText text = new FormattedText(m_value.ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
    //                typeface, m_radius / 3, whiteBrush);
    //            drawingContext.DrawText(text, new Point());
    //            Point upper = new Point(0, -m_radius / 2);
    //            Point left = new Point(-m_radius / 4, -m_radius * 2 / 5);
    //            Point right = new Point(-m_radius / 4, m_radius * 2 / 5);
    //            Pen pen = new Pen(whiteBrush,2);
    //            drawingContext.DrawLine(pen, left, upper);
    //            drawingContext.DrawLine(pen, right, upper);
    //        }
    //        base.OnRender(drawingContext);
    //    }
    //}
}
