import { Injectable } from '@angular/core';
import { ConfigService } from '../../config/config.service';
import { FleioApiService } from '../fleio-api.service';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { FleioId, IBaseFleioObjectModel } from '../base-model/base-fleio-object.model';
import { ISignupInfoModel } from './model/signup-info.model';
import { ISignupSettingsModel } from './model/signup-settings.model';
import { ISfaSettingsModel } from './model/sfa-settings.model';
import { map } from 'rxjs/operators';
import { ITOSSettingsModel } from './model/tos-settings.model';
import { ITermsOfServiceModel } from './model/terms-of-service.model';
import { FleioObjectsList } from '../fleio-objects-list';
import { IAdvancedSettingsModel } from '@fleio-api/core/model/advanced-settings.model';
import { IFormattingSettingsModel } from '@fleio-api/core/model/formatting-settings.model';
import { ISingleSignOnSettingsModel } from '@fleio-api/core/model/single-sign-on-settings.model';
import { ISingleSignOnInfoModel } from '@fleio-api/core/model/single-sign-on-info.model';
import { IReferralSettingsModel } from '@fleio-api/core/model/referral-settings.model';

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

  getSignupSettings(): Observable<ISignupInfoModel> {
    return this.getAction('sign-up-settings');
  }

  saveSignupSettings(signupSettings: ISignupSettingsModel): Observable<any> {
    return this.postAction('sign-up-settings', signupSettings);
  }

  getSingleSignOnSettings(): Observable<ISingleSignOnInfoModel> {
    return this.getAction('single-sign-on-settings');
  }

  saveSingleSignOnSettings(singleSignOnSettings: ISingleSignOnSettingsModel): Observable<any> {
    return this.postAction('single-sign-on-settings', singleSignOnSettings);
  }

  getSfaSettings(): Observable<ISfaSettingsModel> {
    return this.getAction('sfa-settings').pipe(map(result => result.sfa_settings));
  }

  saveSfaSettings(sfaSettings: ISfaSettingsModel): Observable<any> {
    return this.postAction('sfa-settings', sfaSettings);
  }

  getTOSSettings(): Observable<ITOSSettingsModel> {
    return this.getAction('tos-settings').pipe(map(result => result.tos_settings));
  }

  saveTOSSettings(tosSettings: ITOSSettingsModel): Observable<any> {
    return this.postAction('tos-settings', tosSettings);
  }

  getFormattingSettings(): Observable<IFormattingSettingsModel> {
    return this.getAction('formatting-settings').pipe(map(result => result.formatting_settings));
  }

  saveFormattingSettings(advancedSettings: IFormattingSettingsModel): Observable<any> {
    return this.postAction('formatting-settings', advancedSettings);
  }

  getReferralSettings(): Observable<IReferralSettingsModel> {
    return this.getAction('referral-settings').pipe(map(result => result.referral_settings));
  }

  saveReferralSettings(referralSettings: IReferralSettingsModel): Observable<any> {
    return this.postAction('referral-settings', referralSettings);
  }

  getAdvancedSettings(): Observable<IAdvancedSettingsModel> {
    return this.getAction('advanced-settings').pipe(map(result => result.advanced_settings));
  }

  saveAdvancedSettings(advancedSettings: IAdvancedSettingsModel): Observable<any> {
    return this.postAction('advanced-settings', advancedSettings);
  }

  createTermsOfService(termsOfService: ITermsOfServiceModel): Observable<any> {
    return this.postAction('tos', termsOfService);
  }

  deleteTermsOfService(id: FleioId): Observable<any> {
    return this.delete(`tos/${id}`);
  }

  saveTermsOfService(termsOfService: ITermsOfServiceModel): Observable<any> {
    return this.putAction(`tos/${termsOfService.id}`, termsOfService);
  }

  getTermsOfService(): Observable<FleioObjectsList<ITermsOfServiceModel>> {
    return this.getAction('tos').pipe(map(objectList => objectList as FleioObjectsList<ITermsOfServiceModel>));
  }
  getSingleTermsOfService(id: FleioId): Observable<ITermsOfServiceModel> {
    return this.getAction(`tos/${id}`);
  }
}
