import { Injectable } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import * as actions from './task-files.actions';
import { flatMap, catchError, map } from 'rxjs/operators';
import { EISPublicService, StrictHttpResponse, ResponseArgsOfIEnumerableOfFileDTO } from '@swagger/eis';
import { of, NEVER } from 'rxjs';
import { FileDTO } from './defs';

@Injectable()
export class TaskFilesEffects {
  constructor(private actions$: Actions, private eisPublicService: EISPublicService) {}

  getFiles$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getFiles),
      flatMap((action) => [
        actions.getFilesRequest({ taskId: action.taskId }),
        actions.getImageFilesRequest({ taskId: action.taskId }),
        actions.getImageTeaserFilesRequest({ taskId: action.taskId }),
        actions.getConfiramtionImageFileRequest({ taskId: action.taskId }),
      ])
    )
  );

  getFilesRequest$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getFilesRequest),
      flatMap((action) =>
        this.eisPublicService
          .EISPublicGetFilesResponse({
            infoId: action.taskId,
          })
          .pipe(
            catchError((err) => of<StrictHttpResponse<ResponseArgsOfIEnumerableOfFileDTO>>(err)),
            map((response) => actions.getFilesResponse({ taskId: action.taskId, response }))
          )
      )
    )
  );

  getFilesResponse$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getFilesResponse),
      flatMap((action) => {
        if (action.response.ok) {
          return [
            actions.clear({ taskId: action.taskId, section: 'file' }),
            actions.addFiles({
              files: action.response.body.result.map<FileDTO>((file) => ({
                ...file,
                section: 'file',
                taskId: action.taskId,
              })),
            }),
          ];
        }
        return NEVER;
      })
    )
  );

  getImageFilesRequest$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getImageFilesRequest),
      flatMap((action) =>
        this.eisPublicService
          .EISPublicGetImagesResponse({
            infoId: action.taskId,
          })
          .pipe(
            catchError((err) => of<StrictHttpResponse<ResponseArgsOfIEnumerableOfFileDTO>>(err)),
            map((response) => actions.getImageFilesResponse({ taskId: action.taskId, response }))
          )
      )
    )
  );

  getImageFilesResponse$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getImageFilesResponse),
      flatMap((action) => {
        if (action.response.ok) {
          return [
            actions.clear({ taskId: action.taskId, section: 'image' }),
            actions.addFiles({
              files: action.response.body.result.map<FileDTO>((file) => ({
                ...file,
                section: 'image',
                taskId: action.taskId,
              })),
            }),
          ];
        }
        return NEVER;
      })
    )
  );

  getImageTeaserFilesRequest$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getImageTeaserFilesRequest),
      flatMap((action) =>
        this.eisPublicService
          .EISPublicGetImagesResponse({
            infoId: action.taskId,
          })
          .pipe(
            catchError((err) => of<StrictHttpResponse<ResponseArgsOfIEnumerableOfFileDTO>>(err)),
            map((response) => actions.getImageTeaserFilesResponse({ taskId: action.taskId, response }))
          )
      )
    )
  );

  getImageTeaserFilesResponse$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getImageTeaserFilesResponse),
      flatMap((action) => {
        if (action.response.ok) {
          return [
            actions.clear({ taskId: action.taskId, section: 'teaser' }),
            actions.addFiles({
              files: action.response.body.result.map<FileDTO>((file) => ({
                ...file,
                section: 'teaser',
                taskId: action.taskId,
              })),
            }),
          ];
        }
        return NEVER;
      })
    )
  );

  getConfiramtionImageFileRequest$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getConfiramtionImageFileRequest),
      flatMap((action) =>
        this.eisPublicService
          .EISPublicGetImagesResponse({
            infoId: action.taskId,
          })
          .pipe(
            catchError((err) => of<StrictHttpResponse<ResponseArgsOfIEnumerableOfFileDTO>>(err)),
            map((response) => actions.getConfiramtionImageFileResponse({ taskId: action.taskId, response }))
          )
      )
    )
  );

  getConfiramtionImageFileResponse$ = createEffect(() =>
    this.actions$.pipe(
      ofType(actions.getConfiramtionImageFileResponse),
      flatMap((action) => {
        if (action.response.ok) {
          return [
            actions.clear({ taskId: action.taskId, section: 'confirmation' }),
            actions.addFiles({
              files: action.response.body.result.map<FileDTO>((file) => ({
                ...file,
                section: 'confirmation',
                taskId: action.taskId,
              })),
            }),
          ];
        }
        return NEVER;
      })
    )
  );
}
