import { ofType } from 'redux-observable';
import { concat, of } from 'rxjs';
import { mergeMap, switchMap } from 'rxjs/operators';

import { QUERY_PARAM_PLAYLIST_EDIT } from '@/modules/editorial/constants/routing';
import { PLAYLIST_TYPE } from '@/modules/editorial/RightSideBar/TabPlaylist/Edit/constants';

import {
  clipsSelector,
  frequencySelector,
  hubSelector,
  includeSponsoredContentSelector,
  isPrefixSelector,
  nameSelector,
  playerSelector,
  playlistIdSelector,
  shuffleSelector,
  statusSelector,
  supplyTagSelector,
  urlSelector,
} from '../selectors';
import { clearCreateFormAction, updatePlaylistAction } from '../slices';
import { removeQueriesAction } from '@/modules/@common/location/redux/slices';
import { gqlRequest } from '@/modules/@common/request';
import { getPlaylistByIdAction } from '@/modules/editorial/RightSideBar/TabPlaylist/redux/slices';

const queryGQL = `
  mutation updatePlaylist(
  $playlistId: Int!,
  $publisherId: Int!,
  $name: String,
  $url: String,
  $clips: [String],
  $playerId: Int,
  $isPrefix: Boolean,
  $status: Boolean,
  $shuffle: Boolean,
  $includeSponsoredContent: Boolean,
  $frequency: Int,
  $supplyTagId: String,
  $supplyTagName: String,
  ) {
    updatePlaylist(
      playlistId: $playlistId,
      publisherId: $publisherId,
      name: $name,
      url: $url,
      clips: $clips,
      playerId: $playerId,
      isPrefix: $isPrefix,
      status: $status,
      shuffle: $shuffle,
      includeSponsoredContent: $includeSponsoredContent,
      frequency: $frequency,
      supplyTagId: $supplyTagId,
      supplyTagName: $supplyTagName,
    ){
      playlistId
    }
  }
`;

export default (action$, state$) =>
  action$.pipe(
    ofType(updatePlaylistAction.type),
    switchMap(() => {
      const playlistId = playlistIdSelector(state$.value);
      const hub = hubSelector(state$.value);
      const name = nameSelector(state$.value);
      const status = statusSelector(state$.value);
      const url = urlSelector(state$.value);
      const playerId = playerSelector(state$.value).id;
      const isPrefix = isPrefixSelector(state$.value);
      const shuffle = shuffleSelector(state$.value);
      const clips = clipsSelector(state$.value);

      const includeSponsoredContent = includeSponsoredContentSelector(state$.value);
      const frequency = parseInt(frequencySelector(state$.value), 10);
      const supplyTagId = supplyTagSelector(state$.value)?.id;
      const supplyTagName = supplyTagSelector(state$.value)?.name;

      const params = {
        playlistId,
        publisherId: hub.value,
        name,
        url,
        status: !!status,
        playerId,
        isPrefix,
        shuffle,
        clips,
        type: PLAYLIST_TYPE,

        includeSponsoredContent,
        frequency,
        supplyTagId,
        supplyTagName,
      };

      const stream$ = gqlRequest({
        query: queryGQL,
        variables: params,
      }).pipe(
        mergeMap(({ data, errors }) => {
          const actions = [];

          if (!errors.length) {
            actions.push(
              of(clearCreateFormAction()),
              of(removeQueriesAction([QUERY_PARAM_PLAYLIST_EDIT])),
              of(getPlaylistByIdAction({ id: data.updatePlaylist.playlistId })),
            );
          }

          return concat(...actions);
        }),
      );

      return concat(stream$);
    }),
  );
