using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using AyCode.Core.Extensions;
using AyCode.Utils.Extensions;

namespace AyCode.Core.Consts
{
    public abstract class AcConst
    {
        public static Guid ProjectId;
        public static string ProjectSalt;
        
        public const int ImageUploadLimit = 4096 * 1024;

        public static int ConsentCount = 1;
        public static readonly Guid NullMessageInstanceId;

        public const int MinFullUserNameLength = 6;
        public const int MaxFullUserNameLength = 250;

        public const int MinUserNameLength = 3;
        public const int MaxUserNameLength = 255;

        public const int MinPlayerNameLength = MinUserNameLength;
        public const int MaxPlayerNameLength = MaxUserNameLength;

        public const int MinPasswordLength = 6;
        public const int MaxPasswordLength = 32;
        public const int MinUserTokenLength = 8;
        public const int MaxUserTokenLength = 12;

        public const int MinDomainLength = 6;
        public const int MaxDomainLength = 100;

        public const int MinDomainExtensionLength = 2;
        public const int MaxDomainExtensionLength = 15;

        public const int MaxTcpBodyLengthDefault = 65535;

        public const string NullMessageString = "_";
        public const string EmptyMessageString = "#";
        public const long NullMessageDateTime = -1;

        public static readonly string AnataBrandFolder;
        public static readonly string AnataRootCacheFolder;
        public static readonly string AnataUserMediaCacheFolder;
        public static readonly string AnataAssetBundleCacheFolder;

        public static readonly Guid RegionDefaultTemplateId;

        public static readonly Guid InitScene;
        public static readonly Guid LobbyRoom;

        /// <summary>
        /// MainScene, Lobby
        /// </summary>
        public static readonly Guid AnataCity;

        public static readonly Guid PlayerGarage;
        public static readonly Guid PlayerWardrobe;
        public static readonly Guid LoadingId;
        public static readonly Guid GlobalChat;

        /// <summary>
        /// RegionAdmin
        /// </summary>
        public static readonly Guid RegionAdminUserId;

        public static readonly Guid RegionAdminPlayerId;

        public static readonly Guid GamePlayer;
        public static readonly Guid AdultPlayer;
        public static readonly Guid BusinessPlayer;

        public static readonly Guid AnataWorldDomainId;
        public static readonly Guid AnataWorldBrandId;

        public static string[] AvailableDomainSuffixes = new string[] { "3d", "anata", "app", "art", "club", "game", "blog", "shop", "biz", "chat", "conf", "city", "com", "net", "cool", "dance", "date", "fun", "design", "digital", "estate", "metaverse", "mv", "events", "fans", "fashion", "makeup", "fin", "fm", "sport", "gallery", "io", "info", "job", "mev", "land", "world", "life", "live", "lol", "love", "market", "media", "museum", "news", "ngo", "ninja", "kiwi", "one", "org", "party", "pink", "press", "slide", "property", "pub", "race", "sale", "school", "science", "social", "study", "style", "support", "tattoo", "team", "tech", "theatre", "town", "trade", "travel", "tv", "university", "education", "video", "vip", "vision", "wiki", "work", "xxx", "yeti", "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi", "bj", "bm", "bn", "bo", "bq", "br", "bs", "bt", "bw", "by", "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm", "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sk", "sl", "sm", "sn", "so", "sr", "ss", "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", "za", "zm", "zw" };

        //TODO: kitörölni és DB-ből kiszedni! - J.
        //private static string _tiamProjectIdString = "684f34d1-163a-4077-918f-a9d9df5ce789";
        static AcConst()
        {
            ProjectId = Guid.Parse(AcEnv.AppConfiguration["AyCode:ProjectId"].ToLower());
            ProjectSalt = GenerateProjectSalt(ProjectId.ToString("N").ToLower());

            //var anataFolder = AcDomain.IsProductVersion ? "Anata" : "AnataDev";

            //AnataRootCacheFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "AyCode", anataFolder);
            //AnataUserMediaCacheFolder = Path.Combine(AnataRootCacheFolder, "MediaContents");
            //AnataAssetBundleCacheFolder = Path.Combine(AnataRootCacheFolder, "AssetBundles");

            //AnataBrandFolder = Path.Combine(AnataRootCacheFolder, "Brands");

            //NullMessageInstanceId = Guid.Empty;

            //RegionDefaultTemplateId = Guid.Parse(AntDomain.IsProductVersion ? "6201b570-09f2-4cce-93b8-0e92d37b9b6b" : "ac087b5a-1dff-48c0-ab7b-c3a5defa0d9e");

            //InitScene = Guid.Parse("2db0be7b-5393-4b0f-a3f6-868155a6e62f");
            //LobbyRoom = Guid.Parse("73a7dbc6-c85f-40f7-88d5-d7baecdbb961");
            //AnataCity = Guid.Parse("dec9a00c-3fad-438d-aaa4-2109a7d5451f");
            //LoadingId = Guid.Parse("3d7b7c8c-3eed-4625-a727-0caf665d4208");
            //PlayerGarage = Guid.Parse("6ec1f5d7-57e9-440f-a486-20be5782f86a");
            //PlayerWardrobe = Guid.Parse("69acf032-dc75-4fae-badb-1fe8473f664f");

            //GlobalChat = Guid.Parse("ab5efc63-c654-41cd-b0a6-fa6aecc02d58");

            //RegionAdminUserId = Guid.Parse("51B53FCC-CE85-473D-8E00-F42A7F0DBE1D");
            //RegionAdminPlayerId = Guid.Parse("4ADF8015-C127-4E98-A022-B7407DBD3A38");

            //GamePlayer = Guid.Parse("E023500F-D024-4D9F-A8F9-ECDF54382961");
            //AdultPlayer = Guid.Parse("6A5E3C40-2022-4DFF-A362-396B73F15A69");
            //BusinessPlayer = Guid.Parse("F73BFDFC-3A92-4A14-B09C-F5ED22BF4027");

            //AnataWorldDomainId = Guid.Parse("8EFA53B3-7114-41E2-98F1-D690ECF509D2");
            //AnataWorldBrandId = Guid.Parse("99265775-4B19-4975-B338-EB0F2F890806");
        }

        //SEMMILYEN KÖRÜLMÉNYEK KÖZÖTT SE VÁLTOZTASD MEG METÓDUS LOGIKÁJÁT!!! Különben senki sem fog tudni Login-olni! - J.
        public static string GenerateProjectSalt(string projectIdString)
        {
            if (projectIdString.IsNullOrWhiteSpace()) return string.Empty;

            var projectSalt = new StringBuilder();

            var projectIdChars = projectIdString.ToCharArray();
            var mid = (projectIdChars.Length + 1) / 2;

            var firstHalf = projectIdChars.Take(mid).Reverse();
            var secondHalf = projectIdChars.Skip(mid).Reverse();
            var mixedChars = firstHalf.MixCharacters(secondHalf).ToArray();

            for (var i = mixedChars.Length - 1; i >= 0; i--)
            {
                projectSalt.Append(mixedChars[i]);

                mixedChars[i] = ' ';
                projectIdChars[i] = ' ';
            }

            return projectSalt.ToString();
        }
    }
}
