"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require('@angular/core');
var ng2_nvd3_1 = require('ng2-nvd3');
var angular2_websocket_1 = require('angular2-websocket/angular2-websocket');
var event_emitter_service_1 = require('../services/event-emitter.service');
var DashboardIntroComponent = (function () {
    function DashboardIntroComponent(el, emitter) {
        this.el = el;
        this.emitter = emitter;
        this.title = 'HubApp Dashboard';
        this.wsUrl = 'wss://hubapp-rfprod.c9users.io/appusage';
        this.countDown = 60;
        this.timerArr = [];
        this.chartOptions = {
            chart: {
                type: 'pieChart',
                height: 450,
                donut: true,
                x: function (d) { return d.key; },
                y: function (d) { return d.y; },
                showLabels: true,
                pie: {
                    startAngle: function (d) { return d.startAngle / 2 - Math.PI / 2; },
                    endAngle: function (d) { return d.endAngle / 2 - Math.PI / 2; }
                },
                duration: 1000,
                legend: {
                    margin: {
                        top: 5,
                        right: 140,
                        bottom: 5,
                        left: 0
                    }
                }
            }
        };
        this.appUsageData = [
            {
                key: "Default",
                y: 1
            },
            {
                key: "Default",
                y: 1
            },
            {
                key: "Default",
                y: 1
            },
            {
                key: "Default",
                y: 1
            },
            {
                key: "Default",
                y: 1
            }
        ];
        this.ws = new angular2_websocket_1.$WebSocket(this.wsUrl);
        //this.el.nativeElement;
    }
    DashboardIntroComponent.prototype.initWsTimer = function () {
        console.log('ws timer init');
        clearInterval(this.timerArr[0]);
        this.timerArr = [];
        this.countDown = 60;
    };
    DashboardIntroComponent.prototype.emitSpinnerStartEvent = function () {
        console.log('root spinner start event emitted');
        this.emitter.emitEvent({ sys: 'start spinner' });
    };
    DashboardIntroComponent.prototype.emitSpinnerStopEvent = function () {
        console.log('root spinner stop event emitted');
        this.emitter.emitEvent({ sys: 'stop spinner' });
    };
    DashboardIntroComponent.prototype.ngOnInit = function () {
        var _this = this;
        console.log('ngOnInit: DashboardIntroComponent initialized');
        this.emitSpinnerStartEvent();
        this.emitter.emitEvent({ route: '/intro' });
        this.emitter.emitEvent({ appInfo: 'show' });
        this.ws.send(JSON.stringify({ action: 'get' }));
        this.ws.onOpen(function (event) {
            console.log('ws connection opened, state ' + _this.ws.getReadyState());
        });
        this.ws.onMessage(function (message) {
            console.log('ws incoming message');
            if (_this.timerArr.length > 0)
                _this.initWsTimer();
            else
                _this.emitSpinnerStopEvent();
            var timer = setInterval(function () {
                _this.countDown--;
                console.log('ws status: ' + _this.ws.getReadyState() + ' | time left: ' + _this.countDown);
                if (_this.countDown == 0) {
                    clearInterval(_this.timerArr[0]);
                }
            }, 1000);
            _this.timerArr.push(timer);
            console.log(message);
            _this.appUsageData = [];
            var data = JSON.parse(message.data);
            for (var d in data) {
                _this.appUsageData.push(data[d]);
            }
        }, {});
        this.ws.onError(function (event) {
            console.log('ws connection error, state ' + _this.ws.getReadyState());
            console.log(event);
            _this.ws.close(true);
        });
        this.ws.onClose(function (event) {
            console.log('ws connection closed, state ' + _this.ws.getReadyState());
            console.log(event);
        });
        this.subscription = this.emitter.getEmitter().subscribe(function (message) {
            console.log('/intro consuming event: ' + JSON.stringify(message));
            if (message.sys === 'close websocket') {
                console.log('closing webcosket');
                _this.subscription.unsubscribe();
                _this.ws.close(true);
                _this.initWsTimer();
            }
        });
    };
    DashboardIntroComponent.prototype.ngOnDestroy = function () {
        console.log('ngOnDestroy: DashboardIntroComponent destroyed');
        this.subscription.unsubscribe();
        this.ws.close(true);
        this.initWsTimer();
        this.emitSpinnerStopEvent(); // this is relevant in case user switches to another view fast, not allowing data to load
    };
    DashboardIntroComponent = __decorate([
        core_1.Component({
            selector: 'dashboard-intro',
            templateUrl: "/public/js/angular/2.0/views/dashboard-intro.html",
            providers: [],
            directives: [ng2_nvd3_1.nvD3]
        }), 
        __metadata('design:paramtypes', [core_1.ElementRef, event_emitter_service_1.EventEmitterService])
    ], DashboardIntroComponent);
    return DashboardIntroComponent;
}());
exports.DashboardIntroComponent = DashboardIntroComponent;
//# sourceMappingURL=dashboard-intro.component.js.map