using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Controls;
using BasicLibrarySamples;
using Windows.UI.Xaml;

// The data model defined by this file serves as a representative example of a strongly-typed
// model that supports notification when members are added, removed, or modified.  The property
// names chosen coincide with data bindings in the standard item templates.
//
// Applications may use this model as a starting point and build on it, or discard it entirely and
// replace it with something appropriate to their needs.

namespace BasicLibrarySamples.Data
{
    /// <summary>
    /// Base class for <see cref="SampleDataItem"/> and <see cref="SampleDataGroup"/> that
    /// defines properties common to both.
    /// </summary>
    [Windows.Foundation.Metadata.WebHostHidden]
    public abstract class SampleDataCommon : BasicLibrarySamples.Common.BindableBase
    {
        private static Uri _baseUri = new Uri("ms-appx:///");

        public SampleDataCommon(String uniqueId, String title, String description, String imagePath)
        {
            this._uniqueId = uniqueId;
            this._title = title;
            this._description = description;
            this._imagePath = imagePath;
            this.Name = title;
        }

        private string _uniqueId = string.Empty;
        public string UniqueId
        {
            get { return this._uniqueId; }
            set { this.SetProperty(ref this._uniqueId, value); }
        }

        private string _title = string.Empty;
        public string Title
        {
            get { return this._title; }
            set { this.SetProperty(ref this._title, value); }
        }

        private string _description = string.Empty;
        public string Description
        {
            get { return this._description; }
            set { this.SetProperty(ref this._description, value); }
        }

        public string Name { get; set; }

        public DataTemplate Icon
        {
            get
            {
                object res;
                if (App.Current.Resources.TryGetValue("IconC1" + Name.Replace(" ", ""), out res))
                    return (DataTemplate)res;
                else
                {
                    res = App.Current.Resources["IconC1Gray"];
                    return (DataTemplate)res;
            }
                return App.Current.Resources.TryGetValue("IconC1" + Name.Replace(" ", ""), out res) ? (DataTemplate)res :
                    (DataTemplate)App.Current.Resources["IconC1Gray"];

                //return (DataTemplate)App.Current.Resources["IconC1" + Name.Replace(" ", "")] ?? (DataTemplate)App.Current.Resources["IconC1Gray"];
        }
        }

        private ImageSource _image = null;
        private String _imagePath = null;
        public ImageSource Image
        {
            get
            {
                if (this._image == null && this._imagePath != null)
                {
                    this._image = new BitmapImage(new Uri(SampleDataCommon._baseUri, this._imagePath));
                }
                return this._image;
            }

            set
            {
                this._imagePath = null;
                this.SetProperty(ref this._image, value);
            }
        }

        public void SetImage(String path)
        {
            this._image = null;
            this._imagePath = path;
            this.OnPropertyChanged("Image");
        }
    }

    /// <summary>
    /// Generic item data model.
    /// </summary>
    public class SampleDataItem : SampleDataCommon
    {
        public SampleDataItem(String uniqueId, String title, String description, String imagePath, Type pageType)
            : base(uniqueId, title, description, imagePath)
        {
            _pageType = pageType;
        }

        private Type _pageType;
        public Type PageType
        {
            get { return this._pageType; }
            set { this.SetProperty(ref this._pageType, value); }
        }
    }

    /// <summary>
    /// Creates a collection of groups and items with hard-coded content.
    /// </summary>
    public sealed class SampleDataSource
    {
        private static SampleDataSource _sampleDataSource = new SampleDataSource();

        private ObservableCollection<SampleDataItem> _allItems = new ObservableCollection<SampleDataItem>();
        public ObservableCollection<SampleDataItem> AllItems
        {
            get { return this._allItems; }
        }

        public static IEnumerable<SampleDataItem> GetItems(string uniqueId)
        {
            if (!uniqueId.Equals("AllItems")) throw new ArgumentException("Only 'AllItems' is supported as a collection of items");

            return _sampleDataSource.AllItems;
        }

        public static SampleDataItem GetItem(string uniqueId)
        {
            // Simple linear search is acceptable for small data sets
            var matches = _sampleDataSource.AllItems.Where((item) => item.UniqueId.Equals(uniqueId));
            if (matches.Count() == 1) return matches.First();
            return null;
        }


        public SampleDataSource()
        {
   
            _allItems.Add(new SampleDataItem("MaskedTextBox",
                    "MaskedTextBox",
                    "The C1MaskedTextBox control allows users to type text that conforms to a given mask.",
                    "Assets/LightGray.png",
                    typeof(MaskedTextBoxSample)));
            _allItems.Add(new SampleDataItem("NumericBox",
                    "NumericBox",
                    "The C1NumericBox control allows users to type formatted numeric values (use the UP/DOWN buttons to skip by an increment).",
                    "Assets/DarkGray.png",
                    typeof(NumericBoxSample)));
            _allItems.Add(new SampleDataItem("DropDown",
                    "DropDown",
                    "Host any UIElement in a drop-down with the C1DropDown control.",
                    "Assets/LightGray.png",
                    typeof(DropDownDemo)));
            _allItems.Add(new SampleDataItem("HierarchicalDropDown",
                    "Hierarchical Tree",                    
                    "Create a hierarchical drop-down combobox by putting a C1TreeView control inside C1DropDown.",
                    "Assets/LightGray.png",
                    typeof(HierarchicalDropDown)));
            _allItems.Add(new SampleDataItem("AutoComplete",
                    "AutoComplete",
                    "Create an auto-complete textbox with the C1DropDown control.",
                    "Assets/LightGray.png",
                    typeof(AutoCompleteDropDown)));
            _allItems.Add(new SampleDataItem("DockPanel",
                    "DockPanel",
                    "Dock content along the edges of your page using the C1DockPanel control.",
                    "Assets/LightGray.png",
                    typeof(DockPanelSample)));
            _allItems.Add(new SampleDataItem("UniformGrid",
                    "UniformGrid",
                    "Display content neatly in a grid using the C1UniformGrid control.",
                    "Assets/LightGray.png",
                    typeof(UniformGridSample)));
            _allItems.Add(new SampleDataItem("WrapPanel",
                    "WrapPanel",
                    "Wrap content vertically or horizontally using the C1WrapPanel control.",
                    "Assets/DarkGray.png",
                    typeof(WrapPanelSample)));
            _allItems.Add(new SampleDataItem("RadialPanel",
                   "RadialPanel",
                   "Display content in a circle.",
                   "Assets/LightGray.png",
                   typeof(RadialPanelSample)));
            _allItems.Add(new SampleDataItem("EntranceTab",
                   "TabControl",
                   "This demo shows how you can easily switch among multiple pages of content using a C1TabControl.",
                   "Assets/LightGray.png",
                   typeof(EntranceTab)));
            _allItems.Add(new SampleDataItem("ClearStyleTabControl",
                   "Tab Placement",
                   "Align the tabs along any edge of the control.",
                   "Assets/LightGray.png",
                   typeof(ClearStyleTabControl)));
            _allItems.Add(new SampleDataItem("TabControlSample",
                   "Close and Pin",
                   "This demo shows a classic looking tab control complete with close and pin buttons.",
                   "Assets/LightGray.png",
                   typeof(TabControlSample)));
            _allItems.Add(new SampleDataItem("CollectionViewConditions",
                    "Conditions",
                    "C1CollectionView implements the ICollectionView interface and provides live sorting, filtering and grouping for any list control.",
                    "Assets/LightGray.png",
                    typeof(CollectionViewConditions)));
            _allItems.Add(new SampleDataItem("BindingToC1CollectionView",
                    "Grouping",
                    "This sample shows how to setup ListBox and GridView controls to work with C1CollectionView in the group mode.",
                    "Assets/LightGray.png",
                    typeof(BindingToC1CollectionView)));
            _allItems.Add(new SampleDataItem("ListBox",
                    "ListBox",
                    "The C1ListBox control is a high performance list box that has zoom functionality built-in. Users can scale the list of items by intuitively pinching.",
                    "Assets/LightGray.png",
                    typeof(ListBoxSample)));
            //_allItems.Add(new SampleDataItem("TileListBox",
            //        "TileListBox",
            //        "The C1TileListBox control is a high performance list box that can arrange its items in both rows and columns to create tile displays. Both ComponentOne ListBox controls support incremental loading so you can infinitely load more items on demand as the user scrolls to the end of the list.",
            //        "Assets/DarkGray.png",
            //        typeof(TileListBoxSample)));
            _allItems.Add(new SampleDataItem("TreeView",
                     "TreeView",
                     "The familiar TreeView UI is now available for Windows Store apps with the C1TreeView control.",
                     "Assets/LightGray.png",
                     typeof(TreeViewSample)));
            _allItems.Add(new SampleDataItem("CheckBoxTreeView",
                    "CheckBoxTreeView",
                    "Display check boxes on the nodes of the C1TreeView.",
                    "Assets/LightGray.png",
                    typeof(CheckBox)));
            _allItems.Add(new SampleDataItem("DragDropTreeView",
                    "DragDropTreeView",
                    "Shows built-in support for drag-and-drop operations. ",
                    "Assets/LightGray.png",
                    typeof(DragDrop)));
            _allItems.Add(new SampleDataItem("HierarchicalTemplateTreeView",
                   "HierarchicalTreeView",
                   "Support for Hierarchical Data Templates allows you to easily customize the appearance of the C1Treeview nodes based upon the data items.",
                   "Assets/LightGray.png",
                   typeof(HierarchicalTemplate)));
            if (!(Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")))
            {
                _allItems.Add(new SampleDataItem("EditTreeView",
                    "EditTreeView",
                    "Add editing support to the nodes of the C1TreeView. Press F2 on the selected node to edit, you can also press and hold the node to edit in touch mode.",
                    "Assets/LightGray.png",
                    typeof(Edit)));
            }
            _allItems.Add(new SampleDataItem("Menu",
                    "Menu",
                    "Represents a control that displays hierarchical data in a menu structure.",
                    "Assets/LightGray.png",
                    typeof(MenuSample)));
            _allItems.Add(new SampleDataItem("ContextMenu",
                    "ContextMenu",
                    "Represents a control that displays hierarchical context menu.",
                    "Assets/LightGray.png",
                    typeof(ContextMenu)));
            _allItems.Add(new SampleDataItem("RadialMenu",
                    "RadialMenu",
                    "Represents a control that displays hierarchical data in a touch-friendly menu.",
                    "Assets/LightGray.png",
                    typeof(RadialMenu)));
            _allItems.Add(new SampleDataItem("ProgressIndicator",
                    "Progress Indicator",
                    "Indicate to the user that your app is busy with the C1ProgressIndicator control.",
                    "Assets/DarkGray.png",
                    typeof(ProgressIndicatorDemo)));
            _allItems.Add(new SampleDataItem("InputHandling",
                    "Input Handling",
                    "Show how to handle drag and zoom gestures using cross-platform helpers.",
                    "Assets/DarkGray.png",
                    typeof(InputHandling)));
            _allItems.Add(new SampleDataItem("RangeSlider",
                    "RangeSlider",
                    "Represent a RangeSlider",
                    "Assets/DarkGray.png",
                    typeof(RangeSlider)));
            _allItems.Add(new SampleDataItem("GridSplitter",
                    "GridSplitter",
                    "Use grid splitter to redestribute the space between grid rows and columns.",
                    "Assets/LightGray.png",
                    typeof(GridSplitterSample)));

            var orderby = from e in _allItems orderby e.Title select e;
            _allItems = new ObservableCollection<SampleDataItem>(orderby);

        }
    }
}
