using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;

namespace TEAMModelOS.SDK.Helper.Common.JsonHelper.JsonPath
{
    public class JsonApiValueSystem : IJsonPathValueSystem
    {
        public bool HasMember(object value, string member)
        {
            // JsonElement document = (JsonElement)value;
            if (value is JsonElement document)
            {
                if (document.ValueKind is JsonValueKind.Object)
                {
                    return document.TryGetProperty(member, out JsonElement json);

                }

                if (document.ValueKind is JsonValueKind.Array)
                {
                    var index = ParseInt(member, -1);
                    return index >= 0 && index < document.EnumerateArray().ToArray().Length;
                }
            }

            return false;
        }

        public object GetMemberValue(object value, string member)
        {

            // JsonElement document = (JsonElement)value;
            if (value is JsonElement document)
            {
                if (document.ValueKind is JsonValueKind.Object)
                {
                    return document.GetProperty(member);
                }
                if (document.ValueKind is JsonValueKind.Array)
                {
                    var index = ParseInt(member, -1);
                    return document.EnumerateArray().ToArray()[index];
                }
            }
            return null;
        }

        public IEnumerable GetMembers(object value)
        {
            if (value is JsonElement document)
            {
                return document.EnumerateObject().Select(property => property.Name);
            }
            return null;
        }

        public bool IsObject(object value)
        {
            if (value is JsonElement document)
            {
                if (document.ValueKind is JsonValueKind.Array)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return false;
        }

        public bool IsArray(object value)
        {
            if (value is JsonElement document)
                if (document.ValueKind is JsonValueKind.Array)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            return false;
        }

        public bool IsPrimitive(object value)
        {
            if (value is JsonElement document)
            {
                if (value == null)
                {
                    throw new ArgumentNullException("value");
                }
                return !(document.ValueKind is JsonValueKind.Object) && !(document.ValueKind is JsonValueKind.Array);
            }
            return false;
        }
        int ParseInt(string s, int defaultValue) => int.TryParse(s, out int result) ? result : defaultValue;
    }
}
