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, USER_UNIT_ID } from '~/constants/login-constants';
import { WebApiService } from '~/core/services/web-api.service';
import { WarehouseAll } from '../entities/input/warehouseAll';
import { WarehouseItemAllLent } from '../entities/input/warehouseItemAllLent';
import { WarehouseAllOutput } from '../entities/output/warehouseAllOutput';
import { WarehouseItemAllLentOutput } from '../entities/output/warehouseItemAllLentOutput';
import { LoadWarehouseAll, LoadWarehouseItemAllLent } from './reservation-lent.action';

export interface ReservationLentStateModel {
    loadingWarehouseItemAllLent: boolean;
    loadingWarehouseAll: boolean;
    warehouseItemAllLent: WarehouseItemAllLentOutput[];
    warehouses: WarehouseAllOutput[];
}

const initialStateModel: ReservationLentStateModel = {
    loadingWarehouseItemAllLent: false,
    loadingWarehouseAll: false,
    warehouseItemAllLent: [],
    warehouses: []
};

@State<ReservationLentStateModel>({
    name: 'reservationLent',
    defaults: initialStateModel
})
export class ReservationLentState {
    @Selector()
    static getReservationLent(state: ReservationLentStateModel) {
        return state.warehouseItemAllLent;
    }

    @Selector()
    static getWarehouseAll(state: ReservationLentStateModel) {
        return state.warehouses;
    }

    @Selector()
    static isLoading(state: ReservationLentStateModel) {
        return state.loadingWarehouseItemAllLent || state.loadingWarehouseAll;
    }

    constructor(
        private webApiService: WebApiService
    ) { }

    /**
     * Load warehouses
     *
     * @param {StateContext<ReservationLentStateModel>} ctx
     * @param {LoadWarehouseAll} action
     * @returns {Observable<any>}
     */
    @Action(LoadWarehouseAll)
    loadWarehouseAll(ctx: StateContext<ReservationLentStateModel>, action: LoadWarehouseAll) {
        ctx.patchState({
            loadingWarehouseAll: true
        });

        return this.webApiService.call(
            new WarehouseAll(APP_ID, ApplicationSettings.getString(USER_TOKEN), ApplicationSettings.getNumber(USER_UNIT_ID)),
            () => new WarehouseAllOutput(),
            wsMaterial
        ).pipe(
            tap((response: WarehouseAllOutput[]) => {
                ctx.patchState({
                    loadingWarehouseAll: false,
                    warehouses: response
                });
            }),
            catchError(e => {
                ctx.patchState({
                    loadingWarehouseAll: false,
                    warehouses: initialStateModel.warehouses
                });

                throw e;
            })
        );
    }

    /**
     * Load all lent items
     *
     * @param {StateContext<ReservationLentStateModel>} ctx
     * @param {LoadWarehouseItemAllLent} action
     * @returns {Observable<any>}
     */
    @Action(LoadWarehouseItemAllLent)
    loadWarehouseItemAllLent(ctx: StateContext<ReservationLentStateModel>, action: LoadWarehouseItemAllLent) {
        ctx.patchState({
            loadingWarehouseItemAllLent: true
        });

        return this.webApiService.call(
            new WarehouseItemAllLent(APP_ID, ApplicationSettings.getString(USER_TOKEN), ApplicationSettings.getNumber(USER_UNIT_ID)),
            () => new WarehouseItemAllLentOutput(),
            wsMaterial
        ).pipe(
            tap((response: WarehouseItemAllLentOutput[]) => {
                ctx.patchState({
                    loadingWarehouseItemAllLent: false,
                    warehouseItemAllLent: response
                });
            }),
            catchError(e => {
                ctx.patchState({
                    loadingWarehouseItemAllLent: false,
                    warehouseItemAllLent: initialStateModel.warehouseItemAllLent
                });

                throw e;
            })
        );
    }

}
