// 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 System.Linq;
using Hiperscan.SGS.CalibrationDevice;
using Hiperscan.SGS.Common;
using Hiperscan.SGS.SerialInterface;
using Hiperscan.SGS.SerialInterface.USB;
using Hiperscan.SGS.SerialInterface.USB.DeviceFinder;
using Hiperscan.SGS.Sgs1900;
using Hiperscan.Unix;
using Version = Hiperscan.SGS.Common.Version;

namespace Hiperscan.SGS
{
    public partial class DeviceList
    {
        public void AddRemoteDevice(string server, uint port)
        {
            if (this.IsDisposed)
                return;

            RemoteSerialInterfaceClient remote_usb = new RemoteSerialInterfaceClient(server, port);
            lock (this)
            {
                if (this.ContainsSerial(remote_usb.DeviceInfo.SerialNumber))
                    throw new Exception(string.Format(Catalog.GetString("A device with the serial number {0} is already opened."),
                                                      remote_usb.DeviceInfo.SerialNumber));
            }

            this.OnDeviceAdded(remote_usb.DeviceInfo);
        }

        public void RemoveRemoteDevice(IDevice device)
        {
            this.OnDeviceRemoved(device.Info);
        }

        private void MoveRemoteDeviceToUsb(DeviceInfo device_info)
        {
            if (this.Devices.ContainsKey(device_info.SerialNumber) == false)
                return;

            if (this.Devices[device_info.SerialNumber].Info.IsRemote == false)
                return;

            string device_name = this.DisconnectAndRemoveDevice(device_info.SerialNumber);
            this.DeviceRemoved?.Invoke(this, device_info.SerialNumber, device_name);
            System.Threading.Thread.Sleep(1000);
        }
    }
}