﻿
// https://www.cssscript.com/touch-keen-slider/ 
// https://keen-slider.io/examples/#examples
(function ($, appCore, ws, unq) {

    //#region fields

    let api = {};
    let me = {};
    let pnlProductTop = null;

    me.photoStripDTO = {

        ProductType: 0,
        StripDataURL: null,
        StripWebURL : null,
        GraphicID   : null,
        MediaCode   : null,
        Quantity    : 1
    };


    //#endregion fields

    //#region data

    let AddToCart = function (dto) {

        return ws.ShoppingCartAPI.AddPhotoStripToShoppingCart(dto);
    };

    //#endregion data

    //#region properties

    me.ProductTypeID = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            return unq.HTML.GetControlValueAsInt(me.$hdnProductTypeID[0]);
        } else {

            unq.HTML.SetControlValueAsInt(me.$hdnProductTypeID[0], val);
        }
    };

    me.ProductID = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            return unq.HTML.GetControlValueAsInt(me.$hdnProductID[0]);
        } else {

            unq.HTML.SetControlValueAsInt(me.$hdnProductID[0], val);
        }
    };

    me.GraphicID = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            return unq.HTML.GetControlValueAsString(me.$hdnGraphicID[0]);
        } else {

            unq.HTML.SetControlValue(me.$hdnGraphicID[0], val);
        }
    };

    me.MediaCode = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            return unq.HTML.GetControlValueAsString(me.$hdnMediaCode[0]);
        } else {

            unq.HTML.SetControlValue(me.$hdnMediaCode[0], val);
        }
    };

    me.StripWebURL = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            return unq.HTML.GetControlValueAsString(me.$hdnStripWebURL[0]);
        } else {

            unq.HTML.SetControlValue(me.$hdnStripWebURL[0], val);
        }
    };

    me.Quantity = function (val) {

        if (unq.Utilities.IsNullOrUndefined(val)) {

            let qty = unq.HTML.GetControlValueAsInt(me.$txQuantity[0]);
            qty = (qty <= 0) ? 1 : qty;
            qty = (qty > 1000) ? 1000 : qty;

            return qty;

        } else {

            unq.HTML.SetControlValueAsInt(me.$txQuantity[0], val);
        }
    };

    //#endregion properties

    //#region events

    let btnAddToCart_OnClick = function (evt, $btn) {

        evt.preventDefault();
        me.MasterPage.ToggleShoppingCartButtonSpinner($btn, true);

        let reprintDTO = getDTOFromForm();

        AddToCart(reprintDTO).then(

            (data) => 
            {
                me.MasterPage.ToggleShoppingCartButtonSpinner($btn, false);
                me.MasterPage.IncrementShoppingCartItemCount(reprintDTO.Quantity);
                me.MasterPage.DisplayAlert("success", me.MasterPage.Messages.ItemAddedToCart);
            },
            (reason) => {

                me.MasterPage.ToggleShoppingCartButtonSpinner($btn, false);

                let errMsg = `AddToCart Failed: ${reason}`;
                me.MasterPage.DisplayAlert("danger", errMsg);
                me.MasterPage.HandleException(errMsg);
            }
        );        
    };

    //#endregion events

    //#region init

    function init() {

        initFields();
        initEvents();
        initCanvas();
        initSliders();

    };

    function initFields() {

        me.$btnAddToCart = $("a[id$='lnkAddToCart']");

        me.$hdnProductTypeID = $("input[id$='hdnProductTypeID']");
        me.$hdnProductID     = $("input[id$='hdnProductID']");
        me.$hdnGraphicID     = $("input[id$='hdnGraphicID']");
        me.$hdnMediaCode     = $("input[id$='hdnMediaCode']");
        me.$hdnStripWebURL   = $("input[id$='hdnStripWebURL']");
        me.$txQuantity       = $("input[id$='txReprintQty']");
        this.$pnlProductTitleTop = $("#pnlProductTitleTop");

        window.setTimeout(() => { me.MasterPage = window.BlabaBooth.Pages.Master; }, 100);

    };

    function initEvents() {

        me.$btnAddToCart.off("click").on("click", (evt) => {

            btnAddToCart_OnClick(evt, me.$btnAddToCart);
        });
    };

    function initCanvas() {
    };

    function initSliders() {
    };

    //#endregion init

    //#region private

    let getDTOFromForm  = function() {

        me.photoStripDTO.GraphicID   = me.GraphicID();
        me.photoStripDTO.MediaCode   = me.MediaCode();
        me.photoStripDTO.ProductType = me.ProductTypeID();
        me.photoStripDTO.Quantity    = me.Quantity();
        me.photoStripDTO.StripWebURL = me.StripWebURL();
        me.photoStripDTO.ProductID   = me.ProductID();

        return me.photoStripDTO;
    };

    //#endregion private


    $(document).ready(function () {

        window.BlabaBooth = (window.BlabaBooth == null) ? {} : window.BlabaBooth;
        window.BlabaBooth.Pages = (window.BlabaBooth.Pages == null) ? {} : window.BlabaBooth.Pages;
        window.BlabaBooth.Pages.ProductReprint = api;

        init();
    })

})(jQuery, window.APP.Core, window.IFOTO.WebServices, window.Unique);
