using AddinExamplesGeneral;
using System;
using System.Threading;
using Toolbox.Docking.Api.Control;
using Toolbox.Docking.Api.Data;
using Toolbox.Docking.Api.ExtraActions;
using Toolbox.Docking.Api.Objects;
using Toolbox.Docking.Api.Objects.Calculator;
using Toolbox.Docking.Api.Units;

namespace ToolboxAddinExamples.Calculator
{
    public class CalculatorAddin : ITbCalculator, ITbObjectTunable, ITbObjectExtraAction
    {
        public const string ScaleName = "Smiles Length";

        public const string UnitName = "chars";

        private static TbUnit Unit { get; } = new TbUnit(ScaleName, UnitName);

        public void Dispose()
        {
            // Nothing to dispose
        }

        public TbScalarData Calculate(ITbBasket target)
        {
            var result = CalculateSmilesLength(target.Chemical.Smiles);
            return new TbScalarData(Unit, result);
        }

        private static int CalculateSmilesLength(string smiles)
        {
            return smiles.Length;
        }

        public void SetOptions(string newOptions)
        {
            //do nothing;
        }

        public TbExtraActionResult ExecuteExtraAction(Guid action, int command, string payload, ITbBasket target)
        {
            if (action.Equals(ServerEaExplain))
            {
                using (var subTask =
                    target.WorkTask.CreateSubWorkTask("Action which is explainable on client", 100, true))
                    for (var i = 0; i < 100; i++)
                    {
                        Thread.Sleep(50);
                        subTask.WorkItemDone();
                    }
                return new TbExtraActionResult(0, "Text to be explained on client!",AddinExamplesConsts.CalculatorPluginExplainClientModule);
            }

            if (action.Equals(ServerEaNoExplain))
            {
                using (var subTask =
                    target.WorkTask.CreateSubWorkTask("Action which has no explanation on client", 100, true))
                    for (var i = 0; i < 100; i++)
                    {
                        Thread.Sleep(50);
                        subTask.WorkItemDone();
                    }
                return new TbExtraActionResult(0, "Text will not be seen on client!", null);
            }

            if (action.Equals(HiddenAction))
            {
                using (var subTask =
                    target.WorkTask.CreateSubWorkTask("Action unknown for Toolbox", 100, true))
                    for (var i = 0; i < 100; i++)
                    {
                        Thread.Sleep(50);
                        subTask.WorkItemDone();
                    }
                return new TbExtraActionResult(0, "Result from hidden action!", AddinExamplesConsts.CalculatorPluginExplainClientModule);
            }

            throw new ArgumentException("Unknown extra action: " + action);
        }

        internal static readonly Guid DirectClientEa = new Guid("8AE50B5E-D2F6-4C7A-B7C1-FE74CF79E20C");

        internal static readonly Guid ServerEaExplain = new Guid("FF126D5C-F822-406A-A9EC-A6E909444976");

        internal static readonly Guid ServerEaNoExplain = new Guid("436B826F-93ED-4078-8A14-E7569B6306A2");

        internal static readonly Guid HiddenAction = new Guid("923DE977-CACF-4CDB-ADAE-7AD833F7171E");//the same is defined in client's party
    }
}