// SearchComboItem.cs
// Copyright (c) 2016 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Telerik.Windows.Controls;

// TODO 1: <whar> See if I can get Telerik debug bindings output out of the mix.

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// A user control for use in the Ribbon Search Combo drop down.
    /// </summary>
    internal class SearchComboItem : RadComboBoxItem
    {
        #region Constructor
        public SearchComboItem(IImageProvider imageProvider, RadComboBox searchCombo)
        {
            ImageProvider = imageProvider;
            SearchCombo = searchCombo;
            InitializeControl();
        }
        #endregion

        #region Public Properties
        public string SearchComboItemDisplayMember
        {
            get { return SearchCombo.Text;  }
        }
        #endregion

        #region Internal Properties
        internal RibbonSearchResultItem SearchResultItem
        {
            get { return m_searchResultItem; }
            set
            {
                m_searchResultItem = value;
                UpdateControl();
            }
        }

        internal void MakeTextOnly(string text)
        {
            m_searchResultItem = null;
            Image.Source = null;
            TextBlock.Text = text;
            IsEnabled = false;
        }
        #endregion

        #region Private Methods
        private void UpdateControl()
        {
            string resourceKey = String.Empty;

            if (SearchResultItem.RibbonLocation == RibbonLocation.DialogLauncher)
                resourceKey = "DialogLauncher"; // I18N. OK (No reference to Haestad.Framework.Windows.Forms and we don't want to add one).
            else if (SearchResultItem.AssociatedCommand is CheckedMenuRibbonUICommand)
            {
                IRibbonLinkedUICommand linkedUICommand = SearchResultItem.AssociatedCommand as IRibbonLinkedUICommand;
                IList<FrameworkElement> checkedMenus = linkedUICommand.GetLinkedElements(RibbonUICommandLinkedElementTypes.CheckedMenus);
                if (checkedMenus.Count == 0)
                    resourceKey = SearchResultItem.AssociatedCommand.GraphicResourceKey;
                else
                {
                    RadMenuItem menuItem = ((RadMenuItem)checkedMenus[0]);
                    if (menuItem.IsChecked)
                        resourceKey = "TelerikChecked"; // I18N. OK (No reference to Haestad.Framework.Windows.Forms and we don't want to add one).
                    else
                        resourceKey = "TelerikUnchecked"; // I18N. OK (No reference to Haestad.Framework.Windows.Forms and we don't want to add one).
                }
            }
            else
                resourceKey = SearchResultItem.AssociatedCommand.GraphicResourceKey;

            if (StringLibrary.IsBlank(resourceKey))
                Image.Source = null;
            else
                Image.Source = ImageProvider.GetImage(false, resourceKey);

            if (RibbonExternalConfiguration.Instance.RibbonSearchDebuggingEnabled)
                TextBlock.Text = SearchResultItem.CommandPath + " [Name: " + SearchResultItem.AssociatedCommand.Name + ", Rank: " + SearchResultItem.CommandRank + "]";
            else
                TextBlock.Text = SearchResultItem.CommandPath;

            IsEnabled = SearchResultItem.AssociatedCommand.CanExecute(null);
        }

        private void InitializeControl()
        {
            InitializeGridColumns();
            AddImageToGrid();
            AddTextToGrid();
            AddChild(Grid);
        }

        private void InitializeGridColumns()
        {
            ColumnDefinition autoColumn = new ColumnDefinition();
            autoColumn.Width = GridLength.Auto;
            Grid.ColumnDefinitions.Add(autoColumn);

            ColumnDefinition starColumn = new ColumnDefinition();
            Grid.ColumnDefinitions.Add(starColumn);
        }

        private void AddImageToGrid()
        {
            Image.Margin = new Thickness(0.0, 0.0, 8.0, 0.0);
            Image.HorizontalAlignment = HorizontalAlignment.Left;
            Image.VerticalAlignment = VerticalAlignment.Center;

            if (RibbonExternalConfiguration.Instance.RibbonDebuggingEnabled)
            {
                Border border = new Border();
                border.BorderThickness = new Thickness(1.0);
                border.Child = Image;
                border.BorderBrush = Brushes.Red;
                Grid.Children.Add(border);
            }
            else
            {
                Grid.Children.Add(Image);
            }
        }

        private void AddTextToGrid()
        {
            TextBlock.HorizontalAlignment = HorizontalAlignment.Left;

            if (RibbonExternalConfiguration.Instance.RibbonDebuggingEnabled)
            {
                Border border = new Border();
                border.BorderThickness = new Thickness(1.0);
                border.Child = TextBlock;
                border.BorderBrush = Brushes.Red;
                Grid.Children.Add(border);
                Grid.SetColumn(border, 1);
            }
            else
            {
                Grid.Children.Add(TextBlock);
                Grid.SetColumn(TextBlock, 1);
            }
        }
        #endregion

        #region Private Properties
        private Grid Grid
        {
            get
            {
                if (m_grid == null)
                    m_grid = new Grid();
                return m_grid;
            }
        }

        private Image Image
        {
            get
            {
                if (m_image == null)
                    m_image = new Image();
                return m_image;
            }
        }

        private TextBlock TextBlock
        {
            get
            {
                if (m_textBlock == null)
                    m_textBlock = new TextBlock();
                return m_textBlock;
            }
        }

        private RadComboBox SearchCombo
        {
            get;
            set;
        }

        private IImageProvider ImageProvider
        {
            get;
            set;
        }
        #endregion

        #region Private Fields
        private Grid m_grid;
        private Image m_image;
        private TextBlock m_textBlock;
        private RibbonSearchResultItem m_searchResultItem;
        #endregion
    }
}
