module insite.layout {
    "use strict";

    export class VmiCartPopupController {
        cart: CartModel;
        cartLocationName: string;
        selectedLocationId: string;
        onClose: any;

        static $inject = ["$window", "coreService", "cartService", "vmiCartPopupService", "$timeout", "$location"];

        constructor(
            protected $window: ng.IWindowService,
            protected coreService: core.ICoreService,
            protected cartService: insite.cart.ICartService,
            protected vmiCartPopupService: insite.layout.IVmiCartPopupService,
            protected $timeout: ng.ITimeoutService,
            protected $location: ng.ILocationService) {
        }

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

            this.vmiCartPopupService.registerDisplayFunction((data) => {
                this.cart = data.cart;
                this.cartLocationName = data.cartLocationName;
                this.selectedLocationId = data.selectedLocationId;
                this.onClose = data.onClose;
                this.coreService.displayModal(popup, () => {
                    if (typeof (this.onClose) !== "undefined") {
                        this.onClose();
                    }
                });
            });
        }

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

        continue(): void {
            // Remove cart
            this.cartService.removeCart(this.cart).then(
                () => {
                    // Create new cart
                    this.cartService.addCart(this.selectedLocationId).then(
                        () => {
                            this.updateQueryString(this.selectedLocationId);
                            this.cartService.getCart();
                        },
                        (error: any) => { this.addCartFailed(error); });
                },
                (error: any) => { this.deleteSavedOrderFailed(error); });
        }

        protected addCartFailed(error: any): void {
        }

        protected deleteSavedOrderFailed(error: any): void {
        }

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

        protected updateQueryString(locationId: string): void {
            this.$timeout(() => {
                this.$location.search("locationId", locationId);
            }, 0);
        }
    }

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

    export class VmiCartPopupService extends base.BasePopupService<any> implements IVmiCartPopupService {
        protected getDirectiveHtml(): string {
            return "<isc-vmi-cart-popup></isc-vmi-cart-popup>";
        }
    }

    angular
        .module("insite")
        .controller("VmiCartPopupController", VmiCartPopupController)
        .service("vmiCartPopupService", VmiCartPopupService)
        .directive("iscVmiCartPopup", () => ({
            restrict: "E",
            replace: true,
            templateUrl: "/PartialViews/Vmi-VmiCartPopup",
            controller: "VmiCartPopupController",
            controllerAs: "vm",
            bindToController: true
        }));
}