using System;
using NcProgramManager.Cnc.Fagor;
using NcProgramManager.Cnc.Fanuc;
using NcProgramManager.Cnc.Heidenhain;
using NcProgramManager.Cnc.Mazak;
using NcProgramManager.Cnc.Mitsubishi;
using NcProgramManager.Cnc.Models;
using NcProgramManager.Cnc.Siemens;

namespace NcProgramManager.Cnc
{
    internal static class CncMachineFactory
    {
        internal static ICncMachine Create(InputArgs args)
        {
            switch (args.manufacturer)
            {
                case CncManufacturer.Heidenhain:
                    return new HeidenhainMachine(new HeidenhainConnectionConfig
                    {
                        Name = "Heidenhain",
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? 19000 : int.Parse(args.porta),
                        Username = args.username,
                        Password = args.password,
                        ConnectTimeout = TimeSpan.FromSeconds(5)
                    });

                case CncManufacturer.Siemens:
                    return new SiemensMachine(new SiemensConnectionConfig
                    {
                        Name = "Siemens",
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? 21 : int.Parse(args.porta),
                        Username = args.username,
                        Password = args.password
                    });

                case CncManufacturer.Mitsubishi:
                    return new MitsubishiMachine(new MitsubishiConnectionConfig
                    {
                        Name = "Mitsubishi",
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? 21 : int.Parse(args.porta),
                        Username = args.username,
                        Password = args.password
                    });

                case CncManufacturer.Fagor:
                    return new FagorMachine(new FagorConnectionConfig
                    {
                        Name = "Fagor",
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? 21 : int.Parse(args.porta),
                        Username = args.username,
                        Password = args.password
                    });

                case CncManufacturer.Mazak:
                    return new MazakMachine(new MazakConnectionConfig
                    {
                        Name = "Mazak",
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? 21 : int.Parse(args.porta),
                        Username = args.username,
                        Password = args.password
                    });

                default: // Fanuc
                    return new FanucMachine(new FanucConnectionConfig
                    {
                        Name = "Fanuc",
                        Transport = args.nodoHssb >= 0 ? FanucTransport.Hssb : FanucTransport.Ethernet,
                        IpAddress = args.ip,
                        Port = string.IsNullOrEmpty(args.porta) ? (ushort)8193 : ushort.Parse(args.porta),
                        HssbNode = args.nodoHssb,
                        ConnectTimeout = TimeSpan.FromSeconds(4)
                    });
            }
        }
    }
}
