import { Injectable } from '@angular/core';
import { ConfigService } from '@shared/config/config.service';
import { FleioApiService } from '../../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { IProjectModel } from './model/project.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';
import { IProjectComputeQuotasModel } from '@fleio-api/openstack/project/model/project-compute-quotas.model';
import { IProjectVolumeQuotasModel } from '@fleio-api/openstack/project/model/project-volume-quotas.model';
import { IProjectNetworkQuotasModel } from '@fleio-api/openstack/project/model/project-network-quotas.model';
import { IProjectDatabaseQuotasModel } from '@fleio-api/openstack/project/model/project-trove-quotas.model';
import { IProjectClusterQuotasModel } from '@fleio-api/openstack/project/model/project-cluster-quotas.model';
import { IRoleModel } from '@fleio-api/client-user/roles/model/role.model';
import { IApiUserModel } from '@fleio-api/openstack/model/api-user.model';

@Injectable({
  providedIn: 'root'
})
export class ProjectsApiService extends FleioApiService<IProjectModel> {
  // noinspection JSUnusedGlobalSymbols
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/projects'));
  }

  deleteProject(id: FleioId, deleteAllResources: boolean): Observable<any> {
    return this.objectPostAction(id, 'delete_project', {
      delete_all_resources: deleteAllResources,
    });
  }

  getComputeQuotas(id: FleioId, regionId: FleioId): Observable<IProjectComputeQuotasModel> {
    return this.objectGetAction(
      id, 'get_compute_quotas', {region_id: regionId},
    ) as unknown as Observable<IProjectComputeQuotasModel>;
  }

  setComputeQuotas(id: FleioId, quotas: IProjectComputeQuotasModel) {
    return this.objectPostAction(id, 'set_compute_quotas', quotas);
  }

  getDatabaseQuotas(id: FleioId, regionId: FleioId): Observable<IProjectDatabaseQuotasModel> {
    return this.objectGetAction(
      id, 'get_database_quotas', {region_id: regionId},
    ) as unknown as Observable<IProjectDatabaseQuotasModel>;
  }

  setDatabaseQuotas(id: FleioId, quotas: IProjectDatabaseQuotasModel) {
    return this.objectPostAction(id, 'set_database_quotas', quotas);
  }

  getClusterQuotas(id: FleioId, regionId: FleioId): Observable<IProjectClusterQuotasModel> {
    return this.objectGetAction(
      id, 'get_cluster_quotas', {region_id: regionId},
    ) as unknown as Observable<IProjectClusterQuotasModel>;
  }

  setClusterQuotas(id: FleioId, quotas: IProjectClusterQuotasModel) {
    return this.objectPostAction(id, 'set_cluster_quotas', quotas);
  }

  getVolumeQuotas(id: FleioId, regionId: FleioId): Observable<IProjectVolumeQuotasModel> {
    return this.objectGetAction(
      id, 'get_volume_quotas', {region_id: regionId},
    ) as unknown as Observable<IProjectVolumeQuotasModel>;
  }

  setVolumeQuotas(id: FleioId, quotas: IProjectVolumeQuotasModel) {
    return this.objectPostAction(id, 'set_volume_quotas', quotas);
  }

  getNetworkQuotas(id: FleioId, regionId: FleioId): Observable<IProjectNetworkQuotasModel> {
    return this.objectGetAction(
      id, 'get_network_quotas', {region_id: regionId},
    ) as unknown as Observable<IProjectNetworkQuotasModel>;
  }

  setNetworkQuotas(id: FleioId, quotas: IProjectNetworkQuotasModel) {
    return this.objectPostAction(id, 'set_network_quotas', quotas);
  }

  getAvailableRoles(): Observable<any> {
    return this.getAction('get_available_roles', {});
  }

  getRoleAssignments(id: FleioId): Observable<{
    role_assignments: {
      [user_name: string]: Array<IRoleModel>,
    };
    api_users: Array<IApiUserModel>,
    api_user_names_to_id: Record<string, string>,
  }> {
    return this.objectGetAction(id, 'get_role_assignments');
  }

  updateRoleAssignments(id: FleioId, assignments) {
    return this.objectPostAction(id, 'update_role_assignments', {
      assignments
    });
  }
}
