/*
    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;
namespace godotVmcSharp
{
    public class VmcExtOk : VmcMessage
    {
        public readonly int Loaded;
        public readonly int? CalibrationState;
        public readonly int? CalibrationMode;
        public readonly int? TrackingStatus;
        public static VmcExtOk CreateFromParams(int loaded)
        {
            if (loaded < 0 || loaded > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
            }
            return new VmcExtOk(loaded);
        }
        private VmcExtOk(int loaded): base(new OscAddress("/VMC/Ext/OK"))
        {
            Loaded = loaded;
        }
        public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode)
        {
            if (loaded < 0 || loaded > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
            }
            if (calibrationState < 0 || calibrationState > 3)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
            }
            if (calibrationMode < 0 || calibrationMode > 2)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
            }
            return new VmcExtOk(loaded, calibrationState, calibrationMode);
        }
        private VmcExtOk(int loaded, int calibrationState, int calibrationMode) : base(new OscAddress("/VMC/Ext/OK"))
        {
            Loaded = loaded;
            CalibrationState = calibrationState;
            CalibrationMode = calibrationMode;
        }
        public static VmcExtOk CreateFromParams(int loaded, int calibrationState, int calibrationMode, int trackingStatus)
        {
            if (loaded < 0 || loaded > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
            }
            if (calibrationState < 0 || calibrationState > 3)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
            }
            if (calibrationMode < 0 || calibrationMode > 2)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
            }
            if (trackingStatus < 0 || trackingStatus > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus);
            }
            return new VmcExtOk(loaded, calibrationState, calibrationMode, trackingStatus);
        }
        private VmcExtOk(int loaded, int calibrationState, int calibrationMode, int trackingStatus) : base(new OscAddress("/VMC/Ext/OK"))
        {
            Loaded = loaded;
            CalibrationState = calibrationState;
            CalibrationMode = calibrationMode;
            TrackingStatus = trackingStatus;
        }
        public static VmcExtOk CreateFromMessage(OscMessage m)
        {
            switch (m.Data.Count)
            {
                case 1:
                    return new VmcExtOk(getLoaded(m));
                case 3:
                    return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m));
                case 4:
                    var trackingStatus = m.Data[3].GetAsInt32();
                    if (trackingStatus < 0 || trackingStatus > 1)
                    {
                        throw new DataOutOfRangeException("/VMC/Ext/OK", 3, "trackingStatus", "{0, 1}", trackingStatus);
                    }
                    return new VmcExtOk(getLoaded(m), getCalibrationState(m), getCalibrationMode(m), trackingStatus);
                default:
                    throw new MissingArgumentsException(m.Address, "{1, 3, 4}", m.Data.Count);
            }
        }
        private static int getLoaded(OscMessage m)
        {
            var loaded = m.Data[0].GetAsInt32();
            if (loaded < 0 || loaded > 1)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 0, "loaded", "{0, 1}", loaded);
            }
            return loaded;
        }
        private static int getCalibrationState(OscMessage m)
        {
            var calibrationState = m.Data[1].GetAsInt32();
            if (calibrationState < 0 || calibrationState > 3)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 1, "calibrationState", "{0..3}", calibrationState);
            }
            return calibrationState;
        }
        private static int getCalibrationMode(OscMessage m)
        {
            var calibrationMode = m.Data[2].GetAsInt32();
            if (calibrationMode < 0 || calibrationMode > 2)
            {
                throw new DataOutOfRangeException("/VMC/Ext/OK", 2, "calibrationMode", "{0..2}", calibrationMode);
            }
            return calibrationMode;
        }
        public new OscMessage ToMessage()
        {
            if (CalibrationState == null)
            {
                return new OscMessage(Addr, new List{
                    OscArgument.CreateFromParams(Loaded, 'i')!
                });
            }
            if (TrackingStatus == null)
            {
                return new OscMessage(Addr, new List{
                    OscArgument.CreateFromParams(Loaded, 'i')!,
                    OscArgument.CreateFromParams(CalibrationState, 'i')!,
                    OscArgument.CreateFromParams(CalibrationMode, 'i')!
                });
            }
            return new OscMessage(Addr, new List{
                OscArgument.CreateFromParams(Loaded, 'i')!,
                OscArgument.CreateFromParams(CalibrationState, 'i')!,
                OscArgument.CreateFromParams(CalibrationMode, 'i')!,
                OscArgument.CreateFromParams(TrackingStatus, 'i')!
            });
        }
    }
}