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 { IDbaasInstanceModel } from '@fleio-api/openstack/model/dbaas-instance.model';
import { FleioId } from '@fleio-api/base-model/base-fleio-object.model';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class DbaasInstancesApiService extends FleioApiService<IDbaasInstanceModel> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('openstack/dbaas-instances'));
  }

  createUser(databaseInstanceId: FleioId, name: string, password: string) {
    return this.objectPostAction(databaseInstanceId, 'create_user', {
      name,
      password,
    });
  }

  deleteUser(databaseInstanceId: FleioId, username: string) {
    return this.objectPostAction(databaseInstanceId, 'delete_user', {
      username,
    });
  }

  getUsers(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'get_users');
  }

  createDatabase(databaseInstanceId: FleioId, name: string, character_set: string, collate: string) {
    return this.objectPostAction(databaseInstanceId, 'create_database', {
      name,
      character_set,
      collate,
    });
  }

  deleteDatabase(databaseInstanceId: FleioId, name: string) {
    return this.objectPostAction(databaseInstanceId, 'delete_database', {
      name,
    });
  }

  grantDatabasesAccess(databaseInstanceId: FleioId, username: string, databases: Array<string>) {
    return this.objectPostAction(databaseInstanceId, 'grant_databases_access', {
      username,
      databases,
    });
  }

  revokeDatabaseAccess(databaseInstanceId: FleioId, username: string, database: string) {
    return this.objectPostAction(databaseInstanceId, 'revoke_database_access', {
      username,
      database,
    });
  }

  resizeVolume(databaseInstanceId: FleioId, volume_size: number) {
    return this.objectPostAction(databaseInstanceId, 'resize_volume', {
      volume_size,
    });
  }

  getResizeOptions(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'resize_options', {});
  }

  resizeFlavor(databaseInstanceId: FleioId, flavorId: number) {
    return this.objectPostAction(databaseInstanceId, 'resize_flavor', {
      flavor_id: flavorId,
    });
  }

  rename(databaseInstanceId: FleioId, name: number) {
    return this.objectPostAction(databaseInstanceId, 'rename', {
      name,
    });
  }

  updateAccessibility(databaseInstanceId: FleioId, isPublic: boolean = null, allowedCidrs=null) {
    return this.objectPostAction(databaseInstanceId, 'update_accessibility', {
      is_public: isPublic,
      allowed_cidrs: allowedCidrs,
    });
  }

  getDatabases(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'get_databases');
  }

  createDatabaseReplica(databaseInstanceId: FleioId, name: string) {
    return this.objectPostAction(databaseInstanceId, 'create_replica', {
      name,
    });
  }

  createBackup(databaseInstanceId: FleioId, name: string, swiftContainer: string, incremental: number) {
    const params = {
      name,
    } as any;
    if (swiftContainer) {
      params.swift_container = swiftContainer;
    }
    if (incremental) {
      params.incremental = 1;
    }
    return this.objectPostAction(databaseInstanceId, 'create_backup', params);
  }

  createDatabaseInstanceFromBackup(databaseInstanceId: FleioId, name: string, backup: any) {
    return this.objectPostAction(databaseInstanceId, 'create_instance_from_backup', {
      name,
      backup_id: backup.id,
    });
  }

  getBackupSchedules(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'get_backup_schedules');
  }

  getBackups(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'get_backups');
  }

  deleteBackup(databaseInstanceId: FleioId, backup: string) {
    return this.objectPostAction(databaseInstanceId, 'delete_backup', {
      backup,
    });
  }

  getRootEnabledStatus(databaseInstanceId: FleioId): Observable<{
    is_instance_root_enabled: boolean;
  }> {
    return this.objectGetAction(databaseInstanceId, 'root_enabled_status');
  }

  getRootEnabledHistory(databaseInstanceId: FleioId) {
    return this.objectGetAction(databaseInstanceId, 'root_enabled_history');
  }

  enableRoot(databaseInstanceId: FleioId, rootPassword?: string): Observable<{
    root_password: string;
  }> {
    const params = {} as Record<string, string>;
    if (rootPassword) {
      params.root_password = rootPassword;
    }
    return this.objectPostAction(databaseInstanceId, 'enable_root', params);
  }

  disableRoot(databaseInstanceId: FleioId): Observable<any> {
    return this.objectPostAction(databaseInstanceId, 'disable_root', {});
  }

  getLogs(databaseInstanceId: FleioId): Observable<any> {
    return this.objectGetAction(databaseInstanceId, 'get_logs');
  }

  enableLogs(databaseInstanceId: FleioId, name: string): Observable<any> {
    return this.objectPostAction(databaseInstanceId, 'enable_logs', {
      name
    });
  }

  publishLogs(databaseInstanceId: FleioId, name: string): Observable<any> {
    return this.objectPostAction(databaseInstanceId, 'publish_logs', {
      name
    });
  }

  disableLogs(databaseInstanceId: FleioId, name: string): Observable<any> {
    return this.objectPostAction(databaseInstanceId, 'disable_logs', {
      name
    });
  }

  discardLogs(databaseInstanceId: FleioId, name: string): Observable<any> {
    return this.objectPostAction(databaseInstanceId, 'discard_logs', {
      name
    });
  }

  getPrice(data): Observable<any> {
    return this.getAction('get_price', data);
  }
}
