using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using InABox.Core;

namespace InABox.Clients
{
    public class Credentials
    {
        [Obsolete]
        public string? UserID { get; set; }
        [Obsolete]
        public string? Password { get; set; }

        public string Platform { get; set; }
        public string Version { get; set; }
        public Guid Session { get; set; }

    }

    public abstract class Request
    {
        public Request()
        {
            Credentials = new Credentials();
        }

        public static Action<Request>? BeforeRequest { get; set; }

        public Credentials Credentials { get; set; }

        public abstract RequestMethod GetMethod();
    }

    public enum StatusCode
    {
        OK,
        Incomplete,
        Error,
        Unauthenticated,
        BadServer
    }

    public enum RequestMethod
    {
        Info,
        Ping,
        Check2FA,
        Validate,
        MultiQuery,
        MultiDelete,
        Delete,
        Save,
        MultiSave,
        Query,
        Notify
    }

    public abstract class Response
    {
        public Response()
        {
            Status = StatusCode.Incomplete;
            Messages = new List<string>();
        }

        public StatusCode Status { get; set; }

        public List<string> Messages { get; }

        public virtual void DeserializeBinary(BinaryReader reader)
        {
            Status = (StatusCode)Enum.ToObject(typeof(StatusCode), reader.ReadInt32());
            Messages.Clear();
            var nMessages = reader.ReadInt32();
            for (int i = 0; i < nMessages; ++i)
            {
                Messages.Add(reader.ReadString());
            }
        }

        public virtual void SerializeBinary(BinaryWriter writer)
        {
            writer.Write((int)Status);

            writer.Write(Messages.Count);
            foreach(var message in Messages)
            {
                writer.Write(message);
            }
        }
    }

    public abstract class BaseRequest<TEntity> : Request where TEntity : Entity, new()
    {
    }

    public abstract class BaseResponse<TEntity> : Response where TEntity : Entity, new()
    {
    }

    /*public class ListRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public Filter<TEntity>? Filter { get; set; }

        public Columns<TEntity>? Columns { get; set; }

        public SortOrder<TEntity>? Sort { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Query;
    }
    
    public class ListResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
        public IEnumerable<object[]> Data { get; set; }
    }*/

    public class QueryRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public Filter<TEntity>? Filter { get; set; }

        public Columns<TEntity>? Columns { get; set; }

        public SortOrder<TEntity>? Sort { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Query;
    }

    public class QueryResponse<TEntity> : BaseResponse<TEntity>, ISerializeBinary where TEntity : Entity, new()
    {
        public CoreTable? Items { get; set; }

        public override void DeserializeBinary(BinaryReader reader)
        {
            base.DeserializeBinary(reader);

            if (reader.ReadBoolean())
            {
                Items = new CoreTable();
                Items.DeserializeBinary(reader);
            }
            else
            {
                Items = null;
            }
        }

        public override void SerializeBinary(BinaryWriter writer)
        {
            base.SerializeBinary(writer);

            if(Items is null)
            {
                writer.Write(false);
            }
            else
            {
                writer.Write(true);
                Items.SerializeBinary(writer);
            }
        }
    }

    /*public class LoadRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public Filter<TEntity>? Filter { get; set; }

        public SortOrder<TEntity>? Sort { get; set; }
    }

    public class LoadResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
        public TEntity[] Items { get; set; }
    }*/

    public class MultiSaveRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public TEntity[] Items { get; set; }
        public string AuditNote { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.MultiSave;
    }

    public class MultiSaveResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
        //public Guid[] IDs { get; set; }
        public TEntity[] Items { get; set; }
    }

    public class SaveRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public TEntity Item { get; set; }
        public string AuditNote { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Save;
    }

    public class SaveResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
        //public Guid ID { get; set; }
        public TEntity Item { get; set; }
    }

    public class DeleteRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public TEntity Item { get; set; }
        public string AuditNote { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Delete;
    }

    public class DeleteResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
    }

    public class MultiDeleteRequest<TEntity> : BaseRequest<TEntity> where TEntity : Entity, new()
    {
        public TEntity[] Items { get; set; }
        public string AuditNote { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.MultiDelete;
    }

    public class MultiDeleteResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
    }

    public class MultiQueryRequest : Request
    {
        public Dictionary<string, string> TableTypes { get; set; }
        public Dictionary<string, string> Filters { get; set; }
        public Dictionary<string, string> Columns { get; set; }
        public Dictionary<string, string> Sorts { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.MultiQuery;
    }

    public class MultiQueryResponse : Response, ISerializeBinary
    {
        public MultiQueryResponse()
        {
            Tables = new Dictionary<string, CoreTable>();
        }

        public Dictionary<string, CoreTable> Tables { get; set; }

        public override void DeserializeBinary(BinaryReader reader)
        {
            base.DeserializeBinary(reader);

            Tables.Clear();

            var nTables = reader.ReadInt32();
            for(int i = 0; i < nTables; ++i)
            {
                var name = reader.ReadString();
                var table = new CoreTable();
                table.DeserializeBinary(reader);
                Tables[name] = table;
            }
        }

        public override void SerializeBinary(BinaryWriter writer)
        {
            base.SerializeBinary(writer);

            writer.Write(Tables.Count);
            foreach(var (name, table) in Tables)
            {
                writer.Write(name);
                table.SerializeBinary(writer);
            }
        }
    }

    public enum ValidationResult
    {
        VALID,
        INVALID,
        REQUIRE_2FA,
        PASSWORD_EXPIRED
    }

    public class ValidateRequest : Request
    {
        public string? UserID { get; set; }
        public string? Password { get; set; }

        public string? PIN { get; set; }

        public bool UsePIN { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Validate;

    }
    public class ValidateResponse : Response
    {
        public ValidationResult ValidationResult { get; set; }

        public Guid UserGuid { get; set; }

        public string UserID { get; set; }

        public Guid SecurityID { get; set; }

        public Guid Session { get; set; }

        public string? Recipient2FA { get; set; }

        public DateTime PasswordExpiration { get; set; }
        
    }

    public class Check2FARequest : Request
    {
        public string Code { get; set; }

        public override RequestMethod GetMethod() => RequestMethod.Check2FA;
    }

    public class Check2FAResponse : Response
    {
        public bool Valid { get; set; }
    }

    public class PingRequest : Request
    {
        public override RequestMethod GetMethod() => RequestMethod.Ping;
    }

    public class PingResponse : Response { }
    
    public class InfoRequest : Request
    {
        public override RequestMethod GetMethod() => RequestMethod.Info;
    }

    public class DatabaseInfo
    {
        public string? ColorScheme { get; set; }
        public byte[]? Logo { get; set; }
        public string Version { get; set; }
    }
    
    public class InfoResponse : Response
    {
        public DatabaseInfo Info { get; set; }

        public InfoResponse() : base()
        {
            Info = new DatabaseInfo();
        }
    }

    public static class Extensions
    {

        public static T Status<T>(this T response, StatusCode status) where T : Response
        {
            response.Status = status;
            return response;
        }

    }

    public class RequestException : Exception
    {
        public StatusCode Status { get; set; }

        public RequestMethod Method { get; set; }

        public RequestException(string message, StatusCode status, Request request): base(message)
        {
            Status = status;
            Method = request.GetMethod();
        }
    }
}