using System;

namespace IGP.Multiplayer.Protocol
{
    // Json.NET accepts extensions (single quotes, comments, hex numbers, trailing
    // commas). Arena's JSON bytes fields require the JSON grammar itself.
    internal sealed class IGPJsonSyntax
    {
        private readonly string text;
        private int index;
        private IGPJsonSyntax(string text) { this.text = text; }
        internal static void Validate(string text)
        {
            var parser = new IGPJsonSyntax(text);
            parser.Value(0); parser.Space();
            if (parser.index != text.Length) throw Invalid();
        }
        private static Exception Invalid() => new InvalidOperationException("Invalid dynamic JSON syntax");
        private char Peek => index < text.Length ? text[index] : '\0';
        private void Space() { while (Peek == ' ' || Peek == '\t' || Peek == '\r' || Peek == '\n') index++; }
        private void Take(char c) { Space(); if (Peek != c) throw Invalid(); index++; }
        private void Value(int depth)
        {
            Space();
            char c = Peek;
            if (c == '{' || c == '[')
            {
                if (depth >= 8) throw Invalid();
                index++; Space(); char end = c == '{' ? '}' : ']';
                if (Peek == end) { index++; return; }
                while (true)
                {
                    if (c == '{') { Space(); String(); Take(':'); }
                    Value(depth + 1); Space();
                    if (Peek == end) { index++; return; }
                    Take(',');
                }
            }
            if (c == '"') { String(); return; }
            if (c == 't') { Literal("true"); return; }
            if (c == 'f') { Literal("false"); return; }
            if (c == 'n') { Literal("null"); return; }
            if (c == '-') index++;
            if (Peek == '0') index++;
            else { if (Peek < '1' || Peek > '9') throw Invalid(); Digits(); }
            if (Peek == '.') { index++; Digits(); }
            if (Peek == 'e' || Peek == 'E') { index++; if (Peek == '+' || Peek == '-') index++; Digits(); }
        }
        private void Digits()
        {
            int start = index;
            while (Peek >= '0' && Peek <= '9') index++;
            if (start == index) throw Invalid();
        }
        private void Literal(string expected)
        {
            foreach (char c in expected) { if (Peek != c) throw Invalid(); index++; }
        }
        private void String()
        {
            if (Peek != '"') throw Invalid(); index++;
            while (index < text.Length)
            {
                char c = text[index++];
                if (c == '"') return;
                if (c < 32) throw Invalid();
                if (c != '\\') continue;
                char escape = Peek; index++;
                if (escape == 'u')
                {
                    for (int i = 0; i < 4; i++)
                    {
                        char hex = Peek;
                        if (!(hex >= '0' && hex <= '9' || hex >= 'A' && hex <= 'F' || hex >= 'a' && hex <= 'f')) throw Invalid();
                        index++;
                    }
                }
                else if (escape == '\0' || "\"\\/bfnrt".IndexOf(escape) < 0) throw Invalid();
            }
            throw Invalid();
        }
    }
}
