using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using NcProgramManager.Cnc.Models;

namespace NcProgramManager
{
    internal static class ArgParser
    {
        public static InputArgs Parse(string[] args)
        {
            var inputArgs = new InputArgs();
            var arguments = new List<string>(args);

            var help = arguments.Find(it => new Regex("-help.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(help))
            {
                inputArgs.mostraHelp = true;
                return inputArgs;
            }

            var azione = arguments.Find(it => new Regex("-azione=.*", RegexOptions.IgnoreCase).IsMatch(it));
            inputArgs.azione = azione.Substring(8);

            var manufacturer = arguments.Find(it => new Regex("-manufacturer=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(manufacturer))
            {
                string mfgVal = manufacturer.Substring(14).Trim().ToLowerInvariant();
                switch (mfgVal)
                {
                    case "heidenhain":  inputArgs.manufacturer = CncManufacturer.Heidenhain;  break;
                    case "siemens":     inputArgs.manufacturer = CncManufacturer.Siemens;     break;
                    case "mitsubishi":  inputArgs.manufacturer = CncManufacturer.Mitsubishi;  break;
                    case "fagor":       inputArgs.manufacturer = CncManufacturer.Fagor;       break;
                    case "mazak":       inputArgs.manufacturer = CncManufacturer.Mazak;       break;
                    default:            inputArgs.manufacturer = CncManufacturer.Fanuc;       break;
                }
            }

            string tipo = arguments.Find(it => new Regex("-tipo=.*", RegexOptions.IgnoreCase).IsMatch(it));
            inputArgs.tipo = !string.IsNullOrEmpty(tipo) ? Int32.Parse(tipo.Substring(6)) : 3;

            var ip = arguments.Find(it => new Regex("-ip=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(ip))
                inputArgs.ip = ip.Substring(4);

            var porta = arguments.Find(it => new Regex("-porta=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(porta))
                inputArgs.porta = porta.Substring(7);

            var nodo = arguments.Find(it => new Regex("-nodo=.*", RegexOptions.IgnoreCase).IsMatch(it));
            inputArgs.nodoHssb = !string.IsNullOrEmpty(nodo) ? Int32.Parse(nodo.Substring(6)) : -1;

            var username = arguments.Find(it => new Regex("-username=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(username))
                inputArgs.username = username.Substring(10);

            var password = arguments.Find(it => new Regex("-password=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(password))
                inputArgs.password = password.Substring(10);

            var compatibility = arguments.Find(it => new Regex("-compatibility.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(compatibility))
                inputArgs.compatibilityMode = true;

            var commento = arguments.Find(it => new Regex("-commento=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(commento))
                inputArgs.commentoProgramma = commento.Substring(10);

            var path = arguments.Find(it => new Regex("-path=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(path))
                inputArgs.pathCNC = Int16.Parse(path.Substring(6));

            var pathprogramma = arguments.Find(it => new Regex("-pathprogramma=.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(pathprogramma))
                inputArgs.pathLocaleProgramma = pathprogramma.Substring(15);

            var hasToolOffsetData = arguments.Find(it => new Regex("-tooloffset.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(hasToolOffsetData))
                inputArgs.hasToolOffsetData = true;

            var hasWorkZeroOffsetData = arguments.Find(it => new Regex("-workzero.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(hasWorkZeroOffsetData))
                inputArgs.hasWorkZeroOffsetData = true;

            var debugMode = arguments.Find(it => new Regex("-debug.*", RegexOptions.IgnoreCase).IsMatch(it));
            if (!string.IsNullOrEmpty(debugMode))
                inputArgs.debugMode = true;

            return inputArgs;
        }
    }
}
