using AddinExamplesGeneral;
using System;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using Toolbox.Declarations;
using Toolbox.Docking.Client.Interfaces;
using Toolbox.Docking.Client.Plugins;
using Toolbox.Docking.Client.Services;

namespace LMC.Toolbox.DummyCalcClientViews
{
  
    [Export(AddinExamplesConsts.CalculatorPluginExplainClientModule, typeof(ITbExtraActionResultExplainPlugin))]
    [Export(AddinExamplesConsts.CalculatorExplainer, typeof(ITbResultExplainer))]
    [Export(AddinExamplesConsts.CalculatorOptions, typeof(ITbOptionsPlugin))]
    [Export(AddinExamplesConsts.CalculatorDirectClientExtraAction, typeof(ITbClientPlugin))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class DummyCalculatorPlugin : ITbExtraActionResultExplainPlugin, ITbResultExplainer, ITbOptionsPlugin, ITbClientPlugin
    {
        public DummyCalculatorPlugin()
            { 
            }

        private void TryRunHidden(ITbClientServiceLocator clientServices, ITbExtraActionInputState input)
        {
            var okHidden = clientServices.DialogService.Confirm("Do you want to execute one hidden action?",
                "Hidden action", false);

            if (okHidden)
                clientServices.ExtraActionService.ExecuteExtraAction(input, "Execute hidden action",
                    input.ToolboxObject.Guid, _hiddenAction, 12345, "HiddenActionStringParameter");
        }

        public Task ExplainExtraActionResult(ITbClientServiceLocator clientServices, ITbExtraActionInputState input,
            ITbExtraActionResult toExplain)
        {
            clientServices.DialogService.ShowMessage(
                "Dummy calculator extra action:" + Environment.NewLine + toExplain.Payload, "Dummy calculator extra action");

            TryRunHidden(clientServices, input);

            return Task.FromResult("");
        }

        private readonly Guid _hiddenAction = new Guid("923DE977-CACF-4CDB-ADAE-7AD833F7171E");//the same is defined on server's party

        public Task Explain(ITbExplainData subject, ITbClientServiceLocator clientServices)
        {
            clientServices.DialogService.ShowMessage(
                "Dummy calculator result:" + Environment.NewLine + subject.TextToExplain, "Dummy calculator explain");
            return Task.FromResult("");
        }

        public Task<string> ExecuteOptions(ITbUiState uiState, ITbObjectId tbObjectId, string options,
            ITbClientServiceLocator clientServices)
        {
            clientServices.DialogService.ShowMessage(options, "Dummy calculator options");
            return Task.FromResult("");
        }

        public Task ClientPluginExecute(ITbClientServiceLocator clientServices, ITbExtraActionInputState input)
        {
            var showText = "Dummy calculator client module:" + Environment.NewLine +
                           "ToolboxObject = " +
                           (input.ToolboxObject == null
                               ? "No object?"
                               : input.ToolboxObject.Caption);

            clientServices.DialogService.ShowMessage(showText, "Client module");

            TryRunHidden(clientServices, input);
            return Task.FromResult("");
        }
    }
}
