using System;
using InABox.Core;

namespace InABox.Rpc
{
    public class RpcValidateParameters : IRpcCommandParameters
    {
        public string? UserID { get; set; }
        public string? Password { get; set; }
        
        public string? PIN { get; set; }
        public bool UsePIN { get; set; }
        
        // reconnect to an existing session
        public Guid SessionID { get; set; }
        
        public Platform Platform { get; set; }
        public string? Version { get; set; }
        
        public void SerializeBinary(CoreBinaryWriter writer)
        {
            writer.Write(UserID ?? string.Empty);
            writer.Write(Password ?? string.Empty);
            writer.Write(PIN ?? string.Empty);
            writer.Write(UsePIN);
            writer.Write(SessionID);
            writer.WriteBinaryValue(Platform);
            writer.Write(Version ?? string.Empty);
        }

        public void DeserializeBinary(CoreBinaryReader reader)
        {
            UserID = reader.ReadString();
            Password = reader.ReadString();
            PIN = reader.ReadString();
            UsePIN = reader.ReadBoolean();
            SessionID = reader.ReadGuid();
            Platform = reader.ReadBinaryValue<Platform>();
            Version = reader.ReadString();
        }

        public string? FullDescription()
        {
            if (SessionID == Guid.Empty)
            {
                if (string.IsNullOrWhiteSpace(UserID))
                {
                    return $"Validate({PIN})";
                }
                else
                {
                    return $"Validate({UserID})";
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(UserID))
                {
                    return $"Validate({PIN}, {SessionID})";
                }
                else
                {
                    return $"Validate({UserID}, {SessionID})";
                }
            }
        }

        public string? ShortDescription() => FullDescription();
    }
}