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

namespace Machinations.GUI
{
    /// <summary>
    /// view class for pool node.
    /// </summary>
    public class PoolView: NodeView<Pool>
    {
        private TextBlock m_resourcesTextBlock;
        private Dictionary<Color, int> m_lastColoring;

        private void ModelOnChanged(object sender, EventArgs eventArgs)
        {
            var newColoring = GetResources().ToDictionary(pair => pair.Key, pair => pair.Value);
            if (!m_lastColoring.Equals(newColoring))
            {
                m_lastColoring = newColoring;
                ColorInside();
            }
        }

        public PoolView(Pool node) : base(node)
        {
            m_model.Changed += ModelOnChanged;
        }

        public int ResourcesCount
        {
            get
            {
                return (m_model).Resources.Count;
            }
            set
            {
                (m_model).StartingResources = value;
                m_lastColoring = new Dictionary<Color, int> {{Model.Color, value}};
                ColorInside();
            }
        }

        public override void InitShape()
        {
            Border border = new Border
            {
                CornerRadius = new CornerRadius(Radius),
                Width = Radius*1,
                Height = Radius*1,
                Background = Brushes.Transparent,
                BorderBrush = new SolidColorBrush(m_model.Color),
                BorderThickness = new Thickness(m_model.Thickness)
            };
            m_uiElement = border;
            m_lastColoring = GetResources();
            m_resourcesTextBlock = new TextBlock
            {
                //Text = "ahoj",
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                Background = ColorsUtils.ConnectionTextBrush
            };
            border.Child = m_resourcesTextBlock;
            ColorInside();
        }


        private void ColorInside()
        {
            m_resourcesTextBlock.Inlines.Clear();
            if (m_lastColoring.Any())
            {
                foreach (var pair in m_lastColoring)
                {
                    string text = (pair.Equals(m_lastColoring.Last())) ? pair.Value+"" : pair.Value + ",";
                        m_resourcesTextBlock.Inlines.Add(new Run
                        {
                        Text = text,
                        Foreground = new SolidColorBrush(pair.Key)
                        
                    });
                }
            }
            else
            {
                string text = Model.Shotage != 0?  "-" + Model.Shotage : "0";
                m_resourcesTextBlock.Inlines.Add(new Run
                {
                    Text = text,
                    Foreground = new SolidColorBrush(Model.Color)
                });
            } 
        }

        //private void OneColor()
        //{
        //    m_textBlock.Text = Model.Resources.Count() + "";

        //    if (m_lastColoring.Count() == 1 && m_lastColoring.Single().Key != Colors.Transparent)
        //    {
        //        m_textBlock.Foreground = new SolidColorBrush(m_lastColoring.First().Key);
        //    }  
        //}

        private Dictionary<Color, int> GetResources()
        {
            Dictionary<Color, int> resources = new Dictionary<Color, int>();
            Model.Resources.ForEach(resource =>
            {
                if (resource.Position == 0)
                {
                    int value;
                    if (resources.TryGetValue(resource.Color, out value))
                    {
                        resources[resource.Color]++;
                    }
                    else { resources.Add(resource.Color, 1); }
                }
            });
            return resources;
        }
    }
}
