import { Injectable } from '@angular/core';
import { Effect, Actions } from '@ngrx/effects';
import { Action, Store } from '@ngrx/store';

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/switch';
import 'rxjs/add/observable/timer';

import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';

import * as ApplicationActions from './actions';
import { AuthService } from '../auth/auth.service';
import { MessageService as MessageServiceP } from '../common/messageservice';
import { AppConfig } from '../AppConfig/AppConfig';

@Injectable()
export class ApplicationEffects {

    TIMEOUT: boolean = false;
    interval: any;

    APPLICATION_TIMEOUT_TIME = 1000 * 60 * 30;

    @Effect()
    extendApplicationTimeout$ = this.actions$
        .switchMap((action: Action) => {
            // console.log("action");
            // console.log(action);
            // var d = new Date();
            // console.log(d);
            if (this.TIMEOUT == true && action.type != ApplicationActions.LOG_OUT) {
                // console.log(action.type);
                this.TIMEOUT = false;
            }
            return Observable.timer(this.APPLICATION_TIMEOUT_TIME)
        })
        .map(() => {
            // console.log("map: logout");
            this.messageService.add({
                severity: 'error', summary: 'Logout Warning',
                detail: 'No action detected in a period of time. System will auto logout if no action detect within 30 secound!'
            });
            this.TIMEOUT = true;
            this.countDown();
            return new ApplicationActions.LogOut()
        });

    constructor(private actions$: Actions, private authService: AuthService,
        public messageService: MessageServiceP, private appconfig: AppConfig) {

        appconfig.onupdate.subscribe(result => {
            try {
                // console.log("AppConfig");
                // console.log(result);
                if (result) {
                    let s = AppConfig.systemSettings;
                    let i = s.filter(a => a.name == "InterfaceSeasonTimeOut");
                    // console.log(s);
                    // console.log(i);
                    this.APPLICATION_TIMEOUT_TIME = 1000 * 60 * parseInt(i[0].value, 10);
                    // console.log(this.APPLICATION_TIMEOUT_TIME);
                }
            } catch (error) {
                console.log(error);
            }
        });


    }

    countDown() {

        this.interval = setInterval(() => {
            // var d = new Date();
            // console.log(d);
            if (this.TIMEOUT) {
                this.authService.logOut();
            }
            else {
                this.messageService.add({
                    severity: 'info', summary: 'Logout Cancel',
                    detail: 'New action detected. Auto logout temporarily canceled.'
                });
            }
            clearInterval(this.interval);
        }, 30000);

    }

}
