using MainShell.Common;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace MainShell.Resources.CustomControl
{
    public partial class DieMapPanelControl : UserControl
    {
        private DieMapPreviewWindow _previewWindow;

        public static readonly DependencyProperty CaptionProperty =
            DependencyProperty.Register("Caption", typeof(string), typeof(DieMapPanelControl), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty MapModelProperty =
            DependencyProperty.Register("MapModel", typeof(DieMapModel), typeof(DieMapPanelControl), new PropertyMetadata(null));

        public static readonly DependencyProperty DieSizeProperty =
            DependencyProperty.Register("DieSize", typeof(double), typeof(DieMapPanelControl), new PropertyMetadata(8.0));

        public static readonly DependencyProperty SpacingProperty =
            DependencyProperty.Register("Spacing", typeof(double), typeof(DieMapPanelControl), new PropertyMetadata(1.0));

        public static readonly DependencyProperty MapBackgroundProperty =
            DependencyProperty.Register("MapBackground", typeof(Brush), typeof(DieMapPanelControl), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(229, 231, 235))));

        public static readonly DependencyProperty CaptionForegroundProperty =
            DependencyProperty.Register("CaptionForeground", typeof(Brush), typeof(DieMapPanelControl), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(107, 114, 128))));

        public static readonly DependencyProperty MapMarginProperty =
            DependencyProperty.Register("MapMargin", typeof(Thickness), typeof(DieMapPanelControl), new PropertyMetadata(new Thickness(8)));

        public static readonly DependencyProperty CaptionMarginProperty =
            DependencyProperty.Register("CaptionMargin", typeof(Thickness), typeof(DieMapPanelControl), new PropertyMetadata(new Thickness(0, 0, 0, 14)));

        public static readonly DependencyProperty DieClickedCommandProperty =
            DependencyProperty.Register("DieClickedCommand", typeof(ICommand), typeof(DieMapPanelControl), new PropertyMetadata(null));

        public string Caption
        {
            get
            {
                return (string)GetValue(CaptionProperty);
            }
            set
            {
                SetValue(CaptionProperty, value);
            }
        }

        public DieMapModel MapModel
        {
            get
            {
                return (DieMapModel)GetValue(MapModelProperty);
            }
            set
            {
                SetValue(MapModelProperty, value);
            }
        }

        public double DieSize
        {
            get
            {
                return (double)GetValue(DieSizeProperty);
            }
            set
            {
                SetValue(DieSizeProperty, value);
            }
        }

        public double Spacing
        {
            get
            {
                return (double)GetValue(SpacingProperty);
            }
            set
            {
                SetValue(SpacingProperty, value);
            }
        }

        public Brush MapBackground
        {
            get
            {
                return (Brush)GetValue(MapBackgroundProperty);
            }
            set
            {
                SetValue(MapBackgroundProperty, value);
            }
        }

        public Brush CaptionForeground
        {
            get
            {
                return (Brush)GetValue(CaptionForegroundProperty);
            }
            set
            {
                SetValue(CaptionForegroundProperty, value);
            }
        }

        public Thickness MapMargin
        {
            get
            {
                return (Thickness)GetValue(MapMarginProperty);
            }
            set
            {
                SetValue(MapMarginProperty, value);
            }
        }

        public Thickness CaptionMargin
        {
            get
            {
                return (Thickness)GetValue(CaptionMarginProperty);
            }
            set
            {
                SetValue(CaptionMarginProperty, value);
            }
        }

        public ICommand DieClickedCommand
        {
            get
            {
                return (ICommand)GetValue(DieClickedCommandProperty);
            }
            set
            {
                SetValue(DieClickedCommandProperty, value);
            }
        }

        public DieMapPanelControl()
        {
            InitializeComponent();
        }

        private void OpenPreviewWindow(object sender, RoutedEventArgs e)
        {
            if (_previewWindow == null)
            {
                _previewWindow = CreatePreviewWindow();
                _previewWindow.Closed += OnPreviewWindowClosed;
                _previewWindow.Show();
                return;
            }

            if (_previewWindow.WindowState == WindowState.Minimized)
            {
                _previewWindow.WindowState = WindowState.Normal;
            }

            _previewWindow.Activate();
        }

        private DieMapPreviewWindow CreatePreviewWindow()
        {
            DieMapPreviewWindow previewWindow = new DieMapPreviewWindow();
            Window owner = Window.GetWindow(this);
            if (owner != null && !ReferenceEquals(owner, previewWindow))
            {
                previewWindow.Owner = owner;
            }

            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.CaptionProperty, CreateBinding(nameof(Caption)));
            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.MapModelProperty, CreateBinding(nameof(MapModel)));
            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.MapBackgroundProperty, CreateBinding(nameof(MapBackground)));
            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.DieSizeProperty, CreateBinding(nameof(DieSize)));
            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.SpacingProperty, CreateBinding(nameof(Spacing)));
            BindingOperations.SetBinding(previewWindow, DieMapPreviewWindow.DieClickedCommandProperty, CreateBinding(nameof(DieClickedCommand)));
            return previewWindow;
        }

        private Binding CreateBinding(string propertyName)
        {
            return new Binding(propertyName)
            {
                Source = this,
                Mode = BindingMode.OneWay
            };
        }

        private void OnPreviewWindowClosed(object sender, EventArgs e)
        {
            if (_previewWindow == null)
            {
                return;
            }

            _previewWindow.Closed -= OnPreviewWindowClosed;
            _previewWindow = null;
        }
    }
}
