/*

  Copyright (C) 2020 willipink.eu
  Author Moritz Münch moritzmuench@mailbox.org

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see <http://www.gnu.org/licenses/>. */



/* create a modal dialog */
class Modal {

    constructor(div, callback, callback_arguments=null, input=null) {

        this.div = '#' + div;
        this.callback = callback;
        this.callback_arguments = callback_arguments;
        this.input = input;
        let self = this;

        if (this.input) {
            $(this.div + ' input[type=text]').each(function() {
                let name = $(this).prop('name');
                if (name in self.input) {
                    $(this).val(self.input[name]);
                }
            });
        }

        $(this.div + '_show')
            .bind('click', function(event) {
                self.show();
        });
    }


    /* show the modal dialog */
    show() {

        let self = this;

        $(this.div).fadeIn('fast');

        /* close modal on click outside of modal */
        $(document)
            .bind('click', function(event) {
                if ($(self.div).is(':visible')) {
                    if ($(event.target).attr('id') == self.div.substring(1)) {
                        hide(self.div);
                    }
                }
            });

        /* close modal on keypress ESC, send modal on keypress ENTER */
        $(document)
            .bind('keypress', function(event) {
                if (event.keyCode == 27) {
                    hide(self.div);
                    $(document).off('keypress');
                } else if (event.keyCode == 13) {
                    self.submit();
                }
            });

        /* close modal on button #<modal_name>_close */
        $(this.div + '_close')
            .bind('click', function(event) {
                hide(self.div);
            });

        /* trigger callback on button #<modal_name>_action */
        $(this.div + '_action')
            .bind('click', function(event) {
                self.submit();
            });

        /* focus first input text element */
        $(this.div + ' input:text:visible:first').focus();
    }


    /*submit modal */
    submit() {
        let response = {};
        $(this.div + ' input[type=text]').each(function() {
            response[$(this).attr('name')] = $(this).val();
        });
        this.callback(this.callback_arguments, response);
    }

}

/* hide the modal dialog */
function hide(div) {
    $(div).fadeOut('fast');
}

/* detach all eventTypes */
function detach(div) {
    hide(div);
    $(div + '_action').off('click');
    $(div + '_close').off('click');
    $(document).off('keypress click');
    $(div + ' input:text:visible').each(function() {
        $(this).val(''); });
}

export { Modal, detach, hide };
