using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.JsonPatch.Operations;
namespace System
{
    public static class JsonPatchHelper
    {
        public static T JsonAdd<T>(this T t, string path, object value) where T : class
        {
            JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
            jsonPatch.Operations.Add(new Operation<T>("add", path, null, value));
            jsonPatch.ApplyTo(t);
            return t;
        }
        public static T JsonRemove<T>(this T t, string path) where T : class
        {
            JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
            jsonPatch.Operations.Add(new Operation<T>("remove", path, null, null));
            jsonPatch.ApplyTo(t);
            return t;
        }
        public static T JsonReplace<T>(this T t, string path, object value) where T : class
        {
            JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
            jsonPatch.Operations.Add(new Operation<T>("replace", path, null, value));
            jsonPatch.ApplyTo(t);
            return t;
        }
        public static T JsonMove<T>(this T t, string from, string path) where T : class
        {
            JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
            jsonPatch.Operations.Add(new Operation<T>("move", path, from));
            jsonPatch.ApplyTo(t);
            return t;
        }
        public static T JsonCopy<T>(this T t, string from, string path) where T : class
        {
            JsonPatchDocument<T> jsonPatch = new JsonPatchDocument<T>();
            jsonPatch.Operations.Add(new Operation<T>("copy", path, from));
            jsonPatch.ApplyTo(t);
            return t;
        }
    }
}
