using Ninject;
using Ninject.Extensions.ChildKernel;
using System.ComponentModel;
using System.Reflection;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.IOCommander.Interfaces;
using VisionBuilder.UI.IOCommander.Modules;
using VisionBuilder.UI.IOCommander.Settings;

namespace VisionBuilder.UI.IOCommander;

public static class ModuleExtensions
{
    public static IKernel UseIOCommander(this IKernel self, string[] cameraNames)
    {
        // for settings to work properly
        TypeDescriptor.AddAttributes(typeof(List<IOCommanderPinAction>), new TypeConverterAttribute(typeof(IOCommanderPinActionListConverter)));
        TypeDescriptor.AddAttributes(typeof(List<IOCommanderCommand>), new TypeConverterAttribute(typeof(IOCommanderCommandListConverter)));

        List<IOCommanderCameraSettings> cameras = cameraNames.Select(name => new IOCommanderCameraSettings(name)).ToList();
        self.Bind<IOCommanderSettings, ISettings>().ToConstant(new IOCommanderSettings(cameras.ToArray()));
        self.Bind<IOCommanderModule,IVisionBuilderModule>().To<IOCommanderModule>().InSingletonScope();
        self.Bind<IOCommanderProgramModule, IVisionBuilderModule>().To<IOCommanderProgramModule>().InSingletonScope();
        self.Bind<IIOCommanderDebugViewService>().To<NoDebugView>().InSingletonScope();
        self.Bind<IIOCommanderFactory>().To<DefaultIOCommanderFactory>().InSingletonScope();
        return self;
    }

    public static IChildKernel UseCameraIOCommander(this IChildKernel self, string cameraName)
    {
        var ioCommanderSettings = self.Get<IOCommanderSettings>();
        var cameraSettings=ioCommanderSettings.Cameras.First(x => x.CameraName == cameraName);
        self.Bind<IOCommanderCameraSettings, ISettings>().ToConstant(cameraSettings);
        self.Bind<IOCommanderCameraModule, IVisionBuilderModule>().To<IOCommanderCameraModule>().InSingletonScope();
        return self;
    }
}