<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TwixelEmotes
{
    public static class EmoteManager
    {
        internal static string Protocol
        {
            get
            {
                if (UseHttps)
                {
                    return "https://";
                }
                else
                {
                    return "http://";
                }
            }
        }

        private static string BaseUrl
        {
            get
            {
                return Protocol + "twitchemotes.com/api_cache/v2/";
            }
        }

        private static Dictionary<long, Emote> emotes;
        private static Dictionary<long, GlobalEmote> globalEmotes;
        private static Dictionary<string, GlobalEmote> globalEmotesByCode;
        private static Dictionary<long, Emote> basicEmotes;

        public static Dictionary<long, Emote> EmotesById
        {
            get
            {
                return emotes;
            }
        }

        public static Dictionary<string, Emote> EmotesByCode { get; private set; }

        public static Dictionary<string, Channel> ChannelsByName { get; private set; }

        public static Dictionary<long, Channel> ChannelsBySet { get; private set; }

        public static Dictionary<long, string> ChannelNameBySet { get; private set; }

        public static Dictionary<string, long> SetByChannelName { get; private set; }

        public static Dictionary<string, DuplicateSet> DuplicateSets { get; private set; }

        private static Channel globalChannel;

        public static TimeSpan AutoRefreshTime { get; set; }

        public static bool UseHttps { get; set; }

        public static bool Initialized { get; private set; }
 
        static EmoteManager()
        {
            Initialized = false;
            emotes = new Dictionary<long, Emote>();
            globalEmotes = new Dictionary<long, GlobalEmote>();
            basicEmotes = new Dictionary<long, Emote>();
            globalEmotesByCode = new Dictionary<string, GlobalEmote>();
            EmotesByCode = new Dictionary<string, Emote>();
            ChannelsByName = new Dictionary<string, Channel>();
            ChannelsBySet = new Dictionary<long, Channel>();
            ChannelNameBySet = new Dictionary<long, string>();
            SetByChannelName = new Dictionary<string, long>();
            DuplicateSets = new Dictionary<string, DuplicateSet>();
            globalChannel = null;
            AutoRefreshTime = TimeSpan.FromMinutes(30);
            UseHttps = true;
        }

        public static async Task Initialize(EmoteDataCache dataCache = null)
        {
            globalChannel = new Channel();
            globalChannel.Title = "--global--";
            globalChannel.Link = null;
            globalChannel.Description = null;
            globalChannel.Id = "--global--";
            globalChannel.Badge = null;
            globalChannel.Set = 0;

            Channel turboChannel33 = new Channel();
            turboChannel33.Title = "--twitch-turbo--";
            turboChannel33.Link = null;
            turboChannel33.Description = null;
            turboChannel33.Id = "--twitch-turbo--";
            turboChannel33.Badge = null;
            turboChannel33.Set = 33;

            Channel turboChannel42 = new Channel();
            turboChannel42.Title = "--twitch-turbo--";
            turboChannel42.Link = null;
            turboChannel42.Description = null;
            turboChannel42.Id = "--twitch-turbo--";
            turboChannel42.Badge = null;
            turboChannel42.Set = 42;

            if (dataCache == null)
            {
                await RetrieveGlobalEmotes();
                await RetrieveImages();
                await RetrieveSets();
                await RetrieveSubscriberEmotes();
                await RetrieveBasicEmotes();
            }
            else
            {
                await LoadGlobalEmotesFromString(dataCache.GlobalString, dataCache.GlobalTime);
                await LoadImagesFromString(dataCache.ImagesString, dataCache.ImagesTime);
                await LoadSetsFromString(dataCache.SetsString, dataCache.SetsTime);
                await LoadSubscriberEmotesFromString(dataCache.SubscriberString, dataCache.SubscriberTime);
                await LoadBasicEmotesFromString(dataCache.Basic0String, dataCache.Basic33String, dataCache.Basic42String, dataCache.BasicTime);
            }

            foreach (KeyValuePair<long, GlobalEmote> globalEmote in globalEmotes)
            {
                globalChannel.Emotes.Add(emotes[globalEmote.Key]);
            }
            foreach (KeyValuePair<long, Emote> basicEmote in basicEmotes)
            {
                if (basicEmote.Value.Set == 0)
                {
                    globalChannel.Emotes.Add(basicEmote.Value);
                }
                else if (basicEmote.Value.Set == 33)
                {
                    turboChannel33.Emotes.Add(basicEmote.Value);
                }
                else if (basicEmote.Value.Set == 42)
                {
                    turboChannel42.Emotes.Add(basicEmote.Value);
                }
                emotes.Add(basicEmote.Key, basicEmote.Value);
            }
            turboChannel33.Link = ChannelsByName["turbo"].Link;
            turboChannel33.Description = ChannelsByName["turbo"].Description;
            turboChannel33.Badge = ChannelsByName["turbo"].Badge;
            turboChannel42.Link = ChannelsByName["turbo"].Link;
            turboChannel42.Description = ChannelsByName["turbo"].Description;
            turboChannel42.Badge = ChannelsByName["turbo"].Badge;
            ChannelsByName.Add("--global--", globalChannel);
            ChannelsBySet.Add(0, globalChannel);
            Channel superTurbo = new Channel();
            superTurbo.Title = turboChannel33.Title;
            superTurbo.Link = turboChannel33.Link;
            superTurbo.Description = turboChannel33.Description;
            superTurbo.Id = turboChannel33.Id;
            superTurbo.Badge = turboChannel33.Badge;
            superTurbo.Set = turboChannel33.Set;
            superTurbo.Emotes.AddRange(turboChannel33.Emotes);
            superTurbo.Emotes.AddRange(turboChannel42.Emotes);
            ChannelsByName.Add("--twitch-turbo--", superTurbo);
            ChannelsBySet.Add(33, turboChannel33);
            ChannelsBySet.Add(42, turboChannel42);
            Initialized = true;
        }

        public static EmoteDataCache GetDataCache()
        {
            return new EmoteDataCache(JsonCache.globalString, JsonCache.globalLocalDate.Value.ToString("o"),
                JsonCache.subscriberString, JsonCache.subscriberLocalDate.Value.ToString("o"),
                JsonCache.setsString, JsonCache.setsLocalDate.Value.ToString("o"),
                JsonCache.imagesString, JsonCache.imagesLocalDate.Value.ToString("o"),
                JsonCache.basic0String, JsonCache.basic33String, JsonCache.basic42String, JsonCache.basicLocalDate.Value.ToString("o"));
        }

        public static async Task LoadGlobalEmotesFromString(string globalEmotesString, DateTime? globalTime)
        {<commit_msg>Turbo channel doesn't exist anymore<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TwixelEmotes
{
    public static class EmoteManager
    {
        internal static string Protocol
        {
            get
            {
                if (UseHttps)
                {
                    return "https://";
                }
                else
                {
                    return "http://";
                }
            }
        }

        private static string BaseUrl
        {
            get
            {
                return Protocol + "twitchemotes.com/api_cache/v2/";
            }
        }

        private static Dictionary<long, Emote> emotes;
        private static Dictionary<long, GlobalEmote> globalEmotes;
        private static Dictionary<string, GlobalEmote> globalEmotesByCode;
        private static Dictionary<long, Emote> basicEmotes;

        public static Dictionary<long, Emote> EmotesById
        {
            get
            {
                return emotes;
            }
        }

        public static Dictionary<string, Emote> EmotesByCode { get; private set; }

        public static Dictionary<string, Channel> ChannelsByName { get; private set; }

        public static Dictionary<long, Channel> ChannelsBySet { get; private set; }

        public static Dictionary<long, string> ChannelNameBySet { get; private set; }

        public static Dictionary<string, long> SetByChannelName { get; private set; }

        public static Dictionary<string, DuplicateSet> DuplicateSets { get; private set; }

        private static Channel globalChannel;

        public static TimeSpan AutoRefreshTime { get; set; }

        public static bool UseHttps { get; set; }

        public static bool Initialized { get; private set; }
 
        static EmoteManager()
        {
            Initialized = false;
            emotes = new Dictionary<long, Emote>();
            globalEmotes = new Dictionary<long, GlobalEmote>();
            basicEmotes = new Dictionary<long, Emote>();
            globalEmotesByCode = new Dictionary<string, GlobalEmote>();
            EmotesByCode = new Dictionary<string, Emote>();
            ChannelsByName = new Dictionary<string, Channel>();
            ChannelsBySet = new Dictionary<long, Channel>();
            ChannelNameBySet = new Dictionary<long, string>();
            SetByChannelName = new Dictionary<string, long>();
            DuplicateSets = new Dictionary<string, DuplicateSet>();
            globalChannel = null;
            AutoRefreshTime = TimeSpan.FromMinutes(30);
            UseHttps = true;
        }

        public static async Task Initialize(EmoteDataCache dataCache = null)
        {
            globalChannel = new Channel();
            globalChannel.Title = "--global--";
            globalChannel.Link = null;
            globalChannel.Description = null;
            globalChannel.Id = "--global--";
            globalChannel.Badge = null;
            globalChannel.Set = 0;

            Channel turboChannel33 = new Channel();
            turboChannel33.Title = "--twitch-turbo--";
            turboChannel33.Link = null;
            turboChannel33.Description = null;
            turboChannel33.Id = "--twitch-turbo--";
            turboChannel33.Badge = null;
            turboChannel33.Set = 33;

            Channel turboChannel42 = new Channel();
            turboChannel42.Title = "--twitch-turbo--";
            turboChannel42.Link = null;
            turboChannel42.Description = null;
            turboChannel42.Id = "--twitch-turbo--";
            turboChannel42.Badge = null;
            turboChannel42.Set = 42;

            if (dataCache == null)
            {
                await RetrieveGlobalEmotes();
                await RetrieveImages();
                await RetrieveSets();
                await RetrieveSubscriberEmotes();
                await RetrieveBasicEmotes();
            }
            else
            {
                await LoadGlobalEmotesFromString(dataCache.GlobalString, dataCache.GlobalTime);
                await LoadImagesFromString(dataCache.ImagesString, dataCache.ImagesTime);
                await LoadSetsFromString(dataCache.SetsString, dataCache.SetsTime);
                await LoadSubscriberEmotesFromString(dataCache.SubscriberString, dataCache.SubscriberTime);
                await LoadBasicEmotesFromString(dataCache.Basic0String, dataCache.Basic33String, dataCache.Basic42String, dataCache.BasicTime);
            }

            foreach (KeyValuePair<long, GlobalEmote> globalEmote in globalEmotes)
            {
                globalChannel.Emotes.Add(emotes[globalEmote.Key]);
            }
            foreach (KeyValuePair<long, Emote> basicEmote in basicEmotes)
            {
                if (basicEmote.Value.Set == 0)
                {
                    globalChannel.Emotes.Add(basicEmote.Value);
                }
                else if (basicEmote.Value.Set == 33)
                {
                    turboChannel33.Emotes.Add(basicEmote.Value);
                }
                else if (basicEmote.Value.Set == 42)
                {
                    turboChannel42.Emotes.Add(basicEmote.Value);
                }
                emotes.Add(basicEmote.Key, basicEmote.Value);
            }
            ChannelsByName.Add("--global--", globalChannel);
            ChannelsBySet.Add(0, globalChannel);
            Channel superTurbo = new Channel();
            superTurbo.Title = turboChannel33.Title;
            superTurbo.Link = turboChannel33.Link;
            superTurbo.Description = turboChannel33.Description;
            superTurbo.Id = turboChannel33.Id;
            superTurbo.Badge = turboChannel33.Badge;
            superTurbo.Set = turboChannel33.Set;
            superTurbo.Emotes.AddRange(turboChannel33.Emotes);
            superTurbo.Emotes.AddRange(turboChannel42.Emotes);
            ChannelsByName.Add("--twitch-turbo--", superTurbo);
            ChannelsBySet.Add(33, turboChannel33);
            ChannelsBySet.Add(42, turboChannel42);
            Initialized = true;
        }

        public static EmoteDataCache GetDataCache()
        {
            return new EmoteDataCache(JsonCache.globalString, JsonCache.globalLocalDate.Value.ToString("o"),
                JsonCache.subscriberString, JsonCache.subscriberLocalDate.Value.ToString("o"),
                JsonCache.setsString, JsonCache.setsLocalDate.Value.ToString("o"),
                JsonCache.imagesString, JsonCache.imagesLocalDate.Value.ToString("o"),
                JsonCache.basic0String, JsonCache.basic33String, JsonCache.basic42String, JsonCache.basicLocalDate.Value.ToString("o"));
        }

        public static async Task LoadGlobalEmotesFromString(string globalEmotesString, DateTime? globalTime)
        {
{'fraction_non_alphanumeric': 0.08824762437115707, 'fraction_numerical': 0.012017887087758524, 'mean_word_length': 1.900303951367781, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6931657', 'n_tokens_mistral': 3804, 'n_tokens_neox': 3582, 'n_words': 738}