using CandyboxPlugin.Module;
using Inspectron.Settings;
using Ninject;
using System.Reflection;
using CandyboxPlugin.Modules.Barcode;
using Ninject.Planning.Bindings;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.Common.Plugins;
using VisionBuilder.UI.Common.Processing;
using VisionBuilder.UI.Common.ViewModel.Interfaces.UI;

namespace CandyboxPlugin
{
    public class Plugin: IPlugin
    {
        public void RegisterGlobalModules(IKernel kernel)
        {
            TypeConverterRegistry.Register<List<BarcodeRecipeMapping>>(new ListBarcodeRecipeMappingConverter());

            kernel.Rebind<IRecipeCreationTool>().To<CandyboxRecipeCreationTool>().InSingletonScope();
        }

        public void RegisterCameraModules(IKernel kernel, string cameraName)
        {

            // remove settings binding
            var existingRecognitionControlSettings = kernel.GetBindings(typeof(BaseRecognitionControlSettings)).First();
            var toRemove = kernel.GetBindings(typeof(ISettings)).First(x => x.ProviderCallback.Target == existingRecognitionControlSettings.ProviderCallback.Target);
            kernel.RemoveBinding(toRemove);

            kernel.Bind<CandyboxRecognitionControlSettings,BaseRecognitionControlSettings, ISettings>().ToConstant(new CandyboxRecognitionControlSettings(cameraName));
            
            kernel.Rebind<IRecognitionControl>().To<CandyboxImageProcessingControl>().InSingletonScope();
            
            kernel.Rebind<ILearningTool>().To<CandyboxLearningTool>().InSingletonScope();

            kernel.Bind<CandyboxBarcodeReaderSettings, ISettings>()
                .ToConstant(new CandyboxBarcodeReaderSettings(cameraName));
            kernel.RegisterModule<CandyboxBarcodeReader>();

        }
    }
}
