using System;
using System.Collections.Generic;
using Avalonia.Controls.Platform;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Platform;
using Avalonia.Remote.Protocol;
using Avalonia.Rendering;

namespace Avalonia.DesignerSupport.Remote
{
    class PreviewerWindowingPlatform : IWindowingPlatform, IPlatformSettings
    {
        static readonly IKeyboardDevice Keyboard = new KeyboardDevice();
        private static IAvaloniaRemoteTransportConnection s_transport;
        private static DetachableTransportConnection s_lastWindowTransport;
        private static PreviewerWindowImpl s_lastWindow;
        public static List<object> PreFlightMessages = new List<object>();

        public ITrayIconImpl CreateTrayIcon() => null;

        public IWindowImpl CreateWindow() => new WindowStub();

        public IWindowImpl CreateEmbeddableWindow()
        {
            if (s_lastWindow != null)
            {
                s_lastWindowTransport.Dispose();
                try
                {
                    s_lastWindow.Dispose();
                }
                catch
                {
                    //Ignore
                }
            }
            s_lastWindow =
                new PreviewerWindowImpl(s_lastWindowTransport = new DetachableTransportConnection(s_transport));
            foreach (var pf in PreFlightMessages)
                s_lastWindowTransport.FireOnMessage(s_lastWindowTransport, pf);
            return s_lastWindow;
        }

        public static void Initialize(IAvaloniaRemoteTransportConnection transport)
        {
            s_transport = transport;
            var instance = new PreviewerWindowingPlatform();
            var threading = new InternalPlatformThreadingInterface();
            AvaloniaLocator.CurrentMutable
                .Bind<IClipboard>().ToSingleton<ClipboardStub>()
                .Bind<ICursorFactory>().ToSingleton<CursorFactoryStub>()
                .Bind<IKeyboardDevice>().ToConstant(Keyboard)
                .Bind<IPlatformSettings>().ToConstant(instance)
                .Bind<IPlatformThreadingInterface>().ToConstant(threading)
                .Bind<IRenderLoop>().ToConstant(new RenderLoop())
                .Bind<IRenderTimer>().ToConstant(new DefaultRenderTimer(60))
                .Bind<ISystemDialogImpl>().ToSingleton<SystemDialogsStub>()
                .Bind<IWindowingPlatform>().ToConstant(instance)
                .Bind<IPlatformIconLoader>().ToSingleton<IconLoaderStub>()
                .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>();

        }

        public Size DoubleClickSize { get; } = new Size(2, 2);
        public TimeSpan DoubleClickTime { get; } = TimeSpan.FromMilliseconds(500);

        public Size TouchDoubleClickSize => new Size(16, 16);

        public TimeSpan TouchDoubleClickTime => DoubleClickTime;
    }
}
