using System;
using Newtonsoft.Json;

namespace InABox.Logikal
{
    public abstract class LogikalResponse : LogikalObject
    {
        public LogikalStatus Status { get; set; } = LogikalStatus.Unknown;
        
        public static LogikalResponse FromMessage(LogikalMessage message)
        {
            if (message.Method == LogikalMethod.Connect)
                return JsonConvert.DeserializeObject<LogikalConnectResponse>(message.Payload);
            if (message.Method == LogikalMethod.Login)
                return JsonConvert.DeserializeObject<LogikalLoginResponse>(message.Payload);
            if (message.Method == LogikalMethod.Logout)
                return JsonConvert.DeserializeObject<LogikalLogoutResponse>(message.Payload);
            if (message.Method == LogikalMethod.Disconnect)
                return JsonConvert.DeserializeObject<LogikalDisconnectResponse>(message.Payload);
            if (message.Method == LogikalMethod.Error)
                return JsonConvert.DeserializeObject<LogikalErrorResponse>(message.Payload);
            throw new Exception("Unknown Message ID");
        }
        
    }

    public class LogikalErrorResponse : LogikalResponse
    {
        public override LogikalMethod Method() => LogikalMethod.Error;
        
        public String Message { get; set; }

        public LogikalErrorResponse()
        {
            Status = LogikalStatus.Error;
        }
    }
}