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 { IClientModel } from '../model/client.model';
import { Observable } from 'rxjs';
import { FleioId } from '../../base-model/base-fleio-object.model';
import { IUserModel } from '@fleio-api/client-user/model/user.model';
import { IRoleModel } from '@fleio-api/client-user/roles/model/role.model';
import {
  IUserNotificationSettingsResponseModel
} from '@fleio-api/notifications/model/user-notification-settings-response.model';

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

  updateUsage(clientId: FleioId): Observable<any> {
    return this.objectPostAction(clientId, 'update_usage', {});
  }

  resetUsage(clientId: FleioId, params: {}): Observable<any> {
    return this.objectPostAction(clientId, 'reset_usage', params);
  }

  changeCredit(
    clientId: FleioId,
    addCredit: boolean,
    amount: number,
    sourceCurrency: string,
    currency: string
  ): Observable<any> {
    return this.objectPostAction(clientId, 'change_credit', {
      add_credit: addCredit,
      amount,
      source_currency: sourceCurrency,
      currency
    });
  }

  referralData(clientId: FleioId) {
    return this.objectPostAction(clientId, 'get_referral_data', {});
  }

  getCloudSummary(clientId: FleioId): Observable<any> {
    return this.objectGetAction(clientId, 'get_cloud_summary', {});
  }

  getUsers(clientId: FleioId): Observable<{ users: IUserModel[] }> {
    return this.objectGetAction(clientId, 'get_users', {});
  }

  addToGroup(
    objectId: FleioId,
    groupId: FleioId
  ): Observable<any> {
    return this.objectPostAction(objectId, `add_to_group`, {group_id: groupId});
  }

  removeFromGroup(
    objectId: FleioId,
    groupId: FleioId
  ): Observable<any> {
    return this.objectPostAction(objectId, `remove_from_group`, {group_id: groupId});
  }

  getClientsInGroup(groupId: FleioId, page: number): Observable<any> {
    return this.getAction('get_clients_in_group', {
      group_id: groupId,
      page
    });
  }

  getAvailableClientsForGroup(groupId: FleioId, search: string): Observable<any> {
    return this.list({
      search,
      filtering: `groups__ne:${groupId}`
    });
  }

  canDissociateAsOwner(clientId: FleioId): Observable<any> {
    return this.objectGetAction(clientId, 'can_dissociate_as_owner');
  }

  dissociateFromClient(clientId: FleioId): Observable<any> {
    return this.objectPostAction(clientId, 'dissociate_from_client', {});
  };

  changeConfiguration(clientId: FleioId, configuration: any): Observable<any> {
    return this.objectPostAction(clientId, 'change_configuration', {
      id: clientId,
      configuration,
    });
  }

  associateUser(clientId: FleioId, userId: FleioId, roles: number[]): Observable<any> {
    return this.objectPostAction(clientId, 'associate_user', {id: clientId, user_id: userId, roles});
  }

  inviteUser(clientId: FleioId, userEmail: string, roles: number[], selectedNotificationsSettings: any[]): Observable<any> {
    return this.objectPostAction(clientId, 'send_invitation', {
      id: clientId,
      user_email: userEmail,
      roles,
      notifications_settings: selectedNotificationsSettings,
    });
  }

  getConfiguration(clientId: FleioId): Observable<any> {
    return this.objectGetAction(clientId, 'get_configuration');
  }

  getConfigurationForClient(clientId: FleioId): Observable<any> {
    return this.objectGetAction(clientId, 'get_configurations_for_client');
  }

  getSummary(): Observable<any> {
    return this.getAction('summary');
  }

  getUserNotificationsSettings(id: FleioId, userId: FleioId, preview = false): Observable<IUserNotificationSettingsResponseModel> {
    return this.objectGetAction(id, 'get_user_notifications_settings', {
      user_id: userId,
      preview
    });
  }

  setUserNotificationsSettings(clientId: FleioId, userId: FleioId, settings: any): Observable<any> {
    return this.objectPostAction(clientId, 'set_user_notifications_settings', {
      id: clientId,
      user_id: userId,
      settings
    });
  }

  getAvailableRoles(id: FleioId): Observable<{ roles: IRoleModel[]; default_role_id: number }> {
    return this.objectGetAction(id, 'get_available_roles');
  }

  updateRoles(clientId: FleioId, userId: FleioId, roles: number[]): Observable<any> {
    return this.objectPostAction(clientId, 'update_roles', {id: clientId, user_id: userId, roles});
  }

  deleteCredit(clientId: FleioId, creditId: FleioId): Observable<any> {
    return this.objectPostAction(clientId, 'delete_credit', {id: clientId, credit_id: creditId});
  }
}
