export const JSONRPC = {
    url: "/jsonrpc/rpc",
    __id: 1,
    /**
     *
     * @param method
     * @param params
     * @returns Object
     * @private
     */
    __invoke: async function (method, params) {
        const request = await JSONRPC.__performRequest(method, params);

        if (!request.success) {
            console.error(request.result);
            throw new Error("Failed to invoke method " + method + " with params " + JSON.stringify(params));
        }

        return request.result;
    },
    __performRequest: async function (method, params) {
        const __this = this;
        const resp = await fetch(
            __this.url,
            {
                method: "POST",
                mode: "cors",
                cache: "no-cache",
                credentials: "include",
                headers: {
                    "Content-Type": "application/json"
                },
                redirect: "follow",
                referrerPolicy: "no-referrer",
                body: JSON.stringify({
                    jsonrpc: '2.0',
                    method: method,
                    params: params,
                    id: __this.__id++
                })
            }
        );
        const success = resp.status === 200;
        const result = (success ? (await resp.json()).result : {
            "error": true,
            "code": resp.status,
            "status": resp.statusText,
            "body": await resp.text()
        });

        return {
            "result": result,
            "success": success
        };
    }
};

export const TYPES = {};
JSONRPC.OVPN = {};

JSONRPC.OVPN.restartSystemService=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.External.API.Components.OVPN::restartSystemService", {});
};

JSONRPC.OVPN.getManagedRoutes=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.External.API.Components.OVPN::getManagedRoutes", {});
};
JSONRPC.SubscriptionManager = {};

JSONRPC.SubscriptionManager.isUpdating=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::isUpdating", {});
};

JSONRPC.SubscriptionManager.triggerUpdate=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::triggerUpdate", {});
};

JSONRPC.SubscriptionManager.writeLocalResourceFile=async function(name, description, domains, ASN, subnets, addresses, autoResolve, storage, subscribe){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::writeLocalResourceFile", {"name":name, "description":description, "domains":domains, "ASN":ASN, "subnets":subnets, "addresses":addresses, "autoResolve":autoResolve, "storage":storage, "subscribe":subscribe});
};

JSONRPC.SubscriptionManager.removeLocalResourceFile=async function(name, storage){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::removeLocalResourceFile", {"name":name, "storage":storage});
};

JSONRPC.SubscriptionManager.getLocalRepositories=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::getLocalRepositories", {});
};

JSONRPC.SubscriptionManager.getSubscribedResources=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::getSubscribedResources", {});
};

JSONRPC.SubscriptionManager.getAvailableResources=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::getAvailableResources", {});
};

JSONRPC.SubscriptionManager.setSubscribedResources=async function(resources){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.SubscriptionManager::setSubscribedResources", {"resources":resources});
};
JSONRPC.Auth = {};

JSONRPC.Auth.listTokens=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::listTokens", {});
};

JSONRPC.Auth.changePassword=async function(password){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::changePassword", {"password":password});
};

JSONRPC.Auth.removeToken=async function(token){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::removeToken", {"token":token});
};

JSONRPC.Auth.createAPIToken=async function(description){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::createAPIToken", {"description":description});
};

JSONRPC.Auth.startSessionByPassword=async function(password){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::startSessionByPassword", {"password":password});
};

JSONRPC.Auth.startSessionByToken=async function(token){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::startSessionByToken", {"token":token});
};

JSONRPC.Auth.isAuthenticated=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::isAuthenticated", {});
};

JSONRPC.Auth.logout=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::logout", {});
};

JSONRPC.Auth.createToken=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.Auth::createToken", {});
};
JSONRPC.System = {};

JSONRPC.System.shutdown=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::shutdown", {});
};

JSONRPC.System.getAppVersion=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getAppVersion", {});
};

JSONRPC.System.getEnabledComponents=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getEnabledComponents", {});
};

JSONRPC.System.setEnabledComponents=async function(components){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::setEnabledComponents", {"components":components});
};

JSONRPC.System.isConfigChanged=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::isConfigChanged", {});
};

JSONRPC.System.saveConfig=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::saveConfig", {});
};

JSONRPC.System.getVersionForUpdate=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getVersionForUpdate", {});
};

JSONRPC.System.getRepositoryTypes=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getRepositoryTypes", {});
};

JSONRPC.System.getAvailableComponents=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getAvailableComponents", {});
};

JSONRPC.System.getComponentConfig=async function(component){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getComponentConfig", {"component":component});
};

JSONRPC.System.setComponentConfig=async function(component, config){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::setComponentConfig", {"component":component, "config":config});
};

JSONRPC.System.getLogs=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getLogs", {});
};

JSONRPC.System.clearLogs=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::clearLogs", {});
};

JSONRPC.System.getVersionInfo=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getVersionInfo", {});
};

JSONRPC.System.setConfig=async function(config){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::setConfig", {"config":config});
};

JSONRPC.System.getConfig=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::getConfig", {});
};

JSONRPC.System.restart=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::restart", {});
};

JSONRPC.System.doUpdate=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.System::doUpdate", {});
};
JSONRPC.NetworkManager = {};

JSONRPC.NetworkManager.isUpdating=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.NetworkManager::isUpdating", {});
};

JSONRPC.NetworkManager.discoverBlockedDomainResources=async function(domain, server){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.NetworkManager::discoverBlockedDomainResources", {"domain":domain, "server":server});
};

JSONRPC.NetworkManager.triggerUpdate=async function(ignoreCache){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.NetworkManager::triggerUpdate", {"ignoreCache":ignoreCache});
};

JSONRPC.NetworkManager.getOutputResources=async function(){
	return await JSONRPC.__invoke("ru.kirillius.pf.sdn.web.RPC.NetworkManager::getOutputResources", {});
};
