using System;
using System.Linq;
using System.Net;
using System.Windows;
using System.Collections.Generic;
using System.Xml.Linq;
using Windows.UI.Xaml;

namespace ControlExplorer
{
    public class ControlDescription: ISearchable
    {
        public const string IS_FAVORITE_SEARCH_WORD = "favorite";
        
        public ControlDescription(XElement c)
        {
            bool b = false;
            IsFavorite = (c.Attribute("isFavorite") != null && bool.TryParse(c.Attribute("isFavorite").Value, out b) ? b : false);
            IsEnabled = (c.Attribute("enabled") != null && bool.TryParse(c.Attribute("enabled").Value, out b) ? b : true);
            AssemblyName = c.Attribute("assembly") != null ? PlatformUtils.AdjustPlatformName(c.Attribute("assembly").Value, false) : string.Empty;
            Name = c.Attribute("name").Value;
            Source = (c.Attribute("source") != null) ? c.Attribute("source").Value : string.Empty;
            Description = (c.Element("Description") != null) ? c.Element("Description").Value : string.Empty;
            Features = (from f in c.Elements("Feature") select new FeatureDescription(this, f)).ToList();
        }

        public ControlDescription(string p1, string p2, string p3, string p4, string p5, bool isEnabled = true)
        {
            Name = p1;
            AssemblyName = p2;
            Features = new List<FeatureDescription>();
            IsEnabled = isEnabled;
        }

        public bool IsFavorite { get; set; }
        public bool IsEnabled { get; set; }
        public string Name { get; set; }
        public string AssemblyName { get; set; }

        public string Source { get; set; }
        public Uri Link
        {
            get
            {
                var first = Features.FirstOrDefault();
                if (first != null)
                    return first.Link;
                else
                    return null;
            }
        }
        public string Description { get; set; }
        public DataTemplate Icon
        {
            get
            {
                try
                {
                    return (DataTemplate)App.Current.Resources["IconC1" + Name.Replace(" ", "")];
                }
                catch
                {
                    return (DataTemplate)App.Current.Resources["IconC1Gray"];
                }
            }
        }
        public DataTemplate IconSearch
        {
            get
            {
                return (DataTemplate)App.Current.Resources["IconSearchC1" + Name.Replace(" ", "")];
            }
        }
        public IList<FeatureDescription> Features { get; set; }

        #region ISearchable Members

        public bool Contains(string word)
        {
            word = word.ToLower();
            bool inFeature = false;
            foreach (var feature in Features)
            {
                inFeature |= feature.Contains(word);
            }

            return inFeature 
                || Name.ToLower().Contains(word) 
                || AssemblyName.ToLower().Contains(word.ToLower())
         //       || Description.ToLower().Contains(word.ToLower())
                || (IsFavorite && string.Equals(word, IS_FAVORITE_SEARCH_WORD, StringComparison.OrdinalIgnoreCase));
        }

        public bool ContainsAny(string[] searchKeys)
        {
            return searchKeys.Any(key => Contains(key));
        }

        #endregion

        internal IEnumerable<FeatureDescription> GetAllFeatures()
        {
            return Features.SelectMany(f => f.SubFeatures.Count() > 0 ? f.SubFeatures.ToList()/*.Concat(new List<FeatureDescription> {f})*/ : new List<FeatureDescription> { f });
        }

        internal FeatureDescription GetFeature(string typeName)
        {
            return Features.Where(f => f.DemoControlTypeName.Equals(typeName)).First<FeatureDescription>();
        }
    }
}
