import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { ApplicationSettings } from '@nativescript/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { APP_ID, SERVER_URL } from '~/constants/api.constants';
import {
    USER_ID,
    USER_NAME,
    USER_PERSON_ID,
    USER_PERSON_NAME,
    USER_ROLE_ID,
    USER_TOKEN,
    USER_UNIT_ID
} from '~/constants/login-constants';
import { LoginData } from '~/core/entities/login-data';

/**
 * Service providing data of log in user or null if is not log in. And allows update that data.
 * It is used for log out user or store user data on one place out of AppSettings
 */
@Injectable()
export class LoginDataService {
    dataSource = new BehaviorSubject<LoginData | null>(null);
    loginData: Observable<LoginData | null> = this.dataSource.asObservable();

    constructor(
        private httpClient: HttpClient
    ) {
    }

    /**
     * It send to dataSource userData(LoginData) from AppSettings or null if user is not log in.
     */
    updateUserData() {
        if (ApplicationSettings.getString(USER_TOKEN)) {
            const userData: LoginData = new LoginData();
            userData.id = ApplicationSettings.getNumber(USER_ID);
            userData.userId = ApplicationSettings.getNumber(USER_PERSON_ID);
            userData.userName = ApplicationSettings.getString(USER_PERSON_NAME);
            userData.userToken = ApplicationSettings.getString(USER_TOKEN);
            userData.userRoleId = ApplicationSettings.getNumber(USER_ROLE_ID);
            userData.userUnitId = ApplicationSettings.getNumber(USER_UNIT_ID);
            this.dataSource.next(userData);
        } else {
            this.dataSource.next(null);
        }
    }

    /**
     * It Call http request on logout. After that it remove properties from AppSettings and
     * update userData providing from this service
     */
    logout() {
        return this.httpClient.get(
            `${SERVER_URL}/Login/LogOut.aspx?AppID=${APP_ID}&Token=${ApplicationSettings.getString(USER_TOKEN)}`
        ).toPromise().then(
            res => {
                ApplicationSettings.remove(USER_NAME);
                ApplicationSettings.remove(USER_TOKEN);
                ApplicationSettings.remove(USER_ROLE_ID);
                ApplicationSettings.remove(USER_ID);
                ApplicationSettings.remove(USER_PERSON_ID);
                ApplicationSettings.remove(USER_PERSON_NAME);
                ApplicationSettings.remove(USER_UNIT_ID);
                this.updateUserData();
            },
            err => {
                ApplicationSettings.remove(USER_NAME);
                ApplicationSettings.remove(USER_TOKEN);
                ApplicationSettings.remove(USER_ROLE_ID);
                ApplicationSettings.remove(USER_ID);
                ApplicationSettings.remove(USER_PERSON_ID);
                ApplicationSettings.remove(USER_PERSON_NAME);
                ApplicationSettings.remove(USER_UNIT_ID);
                this.updateUserData();
            }
        ).catch(
            () => {
                console.log('complete');
            });
    }
}
