import { Action, Selector, State, StateContext } from '@ngxs/store';
import { WebApiService } from '~/core/services/web-api.service';
import { ApplicationSettings as AppSettings } from '@nativescript/core';

import { USER_TOKEN } from '~/constants/login-constants';
import { LoadEventOtherDetail, LoadEventOtherDetailPhotos } from './edu-event-other-detail.actions';
import { APP_ID, wsEvents } from '~/constants/api.constants';
import { catchError, tap } from 'rxjs/operators';
import { ImageSource } from '@nativescript/core';
import { EduEventOtherDetailOutput, EduEventIllustrativePhotoOutput, EventEducationOtherDetail, EventEducationOtherIllustrativePhotoAll }
    from '~/pages/event/entities/edu-event-other-detail.types';

export interface EduEventOtherDetailStateModel {
    eventDetail: EduEventOtherDetailOutput;
    photos: EduEventIllustrativePhotoOutput[];
    photosContent: ImageSource[];
}

const initialStateModel: EduEventOtherDetailStateModel = {
    eventDetail: null,
    photos: [],
    photosContent: [],
};

@State<EduEventOtherDetailStateModel>({
    name: 'eduEventOtherDetail',
    defaults: initialStateModel
})
export class EduEventOtherDetailState {

    constructor(
        private webApiService: WebApiService
    ) {
    }
    @Selector()
    static getEventOtherDetail(state: EduEventOtherDetailStateModel) {
        return state.eventDetail
    }
    @Selector()
    static getEventOtherDetailPhotos(state: EduEventOtherDetailStateModel) {
        return state.photos
    }


    @Action(LoadEventOtherDetail)
    loadEventDetail(ctx: StateContext<EduEventOtherDetailStateModel>,
        action: LoadEventOtherDetail) {

        if (action.eventEducationId) {
            return this.webApiService.call(
                new EventEducationOtherDetail(AppSettings.getString(USER_TOKEN),
                    action.eventEducationId, action.eventEducationCourseId),
                () => new EduEventOtherDetailOutput(),
                wsEvents
            ).pipe(
                tap((response: EduEventOtherDetailOutput[]) => {
                    if (response.length === 1) {
                        ctx.patchState({
                            eventDetail: response[0]
                        });
                    } else {
                        ctx.patchState({
                            eventDetail: initialStateModel.eventDetail
                        });
                        throw new Error('Nepodařilo se nalézt detail akce');
                    }
                }),
                catchError(e => {
                    ctx.patchState({
                        eventDetail: initialStateModel.eventDetail
                    });

                    throw e;
                })
            );
        }
    }


    @Action(LoadEventOtherDetailPhotos)
    loadEventOtherDetailPhotos(ctx: StateContext<EduEventOtherDetailStateModel>,
        action: LoadEventOtherDetailPhotos) {
        if (action.eventEducationId) {
            return this.webApiService.call(
                new EventEducationOtherIllustrativePhotoAll(APP_ID, AppSettings.getString(USER_TOKEN),
                    action.eventEducationId, action.eventEducationCourseId),
                () => new EduEventIllustrativePhotoOutput(),
                wsEvents
            ).pipe(
                tap((response: EduEventIllustrativePhotoOutput[]) => {
                    ctx.patchState({
                        photos: response
                    });

                }),
                catchError(e => {
                    ctx.patchState({
                        photos: initialStateModel.photos
                    });

                    throw e;
                })
            );
        }
    }
}