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 { IPermissionModel } from './model/permission.model';
import { Observable } from 'rxjs';
import { FleioId } from '../../base-model/base-fleio-object.model';
import { IGroupPermissionsModel } from '@fleio-api/core/permissions/model/group-permissions.model';
import { IUserPermissionsModel } from '@fleio-api/core/permissions/model/user-permissions.model';
import { IRolePermissionsModel } from '@fleio-api/core/permissions/model/role-permissions.model';

@Injectable({
  providedIn: 'root'
})
export class PermissionsSetsApiService extends FleioApiService<any> {
  constructor(protected httpClient: HttpClient, protected config: ConfigService) {
    super();
    this.setEndpoint(config.getPanelApiUrl('permissionsset'));
  }

  updatePermissionsForSet(
    objectId: FleioId,
    permissions: {
      implicitly_granted: boolean;
      objects: IPermissionModel[];
    }): Observable<any> {
    return this.objectPostAction(objectId, 'update_objects', permissions);
  }

  groupPermissions(groupId: FleioId): Observable<IGroupPermissionsModel> {
    return this.getAction('group_permissions', {id: groupId});
  }

  saveGroupPermissions(permissions: IGroupPermissionsModel): Observable<any> {
    return this.postAction('save_group_permissions', permissions);
  }

  userPermissions(userId: FleioId): Observable<IUserPermissionsModel> {
    return this.getAction('user_permissions', {id: userId});
  }

  saveUserPermissions(permissions: IUserPermissionsModel): Observable<any> {
    return this.postAction('save_user_permissions', permissions);
  }

  rolePermissions(roleId: FleioId): Observable<IRolePermissionsModel> {
    return this.getAction('role_permissions', {id: roleId});
  }

  saveRolePermissions(permissions: IRolePermissionsModel): Observable<any> {
    return this.postAction('save_role_permissions', permissions);
  }
}
