using SharpChat.Commands;
using System.Globalization;
using System.Text;

namespace SharpChat {
    public class ChatUser(
        long userId,
        string userName,
        ChatColour colour,
        int rank,
        ChatUserPermissions perms,
        string nickName = "",
        ChatUserStatus status = ChatUserStatus.Online,
        string statusText = ""
    ) {
        public const int DEFAULT_SIZE = 30;
        public const int DEFAULT_MINIMUM_DELAY = 10000;
        public const int DEFAULT_RISKY_OFFSET = 5;

        public long UserId { get; } = userId;
        public string UserName { get; set; } = userName ?? throw new ArgumentNullException(nameof(userName));
        public ChatColour Colour { get; set; } = colour;
        public int Rank { get; set; } = rank;
        public ChatUserPermissions Permissions { get; set; } = perms;
        public bool IsSuper { get; set; }
        public string NickName { get; set; } = nickName;
        public ChatUserStatus Status { get; set; } = status;
        public string StatusText { get; set; } = statusText;

        public string LegacyName => string.IsNullOrWhiteSpace(NickName) ? UserName : $"~{NickName}";

        public string LegacyNameWithStatus {
            get {
                StringBuilder sb = new();

                if(Status == ChatUserStatus.Away) {
                    string statusText = StatusText.Trim();
                    StringInfo sti = new(statusText);
                    if(Encoding.UTF8.GetByteCount(statusText) > AFKCommand.MAX_BYTES
                        || sti.LengthInTextElements > AFKCommand.MAX_GRAPHEMES)
                        statusText = sti.SubstringByTextElements(0, Math.Min(sti.LengthInTextElements, AFKCommand.MAX_GRAPHEMES)).Trim();

                    sb.AppendFormat("<{0}>_", statusText.ToUpperInvariant());
                }

                sb.Append(LegacyName);

                return sb.ToString();
            }
        }

        public bool Can(ChatUserPermissions perm, bool strict = false) {
            ChatUserPermissions perms = Permissions & perm;
            return strict ? perms == perm : perms > 0;
        }

        public string Pack() {
            StringBuilder sb = new();

            sb.Append(UserId);
            sb.Append('\t');
            sb.Append(LegacyNameWithStatus);
            sb.Append('\t');
            sb.Append(Colour);
            sb.Append('\t');
            sb.Append(Rank);
            sb.Append(' ');
            sb.Append(Can(ChatUserPermissions.KickUser) ? '1' : '0');
            sb.Append(' ');
            sb.Append(Can(ChatUserPermissions.ViewLogs) ? '1' : '0');
            sb.Append(' ');
            sb.Append(Can(ChatUserPermissions.SetOwnNickname) ? '1' : '0');
            sb.Append(' ');
            sb.Append(Can(ChatUserPermissions.CreateChannel | ChatUserPermissions.SetChannelPermanent, true) ? '2' : (
                Can(ChatUserPermissions.CreateChannel) ? '1' : '0'
            ));

            return sb.ToString();
        }

        public bool NameEquals(string name) {
            return string.Equals(name, UserName, StringComparison.InvariantCultureIgnoreCase)
                || string.Equals(name, NickName, StringComparison.InvariantCultureIgnoreCase)
                || string.Equals(name, LegacyName, StringComparison.InvariantCultureIgnoreCase)
                || string.Equals(name, LegacyNameWithStatus, StringComparison.InvariantCultureIgnoreCase);
        }

        public override int GetHashCode() {
            return UserId.GetHashCode();
        }

        public static string GetDMChannelName(ChatUser user1, ChatUser user2) {
            return user1.UserId < user2.UserId
                ? $"@{user1.UserId}-{user2.UserId}"
                : $"@{user2.UserId}-{user1.UserId}";
        }
    }
}