/**
 * @author 1902 Software
 * @copyright Copyright © 2023 1902 Software (https://1902software.com/magento/)
 * @package WriteTextAI_WriteTextAI
 */

define([
    "jquery",
    "ko",
    "mage/translate",
    "uiComponent",
    "underscore",
    'WriteTextAI_WriteTextAI/js/edit/keywords/keyword-analysis',
    'WriteTextAI_WriteTextAI/js/model/edit/keywords/keyword-analysis',
    'WriteTextAI_WriteTextAI/js/model/edit/invalid-image-popup',
    'WriteTextAI_WriteTextAI/js/model/edit/keywords/keywords',
    "Magento_Ui/js/modal/modal"
], function (
    $,
    ko,
    $t,
    Component,
    _,
    keywordAnalysisComponent,
    keywordAnalysis,
    invalidImagePopupModel,
    keywordsData
) {
    'use strict';

    /**
     * @var {Component} self
     */
    var self;

    return Component.extend({
        defaults: {
            saveUrl: '',
            doNotShowAgainValue: false
        },

        /** @inheritdoc */
        initialize: function () {
            this._super();

            self = this;

            const el = document.querySelector('.wtai-keywords-invalid-image-popup');
            if (el) {
                $('.wtai-keywords-invalid-image-popup > *').applyBindings(this);
            }

            this.initModal();
        },

        /** @inheritdoc */
        initObservable: function () {
            this._super()
                .observe([
                ]);

            this.doNotShowAgain = ko.observable(!!this.doNotShowAgainValue);
            invalidImagePopupModel.doNotShowAgain(!!this.doNotShowAgainValue);

            this.doNotShowAgain.subscribe(function (val) {
                invalidImagePopupModel.doNotShowAgain(!!val);
            }, this);

            this.showProceed = ko.computed(function () {
                return keywordAnalysis.imageUploadFailed();
            });

            this.getThumbnail = ko.computed(function () {
                return keywordsData.thumbnail();
            });

            this.uploadErrorMessage = ko.computed(function () {
                return keywordAnalysis.uploadErrorMessage();
            });

            return this;
        },

        /**
         * Initialize modal
         */
        initModal: function () {
            $('.wtai-keywords-invalid-image-popup').modal({
                type: 'popup',
                responsive: true,
                title: $t('Some images could not be processed:'),
                modalClass: 'wtai-keywords-invalid-image-popup-modal',
                overlayClass: 'modals-overlay wtai-keywords-invalid-image-popup-overlay',
                buttons: [{
                    text: $t('Cancel'),
                    class: 'action-secondary action-dismiss',
                    click: function () {
                        self.doNotShowAgain(!!self.doNotShowAgainValue);
                        keywordAnalysis.optimizing(false);
                        keywordAnalysis.isUploading(false);
                        keywordAnalysis.imageUploadFailed(false);
                        this.closeModal();
                    }
                }, {
                    text: $t('Proceed'),
                    class: 'action-primary wtai-action-proceed',
                    click: function () {
                        window.wtaiInvalidImagePopupDoNotShowAgain = !!self.doNotShowAgain();
                        self.doNotShowAgainValue = !!self.doNotShowAgain();
                        if (self.doNotShowAgainValue) {
                            self.saveDoNotShowAgainSetting(self.doNotShowAgain());
                        }
                        keywordAnalysisComponent.prototype.proceed();
                        this.closeModal();
                    }
                }],
                closed: function () {
                    self.doNotShowAgain(!!self.doNotShowAgainValue);
                    keywordAnalysis.optimizing(false);
                    keywordAnalysis.isUploading(false);
                    keywordAnalysis.imageUploadFailed(false);
                    $('.modals-overlay.wtai-keywords-invalid-image-popup-overlay').remove();
                },
                opened: function () {
                    $('.wtai-keywords-invalid-image-popup-modal .modal-footer .action-dismiss').toggle(self.showProceed());
                    $('.wtai-keywords-invalid-image-popup-modal .modal-footer .wtai-action-proceed').toggle(self.showProceed());

                    var $modal = $('.wtai-keywords-invalid-image-popup-modal');
                    var $footer = $modal.find('.modal-footer');
                    var $doNotShow = $modal.find('.wtai-do-not-show-again');

                    if ($doNotShow.length && $footer.find('.wtai-do-not-show-again').length === 0) {
                        $doNotShow.detach().prependTo($footer);
                    }
                }
            });
        },

        saveDoNotShowAgainSetting: function (val) {
            if (!this.saveUrl) {
                return;
            }

            $.ajax({
                url: this.saveUrl,
                type: 'POST',
                dataType: 'json',
                showWriteTextAILoader: false,
                data: {
                    form_key: window.FORM_KEY,
                    invalid_image_popup_do_not_show_again: !!val,
                    entity_type: '',
                    scope: 'edit'
                },
                success: function (response) {
                    if (!response.success) {
                        console.log(response.message);
                    }
                }
            });
        }
    });
});
