using System;
using System.Collections.Generic;
using System.Linq;
using Toolbox.Docking.Api.Control;
using Toolbox.Docking.Api.Objects;
using Toolbox.Docking.Api.Objects.Profiler;

namespace ToolboxAddinExamples.Profiler
{
    public class ProfilerAddinFactory : ITbProfilerFactory
    {
        private const string Caption = "(Test) ONS atoms";
        private static readonly Guid Guid = new Guid("936004BF-7C5D-4CB6-A20F-DC526E2891C5");
        private static readonly Version Version = new Version(1, 0);
        private const string Description = "Dummy Profiler - Toolbox Docking Example";
        private const string Donator = "LMC";
        private const string Disclaimer = "For demonstrational purposes only.";
        private const string Authors = "Toolbox Development Team @LMC";
        private const string Url = "http://toolbox.oasis-lmc.org";

        public ProfilerAddinFactory()
        {
            ObjectId = new TbObjectId(Caption, Guid, Version);
            ObjectAbout = new TbObjectAbout(Description, Donator, Disclaimer, Authors, Url, Caption,
            //TODO: toolbox object documentation file?
            null, null);
            Flags = TbObjectFlags.None;
        }

        /// <summary>
        /// Return an int value in order profiler to be cached.
        /// Just increase the velue if any modifications are done without chnging name or version of the profiler.
        /// </summary>
        public int? Hash => 1;

        public bool InitFactory(IList<string> errorLog, ITbInitTask initTask)
        {
            return true;
        }

        public TbObjectFlags Flags { get; }

        public TbObjectId ObjectId { get; }

        public TbObjectAbout ObjectAbout { get; }

        public string GetLiteratureForCategory(string category)
        {
            return string.Empty;
        }

        public ITbProfiler GetProfiler(ITbWorkTask task)
        {
            return new ProfilerAddin(AtomToCategoryDictionary);
        }

        public TbCategoryType ProfilerType => TbCategoryType.Empiric;

        private static readonly IReadOnlyDictionary<string, string> AtomToCategoryDictionary =
            new Dictionary<string, string>
            {
                {"O", "Has Oxygen"},
                {"N", "Has Nitrogen"},
                {"S", "Has Sulfur"}
            };

        private TbProfilingCategories _allCategories;

        public TbProfilingCategories AllCategories
        {
            get
            {
                if (_allCategories == null)
                {
                    var categories = AtomToCategoryDictionary.Values.ToList();
                    categories.Add(ProfilerAddin.DefaultCategory);
                    _allCategories = new TbProfilingCategories(categories, 1, false, null);
                }

                return _allCategories;
            }
        }
    }
}
