const START_STREAMING = 'start_streaming';
const STOP_STREAMING = 'stop_streaming';
const MAIN_PAGE_STATE = 'app.main';
const HTTP = 'http';
const HTTPS = 'https';
const IS_LOCALHOST_NOW = location.hostname === "localhost" || location.hostname === "127.0.0.1";

export default class LayoutCtrl {
    constructor($scope, $state, $location, $window) {
        'ngInject';

        this._$location = $location;
        this._$window = $window;

        $scope.$on(START_STREAMING, () => {
            $scope.isClickedStartStreaming = true;
            $scope.isStartedStreaming = true;
            $scope.$apply();
        });
        $scope.$on(STOP_STREAMING, () =>
            $scope.isStartedStreaming = false);

        $scope.startStreaming = () =>
            $scope.isClickedStartStreaming = true;

        $scope.stopStreaming = () => {
            $scope.isClickedStartStreaming = false;
            $state.go(MAIN_PAGE_STATE);
        };

        this.checkHTTPS();
    }

    checkHTTPS() {
        if (IS_LOCALHOST_NOW) {
            return;
        }
        if (this._$location.protocol() !== HTTPS) {
            this._$window.location.href = this._$location.absUrl().replace(HTTP, HTTPS);
        }
    }
}