module insite.layout {
    "use strict";

    export class SwitchModePopupController {
        mode: string;
        vmiDashboardUrl: string;
        homePageUrl?: string;
        cartPageUrl?: string;
        NavigationModeCookieName = "NavigationMode";

        static $inject = ["$window", "coreService", "ipCookie", "cartService", "vmiLocationsService", "switchModePopupService"];

        constructor(
            protected $window: ng.IWindowService,
            protected coreService: core.ICoreService,
            protected ipCookie: any,
            protected cartService: insite.cart.ICartService,
            protected vmiLocationsService: catalog.IVmiLocationsService,
            protected switchModePopupService: insite.layout.ISwitchModePopupService) {
        }

        $onInit(): void {
            const popup = angular.element("#switchModePopup");

            this.switchModePopupService.registerDisplayFunction((data) => {
                this.mode = data.mode;
                this.vmiDashboardUrl = data.vmiDashboardUrl;
                this.homePageUrl = data.homePageUrl;
                this.cartPageUrl = data.cartPageUrl;
                this.coreService.displayModal(popup);
            });
        }

        protected closeModal(): void {
            this.coreService.closeModal("#switchModePopup");
        }

        switchMode(): void {
            const isSecure = window.location.protocol.indexOf("https") > -1;
            this.ipCookie(this.NavigationModeCookieName, this.mode, { path: "/", secure: isSecure });
            if (this.mode === "Vmi") {
                this.createAlternateCart();
            } else {
                const currentCart = this.cartService.getLoadedCurrentCart();
                if (currentCart.status === "AlternateCart") {
                    this.cartService.removeCartById({ cartId: "current", isAlternateCart: true });
                }

                this.homePageUrl ? this.$window.location.href = this.homePageUrl : this.$window.location.reload();
            }
        }

        protected navigateToVmiDashboard(): void {
            this.$window.location.href = this.vmiDashboardUrl;
        }

        protected createAlternateCart(): void {
            const vmiLocationsFilter: insite.catalog.ISearchFilter = {
              page: 1,
              pageSize: 1,
              sort: "Name",
            };
            this.vmiLocationsService.getVmiLocations(vmiLocationsFilter).then(
                (locationCollection: VmiLocationCollectionModel) => {
                    const vmiLocations = locationCollection.vmiLocations;
                    if (vmiLocations && vmiLocations.length > 0) {
                        this.cartService.addCart(vmiLocations[0].id).then(
                            () => {
                                this.navigateToVmiDashboard();
                            },
                            () => this.navigateToVmiDashboard()
                        );
                    } else {
                        this.navigateToVmiDashboard();
                    }
                },
                () => this.navigateToVmiDashboard()
            );
        }

        backToCart(): void {
            this.cartPageUrl ? this.$window.location.href = this.cartPageUrl : this.$window.location.reload();
        }
    }

    export interface ISwitchModePopupService {
        display(data: any): void;
        registerDisplayFunction(p: (data: any) => void);
    }

    export class SwitchModePopupService extends base.BasePopupService<any> implements ISwitchModePopupService {
        protected getDirectiveHtml(): string {
            return "<isc-switch-mode-popup></isc-switch-mode-popup>";
        }
    }

    angular
        .module("insite")
        .controller("SwitchModePopupController", SwitchModePopupController)
        .service("switchModePopupService", SwitchModePopupService)
        .directive("iscSwitchModePopup", () => ({
            restrict: "E",
            replace: true,
            templateUrl: "/PartialViews/Vmi-SwitchModePopup",
            controller: "SwitchModePopupController",
            controllerAs: "vm",
            bindToController: true
        }));
}