import { ApplicationSettings } from '@nativescript/core';
import { Action, Selector, State, StateContext } from '@ngxs/store';
import { catchError, tap } from 'rxjs/operators';
import { APP_ID, wsMaterial } from '~/constants/api.constants';
import { USER_TOKEN } from '~/constants/login-constants';
import { WebApiService } from '~/core/services/web-api.service';
import { WarehouseItemReservationAll } from '../entities/input/warehouseItemReservationAll';
import { WarehouseItemReservationAllOutput } from '../entities/output/warehouseItemReservationAllOutput';
import { LoadWarehouseItemReservationAll } from './reservation-lent-detail.action';

export interface ReservationLentDetailStateModel {
    loadingWarehouseItemReservationAll: boolean;
    warehouseItemReservationAll: WarehouseItemReservationAllOutput;
}

const initialStateModel: ReservationLentDetailStateModel = {
    loadingWarehouseItemReservationAll: false,
    warehouseItemReservationAll: null
};

@State<ReservationLentDetailStateModel>({
    name: 'reservationLentDetail',
    defaults: initialStateModel
})
export class ReservationLentDetailState {
    @Selector()
    static getwarehouseItemReservationAll(state: ReservationLentDetailStateModel) {
        return state.warehouseItemReservationAll;
    }

    @Selector()
    static isLoading(state: ReservationLentDetailStateModel) {
        return state.loadingWarehouseItemReservationAll;
    }

    constructor(
        private webApiService: WebApiService
    ) {
    }

    /**
     * Load reservation (just one from all of them, it is a kind of reservation detail)
     *
     * @param {StateContext<ReservationLentDetailStateModel>} ctx
     * @param {LoadWarehouseItemReservationAll} action
     * @returns {Observable<any>}
     */
    @Action(LoadWarehouseItemReservationAll)
    loadWarehouseItemReservationAll(ctx: StateContext<ReservationLentDetailStateModel>, action: LoadWarehouseItemReservationAll) {
        ctx.patchState({
            loadingWarehouseItemReservationAll: true
        });

        if (action.idItem && action.idReservation) {
            return this.webApiService.call(
                new WarehouseItemReservationAll(APP_ID, ApplicationSettings.getString(USER_TOKEN), action.idItem, true, action.idReservation),
                () => new WarehouseItemReservationAllOutput(),
                wsMaterial
            ).pipe(
                tap((response: WarehouseItemReservationAllOutput[]) => {
                    if (response.length === 1) {
                        ctx.patchState({
                            loadingWarehouseItemReservationAll: false,
                            warehouseItemReservationAll: response[0]
                        });
                    } else {
                        ctx.patchState({
                            loadingWarehouseItemReservationAll: false,
                            warehouseItemReservationAll: initialStateModel.warehouseItemReservationAll
                        });
                        throw new Error('Nepodařilo se nalézt detail rezervace');
                    }
                }),
                catchError(e => {
                    ctx.patchState({
                        loadingWarehouseItemReservationAll: false,
                        warehouseItemReservationAll: initialStateModel.warehouseItemReservationAll
                    });

                    throw e;
                })
            );
        } else {
            ctx.patchState({
                loadingWarehouseItemReservationAll: false
            });
        }
    }
}
