import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, CanActivate } from '@angular/router';
import { Http } from '../service/Http';
import { App } from '../service/App';
import { AppEnvironment } from '../../environments/AppEnvironment';
import { CodeList } from '../service/codelist';

@Injectable()
export class AuthGuard implements CanActivate {

    /**
     *
     *
     * @private
     * @type {(1 | 2 | 3 | 4)} (匿名用户 | 登录用户 | 匿名及登录用户 | 登录用户且拥有指定资源能力 )
     * @memberof AuthGuard
     */
    private accessMode: 1 | 2 | 3 | 4 = 2;

    /**
     * Creates an instance of AuthGuard.
     * @memberof AuthGuard
     */
    constructor(private $http: Http, private App: App, private codeList: CodeList) { }

    /**
     *
     *
     * @param {ActivatedRouteSnapshot} route
     * @param {RouterStateSnapshot} state
     * @returns {(Promise<boolean> | Observable<boolean> | boolean)}
     * @memberof AuthGuard
     */
    public async canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
        let isNext: boolean = true;
        isNext = this.permissionVerification(route);
        if (!isNext) {
            return false;
        }
        isNext = await this.loadAppData(route);
        if (!isNext) {
            return false;
        }
        isNext = await this.codeList.initCodelist();
        return isNext;
    }

    /**
     * 权限校验
     *
     * @private
     * @param {ActivatedRouteSnapshot} route
     * @returns {Promise<boolean>}
     * @memberof AuthGuard
     */
    private permissionVerification(route: ActivatedRouteSnapshot): boolean {
        const data: any = route.data;
        if (data && data.hasOwnProperty('accessMode')) {
            this.accessMode = data.accessMode;
        }
        if (Object.is(AppEnvironment.LoginModel, 'default') && (this.accessMode === 2 || this.accessMode === 4)) {
            const srfLoginKey = this.App.getLoginKey();
            if (!srfLoginKey || Object.is(srfLoginKey, '')) {
                this.App.clearAccess();
                this.App.login();
                return false;
            }
        }
        return true;
    }

    /**
     * 加载appdata数据
     *
     * @memberof IBizIndexViewController
     */
    private loadAppData(route: ActivatedRouteSnapshot): Promise<any> {
        return new Promise((resolve: any, reject: any) => {
            if(!this.App.getAppData()){
                this.$http.get1(`${AppEnvironment.AppName.toLocaleLowerCase()}/app/${AppEnvironment.AppName.toLocaleLowerCase()}/getappdata`, {}).then((response: any) => {
                    if (!response) {
                        resolve(false);
                        return;
                    }
                    this.App.setAppData(response);
                    if (response && response.remotetag) {
                        window.localStorage.setItem('remotetag', response.remotetag);
                        resolve(true);
                    } else if (response && response.localdata) {
                        window.localStorage.setItem('localdata', response.localdata);
                        resolve(true);
                    } else {
                        // resolve(false);暂时返回true
                        resolve(true);
                    }
                })
            }else{
                resolve(true);
            }
        });
    }

}