using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public static class KF3Names
{
    public static string GetCharaName(int id)
    {
        var charaData = KF3AssetLibrary.Instance.CharaData.FirstOrDefault(c => c.id == id);
        if(charaData != null)
        {
            return $"{(String.IsNullOrEmpty(charaData.nameEn) ? charaData.name : charaData.nameEn)} {(String.IsNullOrEmpty(charaData.nickname) ? "" : charaData.nickname)}";
        }
        switch (id)
        {
            case 1:
                return "Dhole";
            case 2:
                return "Serval";
            case 3:
                return "Fennec";
            case 4:
                return "Common Raccoon";
            case 5:
                return "Tasmanian Devil";
            case 6:
                return "Small-clawed Otter";
            case 7:
                return "Jaguar";
            case 8:
                return "Alpaca Suri";
            case 9:
                return "Lion";
            case 10:
                return "Moose";
            case 11:
                return "Northern White-faced Owl";
            case 12:
                return "Eurasian Eagle Owl";
            case 13:
                return "Ezo Red Fox";
            case 14:
                return "Silver Fox";
            case 15:
                return "Golden Snub-nosed Monkey";
            case 16:
                return "Brown Bear";
            case 17:
                return "Sheep";
            case 18:
                return "Blackbuck";
            case 19:
                return "Californian Sea Otter";
            case 20:
                return "Australian Devil";
            case 21:
                return "Peach Panther";
            case 22:
                return "Meerkat";
            case 23:
                return "Chinese Spot-Billed Duck";
            case 24:
                return "Royal Penguin";
            case 25:
                return "Emperor Penguin";
            case 26:
                return "Humboldt Penguin";
            case 27:
                return "Rockhopper Penguin";
            case 28:
                return "Gentoo Penguin";
            case 29:
                return "Plains Zebra";
            case 30:
                return "Thomson's Gazelle";
            case 31:
                return "Hippopotamus";
            case 33:
                return "Malayan Tapir";
            case 34:
                return "Fossa";
            case 35:
                return "Indian Elephant";
            case 37:
                return "King Cobra";
            case 38:
                return "Southern Tamandua";
            case 39:
                return "Peafowl";
            case 41:
                return "Okapi";
            case 42:
                return "Japanese Crested Ibis";
            case 43:
                return "Scarlet Ibis";
            case 44:
                return "Sand Cat";
            case 45:
                return "Tsuchinoko";
            case 46:
                return "American Beaver";
            case 47:
                return "Black-tailed Prairie Dog";
            case 48:
                return "Aurochs";
            case 49:
                return "Arabian oryx";
            case 51:
                return "White Rhinoceros";
            case 52:
                return "Panther Chameleon";
            case 53:
                return "Shoebill";
            case 54:
                return "Giant Armadillo";
            case 56:
                return "Margay";
            case 57:
                return "Capybara";
            case 58:
                return "Campo Flicker";
            case 59:
                return "Gray Wolf";
            case 60:
                return "Reticulated Giraffe";
            case 61:
                return "African Wild Dog";
            case 62:
                return "Common Dolphin";
            case 63:
                return "Mammoth";
            case 64:
                return "White Tiger";
            case 65:
                return "Saber-Toothed Tiger";
            case 66:
                return "Greater Flamingo";
            case 67:
                return "Raccoon Dog";
            case 68:
                return "Black Leopard";
            case 69:
                return "Cheetah";
            case 70:
                return "Blue Whale";
            case 71:
                return "Giant Penguin";
            case 72:
                return "Holstein Friesian Cattle";
            case 73:
                return "Tibetan Fox";
            case 74:
                return "Atlantic Puffin";
            case 75:
                return "Silky Anteater";
            case 76:
                return "Great Auk";
            case 77:
                return "Manul";
            case 78:
                return "Giant Anteater";
            case 79:
                return "Aye-aye";
            case 83:
                return "Dromedary";
            case 84:
                return "Bactrian Camel";
            case 85:
                return "Western Lowland Gorilla";
            case 87:
                return "Common Ostrich";
            case 88:
                return "Reindeer";
            case 89:
                return "Black rhinoceros";
            case 91:
                return "Arctic Hare";
            case 92:
                return "Alpine Marmot";
            case 93:
                return "Chinese White Dolphin";
            case 94:
                return "Californian Sea Lion";
            case 95:
                return "Komodo Dragon";
            case 97:
                return "Common Slider";
            case 98:
                return "Genbu";
            case 99:
                return "Seiryu";
            case 100:
                return "Byakko";
            case 101:
                return "Suzaku";
            case 102:
                return "Shisa Lefty";
            case 103:
                return "Shisa Right";
            case 104:
                return "Oinarisama";
            case 105:
                return "Kyubikitsune";
            case 106:
                return "Bearded Seal";
            case 108:
                return "African Elephant";
            case 109:
                return "African Rock Python";
            case 110:
                return "Greater Rhea";
            case 112:
                return "Cat";
            case 113:
                return "Unicorn Whale";
            case 114:
                return "Inugamigyoubu";
            case 118:
                return "Giant Pangolin";
            case 119:
                return "Northern Goshawk";
            case 120:
                return "Greater Roadrunner";
            case 125:
                return "Bergman's Bear";
            case 127:
                return "Caracal";
            case 133:
                return "Golden Tabby Tiger";
            case 134:
                return "Kodiak Bear";
            case 136:
                return "Sivatherium";
            case 138:
                return "Giant Panda";
            case 140:
                return "Jinmengyo";
            case 141:
                return "Sky Fish";
            case 148:
                return "Japanese Wolf";
            case 149:
                return "Japanese Otter";
            case 151:
                return "Barbary Lion";
            case 153:
                return "Bald Eagle";
            case 154:
                return "Jungle Crow";
            case 155:
                return "Okinawa Habu";
            case 156:
                return "Bottlenose Dolphin";
            case 158:
                return "Leopard";
            case 161:
                return "Black Jaguar";
            case 165:
                return "Maltese Tiger";
            case 166:
                return "Mexico Salamander";
            case 168:
                return "Yatagarasu";
            case 170:
                return "Okinawa Rail";
            case 174:
                return "Lesser Panda";
            case 176:
                return "Chapman's Zebra";
            case 178:
                return "Chicken";
            case 179:
                return "Alpaca Huacaya";
            case 180:
                return "White Lion";
            case 182:
                return "Geoffroy's Cat";
            case 188:
                return "Red Fox";
            case 191:
                return "Bat-Eared Fox";
            case 193:
                return "Aardwolf";
            case 195:
                return "Dog";
            case 196:
                return "Ezo Brown Bear";
            case 197:
                return "Gambian Rat";
            case 199:
                return "King Penguin";
            case 200:
                return "Adélie Penguin";
            case 203:
                return "Blue Wildebeest";
            case 204:
                return "Black Wildebeest";
            case 207:
                return "Mountain Tapir";
            case 217:
                return "African Penguin";
            case 221:
                return "Salt-water Crocodile";
            case 222:
                return "Shiserval Right";
            case 223:
                return "Shiserval Lefty";
            case 233:
                return "Chestnut Thoroughbred Horse";
            case 234:
                return "Brown Thoroughbred Horse";
            case 235:
                return "White Thoroughbred Horse";
            case 236:
                return "Vampire Bat";
            case 237:
                return "Long-tailed Whiskered Bat";
            case 239:
                return "Black-Headed Ibis";
            case 241:
                return "Serval";
            case 242:
                return "Superb Bird-of-Paradise";
            case 243:
                return "Western Parotia";
            case 244:
                return "Degu";
            case 245:
                return "Long-tailed Chinchilla";
            case 253:
                return "Harp Seal";
            case 258:
                return "Striated Caracara";
            case 263:
                return "Greater Honeyguide";
            case 264:
                return "Scarlet Macaw";
            case 265:
                return "Resplendent Quetzal";
            case 267:
                return "Siberian Husky";
            case 273:
                return "Passenger Pigeon";
            case 276:
                return "Secretarybird";
            case 282:
                return "Greater Bird-of-Paradise";
            case 284:
                return "Indian Wolf";
            case 290:
                return "Emerald Tree Boa";
            case 291:
                return "Garden Tree Boa";
            case 301:
                return "Orca";
            case 302:
                return "Dog";
            case 307:
                return "Amur Tiger";
            case 314:
                return "Pronghorn";
            case 316:
                return "Jack-o'-Lantern";
            case 318:
                return "Cerberus";
            case 321:
                return "Cellval";
            case 322:
                return "Serval";
            case 323:
                return "Pig";
            case 327:
                return "White Serval";
            case 333:
                return "Tasmanian Devil";
            case 334:
                return "Blackbuck";
            case 335:
                return "Californian Sea Otter";
            case 336:
                return "Australian Devil";
            case 337:
                return "Common Dolphin";
            case 338:
                return "Blue Whale";
            case 339:
                return "Californian Sea Lion";
            case 340:
                return "Common Raccoon";
            case 341:
                return "Fennec";
            case 342:
                return "Fennec";
            case 343:
                return "キンシコウ";
            case 344:
                return "アイアイ";
            case 345:
                return "オオセンザンコウ";
            case 346:
                return "オオタカ";
            case 347:
                return "ホワイトライオン";
            case 349:
                return "Royal Penguin【PPPのプリンセス】";
            case 350:
                return "Southern Tamandua【一撃のポーズぅ！】";
            case 351:
                return "Tasmanian Devil";
            case 352:
                return "Lion";
            case 353:
                return "Blackbuck";
            case 354:
                return "Australian Devil";
            case 355:
                return "Malayan Tapir【やってみる…！】";
            case 356:
                return "Common Ostrich【占いのその先へ】";
            case 357:
                return "Chapman's Zebra【オシャレリーダー】";
            case 358:
                return "Common Raccoon【時代が来たのだ！】";
            case 359:
                return "Japanese Crested Ibis【疑惑の美声？】";
            case 360:
                return "Fennec【私の時代も来たかなー】";
            case 363:
                return "Sand Cat【いいこと思いつきました】";
            case 364:
                return "Dhole【頼れる副隊長】";
            case 365:
                return "Small-clawed Otter【笑顔をお届け！】";
            case 366:
                return "Tsuchinoko【頼られちゃ仕方ない】";
            case 367:
                return "Ezo Red Fox【ぷろげーまー】";
            case 368:
                return "Silver Fox【天才発明家・Ｇ】";
            case 369:
                return "Yak";
            case 380:
                return "Hello Kitty♥serval";
            case 381:
                return "Hello Mimmy♥serval";
            case 382:
                return "Jaguar【連撃の極意】";
            case 383:
                return "Black Jaguar【一撃の極意】";
            case 384:
                return "Common Dolphin【わっふいわっふーい！】";
            case 385:
                return "Meerkat【正義の家庭教師】";
            case 386:
                return "Caracal";
            case 387:
                return "Cellval";
            case 388:
                return "Phoenix";
            case 389:
                return "Unico";
            case 390:
                return "Bald Eagle【みんなでジャスティス】";
            case 391:
                return "Northern Goshawk【みんなでクールに】";
            case 396:
                return "Witch";
            case 397:
                return "Draco Centaur";
            case 399:
                return "Tachikoma Standard";
            case 400:
                return "Tachikoma Heavy-armed";
            case 401:
                return "Paraceratherium";
            case 901:
                return "";
            case 902:
                return "";
            case 995:
                return "Tachikoma Standard";
            case 996:
                return "Tachikoma Heavy-armed";
            case 997:
                return "White Serval";
            case 998:
                return "Dhole (Cutscene)";
            case 1001:
                return "Mirai";
            case 1002:
                return "Calenda";
            case 1003:
                return "Flicky";
            case 1004:
                return "Boss";
            case 1005:
                return "Boss (Radio)";
            case 1006:
                return "Kako";
            case 1007:
                return "Nana";
            case 1008:
                return "Calenda (Cutscene)";
            case 1009:
                return "Flicky (Damaged)";
            case 1010:
                return "Eyes";
            case 1011:
                return "RulerCell Chrysalis";
            case 1012:
                return "RulerCell";
            case 1013:
                return "Hello Kitty";
        }
        return id.ToString();
    }

    public static Dictionary<Func<int, bool>, string> CeruleanNames = new Dictionary<Func<int, bool>, string>();

    public static void LoadCeruleanNames()
    {
        string filePath = Path.Combine(Application.dataPath + "/../", "Celliens.txt");
        if (File.Exists(filePath))
        {
            var lines = File.ReadAllLines(filePath);
            foreach(var line in lines)
            {
                var split = line.Split(';');
                if (split.Length != 2) continue;
                int id = -1;
                if (!int.TryParse(split[0], out id)) continue;
                CeruleanNames.Add(x => x < id, split[1].Trim());
            }
            Error.Log(Color.green, $"Loaded {CeruleanNames.Count} Cellien Names");
        }
        else
        {
            Error.Log(Color.yellow, $"Celliens.txt not found, loading default list");
            CeruleanNames = new Dictionary<Func<int, bool>, string>
            {
                { x => x < 20010, "civile" },
                { x => x < 20020, "fangcell" },
                { x => x < 20030, "Zoospores" },
                { x => x < 20040, "Mikazuki" },
                { x => x < 20050, "volvox" },
                { x => x < 20056, "Tirucel" },
                { x => x < 20057, "Tiruchel (Majimun)" },
                { x => x < 20060, "Tiruchel" },
                { x => x < 20070, "Hunter Cell" },
                { x => x < 20080, "denwa" },
                { x => x < 20090, "bolt" },
                { x => x < 20100, "met" },
                { x => x < 20110, "Hwarin" },
                { x => x < 20120, "Koonin" },
                { x => x < 20130, "Octopus" },
                { x => x < 20140, "loco loco" },
                { x => x < 20150, "Horus Cell" },
                { x => x < 20162, "boat" },
                { x => x < 20171, "Santamet" },
                { x => x < 20180, "New Year's bolt" },
                { x => x < 20192, "kepeshsel" },
                { x => x < 20202, "Fang Cell (Christmas)" },
                { x => x < 20210, "Mikazuki (Christmas)" },
                { x => x < 20220, "Yavacell" },
                { x => x < 20230, "phage cell" },
                { x => x < 20240, "white fangcell" },
                { x => x < 20250, "White Mikazuki" },
                { x => x < 20260, "White Volvox" },
                { x => x < 20263, "Octopus (Majimun)" },
                { x => x < 20265, "Octopus (Mazimun)" },
                { x => x < 20270, "Octopus (Octopus)" },
                { x => x < 20310, "Tiruchel (Mazimun)" },
                { x => x < 20330, "Santamet" },
                { x => x < 20340, "Boombox" },
                { x => x < 20350, "loco loco (magimun)" },
                { x => x < 20360, "video camera" },
                { x => x < 20380, "door" },
                { x => x < 20420, "hosiery" },
                { x => x < 20430, "paramecium" },
                { x => x < 20441, "dioid fang cell" },
                { x => x < 20450, "Tachyrians" },
                { x => x < 20460, "Bolt" },
                { x => x < 20470, "Met" },
                { x => x < 20480, "Koonin" },
                { x => x < 20490, "mimicel" },
                { x => x < 20510, "horn cell" },
                { x => x < 21001, "sma" },
                { x => x < 21002, "akagau" },
                { x => x < 21003, "greengau" },
                { x => x < 21004, "bluegau" },
                { x => x < 21005, "akagau" },
                { x => x < 21006, "greengau" },
                { x => x < 21007, "blue-gau" },
                { x => x < 21008, "akagau" },
                { x => x < 21010, "aogau" },
                { x => x < 21030, "gotsun" },
                { x => x < 21036, "eyeball outlet (temporary)" },
                { x => x < 21040, "eyeball outlet" },
                { x => x < 21050, "Ikarin" },
                { x => x < 21059, "raincoat" },
                { x => x < 21060, "raincoat (majimun)" },
                { x => x < 21065, "builder shovel" },
                { x => x < 21067, "Builder's shovel (for checking site destruction)" },
                { x => x < 21068, "Builder Shovel (Mazimun)" },
                { x => x < 21080, "builder's shovel" },
                { x => x < 21090, "Senetocell" },
                { x => x < 21100, "black gau" },
                { x => x < 21112, "Black Gotsun" },
                { x => x < 21121, "Christmas Karin" },
                { x => x < 21130, "Kagamimochi gotsun" },
                { x => x < 21153, "Aogau (Majimun)" },
                { x => x < 21154, "akagau (majimun)" },
                { x => x < 21155, "green gau" },
                { x => x < 21160, "aogau (majimun)" },
                { x => x < 21170, "Gottsun (Mazimun)" },
                { x => x < 21180, "Raincoat (Mazimun)" },
                { x => x < 21190, "Eyeball outlet (Mazimun)" },
                { x => x < 21201, "Builder shovel (Mazimun)" },
                { x => x < 21210, "Ikarin (Mazimun)" },
                { x => x < 21220, "Sachiko (Mazimun)" },
                { x => x < 21224, "Sachiko" },
                { x => x < 21232, "Black Sachiko" },
                { x => x < 21240, "Sachiko" },
                { x => x < 21270, "Alex" },
                { x => x < 21273, "black senet cell" },
                { x => x < 21280, "black builder shovel" },
                { x => x < 21281, "Flicky" },
                { x => x < 21282, "Flicky right foot" },
                { x => x < 21290, "Flicky left foot" },
                { x => x < 21291, "Akagau first generation" },
                { x => x < 21293, "Aogau first generation" },
                { x => x < 21295, "Akagau 1st generation" },
                { x => x < 21310, "Aogau 1st generation" },
                { x => x < 21330, "Akagau 4th generation" },
                { x => x < 21340, "Seiryu the Holy Beast" },
                { x => x < 21350, "Holy Beast Byakko" },
                { x => x < 21360, "Holy Beast Suzaku" },
                { x => x < 21380, "holy beast gembu" },
                { x => x < 21390, "Akagau second generation" },
                { x => x < 21400, "Senetocer" },
                { x => x < 21410, "eyeball outlet (temporary)" },
                { x => x < 21420, "birdcell" },
                { x => x < 21430, "Test boss" },
                { x => x < 21470, "bird cell" },
                { x => x < 21480, "Triceragaon" },
                { x => x < 21486, "Midorigau (Majimun)" },
                { x => x < 21493, "Gotsun (Mazimun)" },
                { x => x < 21501, "Raincoat (Mazimun)" },
                { x => x < 21509, "Eyeball outlet (Mazimun)" },
                { x => x < 21523, "Builder shovel (Mazimun)" },
                { x => x < 21530, "Senet cell" },
                { x => x < 21550, "Sachiko (Mazimun)" },
                { x => x < 21560, "Dio cerulean terminus" },
                { x => x < 21570, "Chibi Dio Cerulean" },
                { x => x < 21610, "Midori gau (magimun)" },
                { x => x < 21620, "Black Senetocer" },
                { x => x < 40000, "Unknown" },
                {x => x <40010, "Gacha Capsule/Card"},
                {x => x <40200, "Gacha Box"},
                {x => x <40300, "Blue Gacha Capsule"},
                {x => x <40400, "Furniture Gacha"},
                { x => x != -1 , "Unknown" }
            };
        }
    }

    public static string GetEnemyName(int id)
    {
        var cases = CeruleanNames;
        return cases.First(kvp => kvp.Key(id)).Value;
    }

    public static string GetDressName(string id)
    {
        switch (id)
        {
            case "a": return "Default";
            case "aa": return "Default (V2)";
            case "b": return "Special";
            case "c": return "Tracksuit";
            case "d": return "Park Ranger";
            case "e": return "Maid";
            case "f": return "Event Costume 1";
            case "g": return "Various";
            case "h": return "Party Dress";
            case "i": return "School Uniform";
            case "j": return "Sports Outfit";
            case "k": return "Raincoat";
            case "l": return "Dojo (Y)";
            case "m": return "Dojo (G)";
            case "n": return "Dojo (B)";
            case "o": return "Dojo (R)";
            case "p": return "Orihime";
            case "q": return "Swimsuit 1";
            case "r": return "Swimsuit 2";
            case "s": return "XXX Swimsuit";
            case "t": return "Lifeguard";
            case "u": return "Harvest";
            case "v": return "Halloween";
            case "w": return "Maid 2";
        }
        return id;
    }
}

public class JsonHelper
{
    public static T[] getJsonArray<T>(string json)
    {
        string newJson = "{ \"array\": " + json + "}";
        Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
        return wrapper.array;
    }

    [Serializable]
    private class Wrapper<T>
    {
        public T[] array;
    }
}

[System.Serializable]
public class CharaData
{
    public int id;
    public string name;
    public string nameEn;
    public string nickname;
}