namespace StandardScene.Fass2Simulator
{
    public sealed class Fass2SimNodeMessage
    {
        public ushort Node { get; set; }
        public ushort Distance { get; set; }
        public byte StartStop { get; set; }
        public byte Direction { get; set; }
        public byte Orientation { get; set; }
        public byte Byroad { get; set; }
        public ushort Speed { get; set; }
        public byte Obstacle { get; set; }
        public byte Audio { get; set; }
        public byte Light { get; set; }
        public byte Charge { get; set; }
        public byte Rest { get; set; }
        public byte Lift { get; set; }
        public byte Clamp { get; set; }
        public byte Tray { get; set; }
        public byte Roll { get; set; }
        public byte Shutdown { get; set; }

        public static Fass2SimNodeMessage FromBytes(byte[] bytes, int offset)
        {
            return new Fass2SimNodeMessage
            {
                Node = ReadUInt16(bytes, offset),
                Distance = ReadUInt16(bytes, offset + 2),
                StartStop = bytes[offset + 4],
                Direction = bytes[offset + 5],
                Orientation = bytes[offset + 6],
                Byroad = bytes[offset + 7],
                Speed = ReadUInt16(bytes, offset + 8),
                Obstacle = bytes[offset + 10],
                Audio = bytes[offset + 11],
                Light = bytes[offset + 12],
                Charge = bytes[offset + 13],
                Rest = bytes[offset + 14],
                Lift = bytes[offset + 15],
                Clamp = bytes[offset + 16],
                Tray = bytes[offset + 17],
                Roll = bytes[offset + 18],
                Shutdown = bytes[offset + 19]
            };
        }

        public void ApplyTo(Fass2SimVehicle vehicle)
        {
            if (vehicle.Node != Node)
            {
                return;
            }

            if (StartStop > 0)
            {
                vehicle.StartStop = StartStop;
            }
            if (Direction > 0)
            {
                vehicle.Direction = Direction;
            }
            if (Orientation > 0)
            {
                vehicle.Orientation = Orientation;
            }
            if (Byroad > 0)
            {
                vehicle.Byroad = Byroad;
            }
            if (Speed > 0)
            {
                vehicle.Speed = Speed;
            }
            if (Obstacle > 0)
            {
                vehicle.Obstacle = Obstacle;
            }
            if (Audio > 0)
            {
                vehicle.Audio = Audio;
            }
            if (Light > 0)
            {
                vehicle.Light = Light;
            }
            if (Charge > 0)
            {
                vehicle.Charge = Charge;
            }
            if (Rest > 0)
            {
                vehicle.Rest = Rest;
            }
            if (Lift > 0)
            {
                vehicle.Lift = Lift;
            }
            if (Clamp > 0)
            {
                vehicle.Clamp = Clamp;
            }
            if (Tray > 0)
            {
                vehicle.Tray = Tray;
            }
            if (Roll > 0)
            {
                vehicle.Roll = Roll;
            }
            if (Shutdown > 0)
            {
                vehicle.Shutdown = Shutdown;
            }
        }

        private static ushort ReadUInt16(byte[] bytes, int offset)
        {
            return System.BitConverter.ToUInt16(bytes, offset);
        }
    }
}
