import { Injectable, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

import { _MODULE_CONFIG } from '../types/token';
import { ModuleConfig } from '../types/module-config';
import {
  FileProfileSaveRequest,
  FileProfileSaveResponse,
  encodeFileProfileSave,
  decodeFileProfileSave
} from '../models/file-profile-save';
import {
  FileTalkDownloadRequest,
  encodeFileTalkDownload
} from '../models/file-talk-download';
import {
  FileTalkSaveRequest,
  FileTalkSaveResponse,
  encodeFileTalkSave,
  decodeFileTalkSave
} from '../models/file-talk-save';
import {
  FileTalkShareRequest,
  FileTalkShareResponse,
  encodeFileTalkShare,
  decodeFileTalkShare
} from '../models/file-talk-share';
import {
  MassTalkDownloadRequest,
  MassTalkDownloadResponse,
  encodeMassTalkDownload,
  decodeMassTalkDownload
} from '../models/mass-talk-download';
import {
  MassTalkSaveRequest,
  MassTalkSaveResponse,
  encodeMassTalkSave,
  decodeMassTalkSave
} from '../models/mass-talk-save';
import {
  TransMassTalkDownloadRequest,
  TransMassTalkDownloadResponse,
  encodeTransMassTalkDownload,
  decodeTransMassTalkDownload
} from '../models/trans-mass-talk-download';
import {
  TransMassTalkSaveRequest,
  TransMassTalkSaveResponse,
  encodeTransMassTalkSave,
  decodeTransMassTalkSave
} from '../models/trans-mass-talk-save';
import {
  TranslationReqRequest,
  TranslationReqResponse,
  encodeTranslationReq,
  decodeTranslationReq
} from '../models/translation-req';
import {
  TranslationSaveRequest,
  TranslationSaveResponse,
  encodeTranslationSave,
  decodeTranslationSave
} from '../models/translation-save';

@Injectable({
  providedIn: 'root'
})
export class CommonApiService {
  constructor(
    @Inject(_MODULE_CONFIG) private moduleConfig: ModuleConfig,
    private httpClient: HttpClient
  ) {}

  public fileProfileSave(
    req: FileProfileSaveRequest
  ): Observable<FileProfileSaveResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.fileProfileSave,
        {},
        {
          params: encodeFileProfileSave(req)
        }
      )
      .pipe(map(res => decodeFileProfileSave(res)));
  }

  public fileTalkDownload(req: FileTalkDownloadRequest): Observable<Blob> {
    return this.httpClient.post<Blob>(
      this.moduleConfig.urls.fileTalkDownload,
      { responseType: 'blob' },
      {
        params: encodeFileTalkDownload(req)
      }
    );
  }

  public fileTalkSave(
    req: FileTalkSaveRequest
  ): Observable<FileTalkSaveResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.fileTalkSave,
        {},
        {
          params: encodeFileTalkSave(req)
        }
      )
      .pipe(map(res => decodeFileTalkSave(res)));
  }

  public fileTalkShare(
    req: FileTalkShareRequest
  ): Observable<FileTalkShareResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.fileTalkShare,
        {},
        {
          params: encodeFileTalkShare(req)
        }
      )
      .pipe(map(res => decodeFileTalkShare(res)));
  }

  public massTalkDownload(
    req: MassTalkDownloadRequest
  ): Observable<MassTalkDownloadResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.massTalkDownload,
        {},
        {
          params: encodeMassTalkDownload(req)
        }
      )
      .pipe(map(res => decodeMassTalkDownload(res)));
  }

  public massTalkSave(
    req: MassTalkSaveRequest
  ): Observable<MassTalkSaveResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.massTalkSave,
        {},
        {
          params: encodeMassTalkSave(req)
        }
      )
      .pipe(map(res => decodeMassTalkSave(res)));
  }

  public transMassTalkDownload(
    req: TransMassTalkDownloadRequest
  ): Observable<TransMassTalkDownloadResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.transMassTalkDownload,
        {},
        {
          params: encodeTransMassTalkDownload(req)
        }
      )
      .pipe(map(res => decodeTransMassTalkDownload(res)));
  }

  public transMassTalkSave(
    req: TransMassTalkSaveRequest
  ): Observable<TransMassTalkSaveResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.transMassTalkSave,
        {},
        {
          params: encodeTransMassTalkSave(req)
        }
      )
      .pipe(map(res => decodeTransMassTalkSave(res)));
  }

  public translationReq(
    req: TranslationReqRequest
  ): Observable<TranslationReqResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.translationReq,
        {},
        {
          params: encodeTranslationReq(req)
        }
      )
      .pipe(map(res => decodeTranslationReq(res)));
  }

  public translationSave(
    req: TranslationSaveRequest
  ): Observable<TranslationSaveResponse> {
    return this.httpClient
      .post<any>(
        this.moduleConfig.urls.translationSave,
        {},
        {
          params: encodeTranslationSave(req)
        }
      )
      .pipe(map(res => decodeTranslationSave(res)));
  }
}
