/**
 * Webkul Software.
 * 
 * PHP version 7.0+
 *
 * @category  Webkul
 * @package   Webkul_Mobikul
 * @author    Webkul <support@webkul.com>
 * @copyright 2010-2019 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html ASL Licence
 * @link      https://store.webkul.com/license.html
 */
/*jshint jquery:true*/
define([
    "jquery",
    'mage/template',
    'mage/translate',
    'Magento_Ui/js/modal/alert',
    "jquery/ui",
], function ($, mageTemplate, $t, alert) {
    "use strict";
    $.widget('categorybannerimage.setattr', {
        _create: function () {
            var attribute = this.options;
            var clicked = false;
            var count = attribute.key;

            $('#container').on('click', '[data-index=mobikul-configuration]', function (event) {
                $('[data-index=ar_model_file_ios]').after($('#wk-mobikul-categorybannerimages').html());
                $('#wk-mobikul-categorybannerimages').remove();
                if (clicked == false) {
                    if (parseInt(attribute.arType) == 1) {
                        $('#container [data-index=ar_2d_file]').hide();
                        $('.categorybanner-image').show();
                    } else {
                        $('#container [data-index=ar_model_file_android]').hide();
                        $('#container [data-index=ar_model_file_ios]').hide();
                        $('.categorybanner-image').hide();
                    }
                    // setTimeout(function() {
                    //     $("#container #ar_model_file_android_model").text(attribute.ar_model_file_android_model);
                    //     $("#container #ar_model_file_ios_model").text(attribute.ar_model_file_ios_model);
                    // }, 1000);
                }
                clicked = true;
            });
            

            // $('#container').on('change', 'select[name="categoryimages[banner]"]', function (event) {
            //     if ($(this).val() == 1) {
            //         $('[data-index=ar_2d_file]').hide();
            //         $('[data-index=ar_model_file_android]').show();
            //         $('[data-index=ar_model_file_ios]').show();
            //         $('.texture-image').show();
            //         setTimeout(function() {
            //             $("#container #ar_model_file_android_model").text(attribute.ar_model_file_android_model);
            //             $("#container #ar_model_file_ios_model").text(attribute.ar_model_file_ios_model);
            //         }, 1000);
            //     } else {
            //         $('[data-index=ar_2d_file]').show();
            //         $('[data-index=ar_model_file_android]').hide();
            //         $('[data-index=ar_model_file_ios]').hide();
            //         $('.texture-image').hide();
            //     }
            // });

            $(document).on('click', '#wk-mobikul-categorybanner-add-more', function (event) {
                var progressTmpl = mageTemplate('#wk-categorybanner-template'),tmpl;
                tmpl = progressTmpl({
                        data: {
                            index: count,
                        }
                    }
                );
                // $('.categorybanner-image').last().after(tmpl);
                $(this).before(tmpl);
                count++;
            });

            $('#container').on('click', '#wk-mobikul-categorybanner-delete', function (event) {
                $(this).parent().parent().remove();
            });

            // $('#container').on('change', 'input[name="product[ar_model_file_android]"]', function (event) {
            //     var fileName = ($(this).val());
            //     var extension = fileName.substr(fileName.lastIndexOf('.') + 1);
            //     if (extension != "sfb") {
            //         alert({
            //             content:  $t("Ar model file for android must have sfb extension.")
            //         });
            //         $(this).val("");
            //         $(this).focus();
            //     }
            // });
            // $('#container').on('change', 'input[name="product[ar_model_file_ios]"]', function (event) {
            //     var fileName = ($(this).val());
            //     var extension = fileName.substr(fileName.lastIndexOf('.') + 1);
            //     if (extension != "obj") {
            //         alert({
            //             content:  $t("Ar model file for android must have obj extension.")
            //         });
            //         $(this).val("");
            //         $(this).focus();
            //     }
            // });

            // $('#container').on('change', 'input[name="product[ar_2d_file]"]', function (event) {
            //     var fileName = ($(this).val());
            //     var extension = fileName.substr(fileName.lastIndexOf('.') + 1);
            //     if (extension != "png") {
            //         alert({
            //             content:  $t("Ar 2D model file must have png extension.")
            //         });
            //         $(this).val("");
            //         $(this).focus();
            //     }
            // });

            $('#container').on('change', 'input[name^="mobikul_categoryimages[banner]"]', function (event) {
                var fileName = ($(this).val());
                var extension = fileName.substr(fileName.lastIndexOf('.') + 1);
                var extArray = ["png", "jpg", "jpeg"];
                if (jQuery.inArray(extension, extArray) === -1) {
                    alert({
                        content:  $t("Wrong file type given in category banner image.")
                    });
                    $(this).val("");
                    $(this).focus();
                }
            });
        }
    });
    return $.categorybannerimage.setattr;
});
