using System;
using System.IO;
using System.Reflection;
using Valg.EVA.Skanning.Configuration;
using Valg.EVA.Skanning.UserInterfaceElements.UserSettings;

namespace Valg.EVA.Skanning.Scan.ReadSoftEmulation.ScannerAccess
{
    public class ScannerConfig
    {
        public ScannerDefinition ScannerDefinition { get; set; }
        
        public string ConfigFilename { get; set; }
        
        


        public static ScannerConfig GetCurrentConfig()
        {
            var settingsManager = new UserSettingsProvider(new System.IO.Abstractions.FileSystem());
            var ret = new ScannerConfig { ScannerDefinition = GetCurrenScannerDefinition() };
            
            ret.ConfigFilename = DefaultIsisConfigFilename;
            return ret;
        }
        
        private static ScannerDefinition GetCurrenScannerDefinition()
        {
            var settingsManager = new UserSettingsProvider(new System.IO.Abstractions.FileSystem());
            var userSettings = settingsManager.GetUserSettings();
            if (userSettings.EmulateScanner)
            {
                return new Emulator();
            }

            var modelName = userSettings.SelectedScannerModel;
            if (!string.IsNullOrEmpty(modelName))
            {
                return new ScannerModel() {ModelName = modelName};
            }
            var driverName = userSettings.SelectedUnsupportedDriver;
            if (!string.IsNullOrEmpty(driverName))
            {
                return new ScannerDriver() {DriverName = driverName};
            }

            throw new Exception("Current scanner definition undefined");
        }

        public static string DefaultIsisConfigFilename
        {
            get
            {
                var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
                var inf =new FileInfo(location.LocalPath);
                
                return Path.Combine(inf.Directory.FullName, "ScannerConfig.ini");
            }
        }
    }
}

