/*
    godotVmcSharp
    Copyright (C) 2023  Cassandra de la Cruz-Munoz
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero 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 Affero General Public License for more details.
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see .
    */
using godotOscSharp;
using System.Collections.Generic;
#nullable enable
namespace godotVmcSharp
{
    public class VmcExtRcv : VmcMessage
    {
        public readonly int Enable;
        public readonly int Port;
        public readonly string? IpAddress;
        public static VmcExtRcv CreateFromMessage(OscMessage m)
        {
            if (m.Data.Count < 2)
            {
                throw new MissingArgumentsException(m.Address, "{2, 3}", m.Data.Count);
            }
            var enable = m.Data[0].GetAsInt32();
            if (enable < 0 || enable > 1)
            {
                throw new DataOutOfRangeException(m.Address, 0, "enable", "{0, 1}", enable);
            }
            var port = m.Data[1].GetAsInt32();
            if (port < 0 || port > 65535)
            {
                throw new DataOutOfRangeException(m.Address, 1, "port", "{0..65535}", port);
            }
            if (m.Data.Count >= 2) {
                return new VmcExtRcv(enable, port);
            }
            return new VmcExtRcv(enable, port, m.Data[2].GetAsString());
        }
        public static VmcExtRcv CreateFromParams(int enable, int port)
        {
            if (enable < 0 || enable > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable);
            }
            if (port < 0 || port > 65535)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port);
            }
            return new VmcExtRcv(enable, port);
        }
        private VmcExtRcv(int enable, int port) : base(new OscAddress("/VMC/Ext/Rcv"))
        {
            Enable = enable;
            Port = port;
        }
        public static VmcExtRcv CreateFromMessage(int enable, int port, string ipAddress)
        {
            if (enable < 0 || enable > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable);
            }
            if (port < 0 || port > 65535)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port);
            }
            return new VmcExtRcv(enable, port, ipAddress);
        }
        private VmcExtRcv(int enable, int port, string ipAddress) : base(new OscAddress("/VMC/Ext/Rcv"))
        {
            if (enable < 0 || enable > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 0, "enable", "{0, 1}", enable);
            }
            if (port < 0 || port > 65535)
            {
                throw new DataOutOfRangeException("/VMC/Ext/Rcv", 1, "port", "{0..65535}", port);
            }
            Enable = enable;
            Port = port;
            IpAddress = ipAddress;
        }
        public new OscMessage ToMessage()
        {
            if (IpAddress == null)
            {
                return new OscMessage(Addr, new List{
                    OscArgument.CreateFromParams(Enable, 'i')!,
                    OscArgument.CreateFromParams(Port, 'i')!,
                });
            }
            return new OscMessage(Addr, new List{
                OscArgument.CreateFromParams(Enable, 'i')!,
                OscArgument.CreateFromParams(Port, 'i')!,
                OscArgument.CreateFromParams(IpAddress, 's')!,
            });
        }
    }
}