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

namespace Machinations.GUI
{
    /// <summary>
    /// view class for simlink. Has reference for view object it represrents
    /// </summary>
    public class SimlinkView: PointObjectView
    {
        private INodeView<MachinationsNode> m_realView;
        private SimlinkModel m_model;


        public SimlinkView(INodeView<MachinationsNode> realView, SimlinkModel model)
        {
            m_realView = realView;
            m_model = model;
            m_uiElement = new Border()
            {
                CornerRadius = new CornerRadius(Radius),
                Width = Radius,
                Height = Radius,
                Background = Brushes.Transparent,
                BorderBrush = new SolidColorBrush(model.Node.Color),
                BorderThickness = new Thickness(2)
            };
        }

        public override Point Position
        {
            get
            {
                return m_position;
            }
            set
            {
                if (!value.Equals(m_position))
                {
                    base.Position = value;
                    m_model.Position = value;
                }
            }
        }

        public SimlinkModel Model
        {
            get { return m_model; }
        }

        public override string Label
        {
            get { return m_textBlock.Text; }
            set
            {
                if (value == "")
                    base.Label = "";
                else
                    base.Label ="l:" + value;
                UpdateLabelPosition();
            }
        }

        public override Color BaseColor
        {
            get { return m_model.Node.Color; }
        }

        public override ViewSelectionMode Selected
        {
            set
            {
                    base.Selected = value;
                    if(m_realView.Selected != m_selected)
                        m_realView.Selected = value;    //mel by si hlidat aby se nemenil pokud uz je
            }
        }

        public override ViewHighlightMode Highlighted
        {
            set
            {
                base.Highlighted = value;
                if (m_realView.Highlighted != m_highlighted)
                    m_realView.Highlighted = value;
            }
        }

        public INodeView<MachinationsNode> RealView
        {
            get { return m_realView; }
        }

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


        public override void ColorView(Brush brush)
        {
            ((Border)m_uiElement).BorderBrush = brush;
        }

        public override GraphElement GetModelElement()
        {
            return m_model.Node;
        }

        public override void AddConnection(IConnectionView connectionView, bool start, bool withModel = true)
        {
            base.AddConnection(connectionView, start);
            if (withModel)
            {
                m_model.Connections.Add(connectionView.Model);
                connectionView.Model.ConnectToElement(m_model.Node, start);
            }
        }

        public override void RemoveConnection(IConnectionView connection, bool start)
        {
            base.RemoveConnection(connection, start);
            m_model.Connections.Remove(connection.Model);
        }

        public override void Dispose()
        {
            base.Dispose();
            m_model.Dispose();
        }

        protected override Brush GetBrushForNormalSelection()
        {
            if (Model.Node.Inhibited) return Brushes.LightGray;
            return new SolidColorBrush(Model.Node.Color);
        }

        public SimlinkView CreateSimlinkView()
        {
            var newSimlink = m_realView.CreateSimlinkView();
            newSimlink.Position = new Point(Position.X, Position.Y + 20);
            return newSimlink;
        }
    }
}
