using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using Penumbra.GameData.Enums;

namespace Glamourer.Services;

public class CodeService
{
    private readonly Configuration _config;
    private readonly SHA256        _hasher = SHA256.Create();

    public enum Sizing
    {
        None,
        Dwarf,
        Giant,
    }

    public bool   EnabledClown      { get; private set; }
    public bool   EnabledEmperor    { get; private set; }
    public bool   EnabledIndividual { get; private set; }
    public Sizing EnabledSizing     { get; private set; }
    public Race   EnabledOops       { get; private set; }
    public bool   EnabledArtisan    { get; private set; }

    public CodeService(Configuration config)
    {
        _config = config;
        Load();
    }

    private void Load()
    {
        var changes = false;
        for (var i = 0; i < _config.Codes.Count; ++i)
        {
            var enabled = CheckCode(_config.Codes[i].Code);
            if (enabled == null)
            {
                _config.Codes.RemoveAt(i--);
                changes = true;
            }
            else
            {
                var value = _config.Codes[i].Enabled;
                enabled(value);
            }
        }

        if (changes)
            _config.Save();
    }

    public bool AddCode(string name)
    {
        if (CheckCode(name) == null || _config.Codes.Any(p => p.Code == name))
            return false;

        _config.Codes.Add((name, false));
        _config.Save();
        return true;
    }

    public Action<bool>? CheckCode(string name)
    {
        using var stream = new MemoryStream(Encoding.UTF8.GetBytes(name));
        var       sha    = (ReadOnlySpan<byte>)_hasher.ComputeHash(stream);
        return sha switch
        {
            _ when CodeClown.SequenceEqual(sha)      => v => EnabledClown = v,
            _ when CodeEmperor.SequenceEqual(sha)    => v => EnabledEmperor = v,
            _ when CodeIndividual.SequenceEqual(sha) => v => EnabledIndividual = v,
            _ when CodeDwarf.SequenceEqual(sha)      => v => EnabledSizing = v ? Sizing.Dwarf : Sizing.None,
            _ when CodeGiant.SequenceEqual(sha)      => v => EnabledSizing = v ? Sizing.Giant : Sizing.None,
            _ when CodeOops1.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Hyur : Race.Unknown,
            _ when CodeOops2.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Elezen : Race.Unknown,
            _ when CodeOops3.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Lalafell : Race.Unknown,
            _ when CodeOops4.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Miqote : Race.Unknown,
            _ when CodeOops5.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Roegadyn : Race.Unknown,
            _ when CodeOops6.SequenceEqual(sha)      => v => EnabledOops = v ? Race.AuRa : Race.Unknown,
            _ when CodeOops7.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Hrothgar : Race.Unknown,
            _ when CodeOops8.SequenceEqual(sha)      => v => EnabledOops = v ? Race.Viera : Race.Unknown,
            _ when CodeArtisan.SequenceEqual(sha)    => v => EnabledArtisan = v,
            _                                        => null,
        };
    }

    public void VerifyState()
    {
        if (EnabledSizing == Sizing.None && EnabledOops == Race.Unknown)
            return;

        for (var i = 0; i < _config.Codes.Count; ++i)
        {
            var (code, enabled) = _config.Codes[i];
            if (!enabled)
                continue;

            using var stream = new MemoryStream(Encoding.UTF8.GetBytes(code));
            var       sha    = (ReadOnlySpan<byte>)_hasher.ComputeHash(stream);
            var _ = EnabledSizing switch
            {
                Sizing.Dwarf when sha.SequenceEqual(CodeGiant) => _config.Codes[i] = (code, false),
                Sizing.Giant when sha.SequenceEqual(CodeDwarf) => _config.Codes[i] = (code, false),
                _                                              => (string.Empty, false),
            };

            var race = OopsRace(sha);
            if (race is not Race.Unknown && race != EnabledOops)
                _config.Codes[i] = (code, false);
        }
    }

    private static Race OopsRace(ReadOnlySpan<byte> sha)
        => sha switch
        {
            _ when CodeOops1.SequenceEqual(sha) => Race.Hyur,
            _ when CodeOops2.SequenceEqual(sha) => Race.Elezen,
            _ when CodeOops3.SequenceEqual(sha) => Race.Lalafell,
            _ when CodeOops4.SequenceEqual(sha) => Race.Miqote,
            _ when CodeOops5.SequenceEqual(sha) => Race.Roegadyn,
            _ when CodeOops6.SequenceEqual(sha) => Race.AuRa,
            _ when CodeOops7.SequenceEqual(sha) => Race.Hrothgar,
            _ when CodeOops8.SequenceEqual(sha) => Race.Viera,
            _                                   => Race.Unknown,
        };

    // @formatter:off
    private static ReadOnlySpan<byte> CodeClown      => new byte[] { 0xC4, 0xEE, 0x1D, 0x6F, 0xC5, 0x5D, 0x47, 0xBE, 0x78, 0x63, 0x66, 0x86, 0x81, 0x15, 0xEB, 0xFA, 0xF6, 0x4A, 0x90, 0xEA, 0xC0, 0xE4, 0xEE, 0x86, 0x69, 0x01, 0x8E, 0xDB, 0xCC, 0x69, 0xD1, 0xBD };
    private static ReadOnlySpan<byte> CodeEmperor    => new byte[] { 0xE2, 0x2D, 0x3E, 0x57, 0x16, 0x82, 0x65, 0x98, 0x7E, 0xE6, 0x8F, 0x45, 0x14, 0x7D, 0x65, 0x31, 0xE9, 0xD8, 0xDB, 0xEA, 0xDC, 0xBF, 0xEE, 0x2A, 0xBA, 0xD5, 0x69, 0x96, 0x78, 0x34, 0x3B, 0x57 };
    private static ReadOnlySpan<byte> CodeIndividual => new byte[] { 0x95, 0xA4, 0x71, 0xAC, 0xA3, 0xC2, 0x34, 0x94, 0xC1, 0x65, 0x07, 0xF3, 0x7F, 0x93, 0x57, 0xEE, 0xE3, 0x04, 0xC0, 0xE8, 0x1B, 0xA0, 0xE2, 0x08, 0x68, 0x02, 0x8D, 0xAD, 0x76, 0x03, 0x9B, 0xC5 };
    private static ReadOnlySpan<byte> CodeDwarf      => new byte[] { 0x55, 0x97, 0xFE, 0xE9, 0x78, 0x64, 0xE8, 0x2F, 0xCD, 0x25, 0xD1, 0xAE, 0xDF, 0x35, 0xE6, 0xED, 0x03, 0x78, 0x54, 0x1D, 0x56, 0x22, 0x34, 0x75, 0x4B, 0x96, 0x6F, 0xBA, 0xAC, 0xEC, 0x00, 0x46 };
    private static ReadOnlySpan<byte> CodeGiant      => new byte[] { 0x6E, 0xBB, 0x91, 0x1D, 0x67, 0xE3, 0x00, 0x07, 0xA1, 0x0F, 0x2A, 0xF0, 0x26, 0x91, 0x38, 0x63, 0xD3, 0x52, 0x82, 0xF7, 0x5D, 0x93, 0xE8, 0x83, 0xB1, 0xF6, 0xB9, 0x69, 0x78, 0x20, 0xC4, 0xCE };
    private static ReadOnlySpan<byte> CodeOops1      => new byte[] { 0x4C, 0x51, 0xE2, 0x38, 0xEF, 0xAD, 0x84, 0x0E, 0x4E, 0x11, 0x0F, 0x5E, 0xDE, 0x45, 0x41, 0x9F, 0x6A, 0xF6, 0x5F, 0x5B, 0xA8, 0x91, 0x64, 0x22, 0xEE, 0x62, 0x97, 0x3C, 0x78, 0x18, 0xCD, 0xAF };  
    private static ReadOnlySpan<byte> CodeOops2      => new byte[] { 0x3D, 0x5B, 0xA9, 0x62, 0xCE, 0xBE, 0x52, 0xF5, 0x94, 0x2A, 0xF9, 0xB7, 0xCF, 0xD9, 0x24, 0x2B, 0x38, 0xC7, 0x4F, 0x28, 0x97, 0x29, 0x1D, 0x01, 0x13, 0x53, 0x44, 0x11, 0x15, 0x6F, 0x9B, 0x56 };  
    private static ReadOnlySpan<byte> CodeOops3      => new byte[] { 0x85, 0x8D, 0x5B, 0xC2, 0x66, 0x53, 0x2E, 0xB9, 0xE9, 0x85, 0xE5, 0xF8, 0xD3, 0x75, 0x18, 0x7C, 0x58, 0x55, 0xD4, 0x8C, 0x8E, 0x5F, 0x58, 0x2E, 0xF3, 0xF1, 0xAE, 0xA8, 0xA0, 0x81, 0xC6, 0x0E };  
    private static ReadOnlySpan<byte> CodeOops4      => new byte[] { 0x44, 0x73, 0x8C, 0x39, 0x5A, 0xF1, 0xDB, 0x5F, 0x62, 0xA1, 0x6E, 0x5F, 0xE6, 0x97, 0x9E, 0x90, 0xD7, 0x5C, 0x97, 0x67, 0xB6, 0xC7, 0x99, 0x61, 0x36, 0xCA, 0x34, 0x7E, 0xB9, 0xAC, 0xC3, 0x76 };  
    private static ReadOnlySpan<byte> CodeOops5      => new byte[] { 0xB7, 0x25, 0x73, 0xDB, 0xBE, 0xD0, 0x49, 0xFB, 0xFF, 0x9C, 0x32, 0x21, 0xB0, 0x8A, 0x2C, 0x0C, 0x77, 0x46, 0xD5, 0xCF, 0x0E, 0x63, 0x2F, 0x91, 0x85, 0x8B, 0x55, 0x5C, 0x4D, 0xD2, 0xB9, 0xB8 };  
    private static ReadOnlySpan<byte> CodeOops6      => new byte[] { 0x69, 0x93, 0xAF, 0xE4, 0xB8, 0xEC, 0x5F, 0x40, 0xEB, 0x8A, 0x6F, 0xD1, 0x9B, 0xD9, 0x56, 0x0B, 0xEA, 0x64, 0x79, 0x9B, 0x54, 0xA1, 0x41, 0xED, 0xBC, 0x3E, 0x6E, 0x5C, 0xF1, 0x23, 0x60, 0xF8 };  
    private static ReadOnlySpan<byte> CodeOops7      => new byte[] { 0x41, 0xEC, 0x65, 0x05, 0x8D, 0x20, 0x68, 0x5A, 0xB7, 0xEB, 0x92, 0x15, 0x43, 0xCF, 0x15, 0x05, 0x27, 0x51, 0xFE, 0x20, 0xC9, 0xB6, 0x2B, 0x84, 0xD9, 0x6A, 0x49, 0x5A, 0x5B, 0x7F, 0x2E, 0xE7 };  
    private static ReadOnlySpan<byte> CodeOops8      => new byte[] { 0x16, 0xFF, 0x63, 0x85, 0x1C, 0xF5, 0x34, 0x33, 0x67, 0x8C, 0x46, 0x8E, 0x3E, 0xE3, 0xA6, 0x94, 0xF9, 0x74, 0x47, 0xAA, 0xC7, 0x29, 0x59, 0x1F, 0x6C, 0x6E, 0xF2, 0xF5, 0x87, 0x24, 0x9E, 0x2B };
    private static ReadOnlySpan<byte> CodeArtisan    => new byte[] { 0xDE, 0x01, 0x32, 0x1E, 0x7F, 0x22, 0x80, 0x3D, 0x76, 0xDF, 0x74, 0x0E, 0xEC, 0x33, 0xD3, 0xF4, 0x1A, 0x98, 0x9E, 0x9D, 0x22, 0x5C, 0xAC, 0x3B, 0xFE, 0x0B, 0xC2, 0x13, 0xB9, 0x91, 0x24, 0x61 };
    // @formatter:on
}
