// Created with MonoDevelop
//
//    Copyright (C) 2023 Hiperscan GmbH Dresden, Germany
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.


using System;
using System.Collections.Generic;
using Hiperscan.SGS.SerialInterface;
using Hiperscan.Unix;

namespace Hiperscan.SGS
{
    public partial class DeviceList
    {
        public static void Init(uint vendor_id, uint product_id, SerialInterfaceConfig.BackendType usb_backend, bool only_remote_devices = false)
        {
            DeviceList.Init(new SerialInterfaceConfig[] { SerialInterfaceConfigurations.GetUsbConfig(vendor_id, product_id, usb_backend) }, only_remote_devices);
        }

        public static void Init(uint product_id, bool only_remote_devices = false)
        {
            DeviceList.Init(VendorId.FTDI, product_id, SerialInterfaceConfig.BackendType.FT245BL, only_remote_devices);
        }

        public static void Init(bool only_remote_devices = false)
        {
            DeviceList.Init(SerialInterfaceConfigurations.DEFAULT_USB_CONFIGS, only_remote_devices);
        }

        public static void Init(IEnumerable<SerialInterfaceConfig> usb_configs, bool only_remote_devices = false)
        {
            lock (DeviceList.InstanceSync)
            {
                if (DeviceList.CurrentInstance != null)
                {
                    string message = Catalog.GetString("Device list is already initialised.");
                    throw new Exception(message);
                }

                DeviceList.CurrentInstance = new DeviceList(usb_configs, only_remote_devices);
            }
        }

        public static DeviceList Get()
        {
            lock(DeviceList.InstanceSync)
            {
                if (DeviceList.CurrentInstance == null)
                {
                    string message = Catalog.GetString("The device list has not been initialised.");
                    throw new Exception(message);
                }

                return DeviceList.CurrentInstance;
            }
        }

        public static void Close()
        {
            lock(DeviceList.InstanceSync)
            {
                if (DeviceList.CurrentInstance == null)
                    return;

                DeviceList.CurrentInstance.Dispose();
                DeviceList.CurrentInstance = null;
                LibUsbDotNet.UsbDevice.Exit();
            }
        }

        public static bool IsInitialized
        {
            get
            {
                lock(DeviceList.InstanceSync)
                {
                    return DeviceList.CurrentInstance != null;
                }
            }
        }

        private static DeviceList CurrentInstance { get; set; }
        private static object InstanceSync { get; set; } = new object();
    }
}