import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {CustomOptions} from "./servicesUtils/customOptions.class";
import {PluginTemplate} from "../utils/entities/adminTool/pluginTemplate";
import {Plugin} from "../utils/entities/adminTool/plugin";
import {properties} from "../../../environments/environment";

@Injectable()
export class PluginsService {

  constructor(private http: HttpClient) {
  }
  getPluginTemplates(api: string, pageId) {
    return this.http.get<Array<PluginTemplate>>(api + 'pluginTemplates' + (pageId ? '/page/' + pageId : ''))

  }

  savePluginTemplate(pluginTemplate: PluginTemplate, api: string) {
    return this.http.post<PluginTemplate>(api + 'pluginTemplate/save', pluginTemplate, CustomOptions.getAuthOptionsWithBody());
  }

  updatePluginTemplateOrder(pluginTemplate: PluginTemplate, api: string, position) {
    return this.http.post<PluginTemplate>(api + 'pluginTemplate/save/order/' + position, pluginTemplate, CustomOptions.getAuthOptionsWithBody());
  }

  updatePluginOrder(plugin: Plugin, api: string, position, community) {
    return this.http.post<Plugin>(api + 'community/'+community+'/plugin/save/order/' + position, plugin, CustomOptions.getAuthOptionsWithBody());
  }

  savePlugin(plugin, api: string, community) {
    return this.http.post<Plugin>(api + 'community/' + community + '/plugin/save', JSON.stringify(plugin), CustomOptions.getAuthOptionsWithBody());
  }

  deletePluginTemplate(id, api: string) {
    return this.http.delete<PluginTemplate>(api + 'pluginTemplate/' + id, CustomOptions.getAuthOptionsWithBody());
  }

  countPluginTemplatePerPage(api: string, pid: string) {
    return this.http.get(api + properties.adminToolsPortalType + '/' + pid + '/pluginTemplate/page/count');
  }

  countPluginTemplatePerPageForAllPortals(api: string) {
    return this.http.get(api + '/pluginTemplate/page/count');
  }

  getPluginsByPage(api: string, pid: string, pageId: string) {
    return this.http.get<Array<Plugin>>(api + properties.adminToolsPortalType + '/' + pid + '/plugins/page/' + pageId);
  }

  getPluginTemplatesByPage(api: string, pid: string, pageId: string) {
    return this.http.get<Array<PluginTemplate>>(api + properties.adminToolsPortalType + '/' + pid + '/pluginTemplates/page/' + pageId);
  }
  getPluginsByPageRoute(api:string, pid:string, route:string){
    let url = api  + 'community/' +pid+'/plugins/page/route?route=' + route;
    return this.http.get<Array<Plugin>>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url) + (properties.forceCacheReload?'&forceReload=true':'')) : url);
  }
  getPluginTemplatesByPageRoute(api:string, pid:string, route:string){
    let url =   api + 'community/' + pid + '/pluginTemplates/page/route?route=' + route;
    return this.http.get<Array<PluginTemplate>>((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url) + (properties.forceCacheReload?'&forceReload=true':'')) : url);
  }

  togglePlugin(id: string, status: boolean, api: string, community) {
    return this.http.post(api + 'community/' + community + '/plugin/status/' + id, status, CustomOptions.getAuthOptionsWithBody());
  }
}
