using System;
using System.Collections.Generic;
using System.Net;
using InABox.Core;
using Newtonsoft.Json;

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 enum StatusCode
    {
        OK,
        Incomplete,
        Error,
        Unauthenticated,
        BadServer
    }

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

        public StatusCode Status { get; set; }

        public List<string> Messages { get; }

    }

    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 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 class QueryResponse<TEntity> : BaseResponse<TEntity> where TEntity : Entity, new()
    {
        public CoreTable? Items { get; set; }
    }

    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 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 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 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 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 class MultiQueryResponse : Response
    {
        public Dictionary<string, CoreTable> Tables { get; set; }
    }

    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 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 class Check2FAResponse : Response
    {
        public bool Valid { get; set; }
    }

    public class PingRequest : Request { }

    public class PingResponse : Response { }
    
    public class InfoRequest : Request { }

    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;
        }

    }
}