import { Injectable } from "@angular/core";
import {
  createResourceGroupQuery,
  deleteResourceGroupQuery,
  editResourceGroupNameQuery,
  editResourceGroupOrderingQuery,
  editResourceGroupClosedQuery,
} from "./querys";
import { SubscriptionService } from "./subscription.service";
import { ResourceGroupCrypto } from "../crypto/top/resource-group";
import { WorkspaceSubscriptionResourceGroupEventRecord } from "./query-records/workspace-records";
import {
  SubscriptionServiceEvent,
  SubscriptionServiceEventType,
} from "./subscription-event";
import { AuthService } from "./auth.service";
import { ServerRestApiService } from "../services/server-rest-api.service";

@Injectable({
  providedIn: "root",
})
export class ResourceGroupService {
  constructor(
    private serverRestApiService: ServerRestApiService,
    private subscriptionService: SubscriptionService,
    private authService: AuthService
  ) {}

  // mutations with the resource group -------------------------------------------
  public createResourceGroup(name: string) {
    return this.serverRestApiService.mutate({
      query: createResourceGroupQuery,
      variables: {
        name,
      },
      encrypt: (variables, extra) => {
        return ResourceGroupCrypto.encrypt(variables["name"], this.authService.getSelfAccountKeyring())
          .then((encryptedName) => {
            variables["name"] = encryptedName;
            return variables;
          });
      },
    });
  }

  public deleteResourceGroup(id) {
    return this.serverRestApiService.mutate({
      query: deleteResourceGroupQuery,
      variables: {
        groupId: id,
      },
    });
  }

  public editResourceGroupName(id, name: string): Promise<any> {
    return this.serverRestApiService.mutate({
      query: editResourceGroupNameQuery,
      variables: {
        groupId: id,
        name,
      },
      encrypt: (variables, extra) => {
        return ResourceGroupCrypto.encrypt(variables["name"], this.authService.getSelfAccountKeyring())
          .then((encryptedName) => {
            variables["name"] = encryptedName;
            return variables;
          });
      },
    });
  }

  public editResourceGroupOrdering(id, ordering: number) {
    return this.serverRestApiService.mutate({
      query: editResourceGroupOrderingQuery,
      variables: {
        groupId: id,
        ordering,
      },
    });
  }

  public editResourceGroupClosed(id, closed: boolean) {
    return this.serverRestApiService.mutate({
      query: editResourceGroupClosedQuery,
      variables: {
        groupId: id,
        closed,
      },
    });
  }

  
}

/*
@Injectable({
  providedIn: "root",
})
export class ResourceGroupCryptoService {
  constructor(private authService: AuthService) {}

  public encryptResourceGroupData(data: string): Promise<Uint8Array> {

  }

  public decryptResourceGroupData(data: Uint8Array): Promise<string | void> {

  }
}
*/