import { CloudlyApiClient } from './classes.cloudlyapiclient.js';
import * as plugins from './plugins.js';

export class Cluster implements plugins.servezoneInterfaces.data.ICluster {
  // STATIC
  public static async getClusterById(cloudlyClientRef: CloudlyApiClient, clusterIdArg: string) {
    const getClusterByIdTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.cluster.IReq_Any_Cloudly_GetClusterById>(
      'getClusterById'
    );
    const response = await getClusterByIdTR.fire({
      identity: cloudlyClientRef.identity,
      clusterId: clusterIdArg,
    });
    const newCluster = new Cluster(cloudlyClientRef);
    Object.assign(newCluster, response.cluster);
    return newCluster;
  }

  public static async getClusters(cloudlyClientRef: CloudlyApiClient) {
    const getClustersTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.cluster.IReq_Any_Cloudly_GetClusters>(
      'getClusters'
    );
    const response = await getClustersTR.fire({
      identity: cloudlyClientRef.identity,
    });
    const clusterConfigs: Cluster[] = [];
    for (const clusterConfig of response.clusters) {
      const newCluster = new Cluster(cloudlyClientRef);
      Object.assign(newCluster, clusterConfig);
      clusterConfigs.push(newCluster);
    }
    return clusterConfigs;
  }

  public static async createCluster(cloudlyClientRef: CloudlyApiClient, clusterNameArg: string) {
    const createClusterTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.cluster.IRequest_CreateCluster>(
      'createCluster'
    );
    const response = await createClusterTR.fire({
      identity: cloudlyClientRef.identity,
      clusterName: clusterNameArg,
    });
    const newCluster = new Cluster(cloudlyClientRef);
    Object.assign(newCluster, response.cluster);
    return newCluster;
  }

  // INSTANCE
  public id: string;
  public data: plugins.servezoneInterfaces.data.ICluster['data'];
  public cloudlyClientRef: CloudlyApiClient;

  constructor(cloudlyClientRef: CloudlyApiClient) {
    this.cloudlyClientRef = cloudlyClientRef;
  }


  public async update() {
    const updateClusterTR = this.cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.cluster.IReq_Any_Cloudly_UpdateCluster>(
      'updateCluster'
    );
    const response = await updateClusterTR.fire({
      identity: this.cloudlyClientRef.identity,
      clusterData: this.data,
    });

    const resultClusterData = response.resultCluster.data;
    plugins.smartexpect.expect(resultClusterData).toEqual(this.data);

    return this;
  }

  public async delete(cloudlyClientRef: CloudlyApiClient, clusterIdArg: string) {
    const deleteClusterTR = cloudlyClientRef.typedsocketClient.createTypedRequest<plugins.servezoneInterfaces.requests.cluster.IReq_Any_Cloudly_DeleteClusterById>(
      'deleteClusterById'
    );
    const response = await deleteClusterTR.fire({
      identity: cloudlyClientRef.identity,
      clusterId: this.id,
    });
    plugins.smartexpect.expect(response.ok).toBeTrue();
    return null;
  }
}