/**
 * Send Message js in sendMessage.html
 *
 * @since cms:1.4.3.2&1.4.3.3 web:1.0
 */
var SendMessage = {};

SendMessage.baseApiUrl = COMMON.format(ClientData.conf_checkApiUrl(), ClientData.userInfo_accountPath()) + CONSTANT.URL.CMS.API.SEND_PUSH_MESSAGE;
SendMessage.contentMaxLength = 207;

/**
 * Get input content
 * @returns string
 */
SendMessage.getCurrentMessageContent = function () {
    return $('#messageContent').val();
};

/**
 * Get operation id selected
 * @returns string
 */
SendMessage.getCurrentOperationId = function () {
    return $('#operationSelected').attr('data-operation-id');
};

/**
 * Get send type selected
 * @returns string
 */
SendMessage.getCurrentSendType = function () {
    return $('input[name="sendType"]:checked').val();
};

/**
 * Check data required when send message request to cms
 * @returns boolean
 */
SendMessage.checkValidation = function () {
    const message = SendMessage.getCurrentMessageContent();
    if (!ValidationUtil.CheckRequiredForText(message)) {
        COMMON.showAlert('msgContentRequired');
        return false;
    }
    if (!ValidationUtil.CheckMaxLengthForByte(message, SendMessage.contentMaxLength)) {
        COMMON.showAlert(COMMON.format(I18N.i18nText('msgContentInvalidLength'), SendMessage.contentMaxLength));
        return false;
    }
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
        COMMON.showAlert('msgOperationRequired');
        return false;
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
        COMMON.showAlert('msgSendTypeRequired');
        return false;
    }
    return true;
};

/**
 * handle click event of send button
 */
SendMessage.onClickSend = function () {
    if (!SendMessage.checkValidation()) {
        return;
    }
    const message = SendMessage.getCurrentMessageContent();
    const operationId = SendMessage.getCurrentOperationId();
    const sendType = SendMessage.getCurrentSendType();
    COMMON.showConfirm('msgSendPushMessageConfirm', function() {
        SendMessage.postMessage(message, operationId, sendType);
    });
};

/**
 * post message data to cms
 * @param {string} message
 * @param {long} operationId
 * @param {int} sendType - 0: Group, 1: All
 */
SendMessage.postMessage = function (message, operationId, sendType) {
    COMMON.showLoading();
    let param = {
        sid: COMMON.getSid(),
        message: message,
        operationId: operationId,
        sendType: sendType,
    };
    COMMON.cmsAjax(
        SendMessage.baseApiUrl,
        param,
        false,
        function (json) {
            COMMON.closeLoading();
            COMMON.showAlert('msgSendPushMessageSuccess', 'success');
        },
        function () {
            console.log('SendMessage.postMessage error');
            COMMON.closeLoading();
        },
    );
};

/**
 * operation selected callback
 * @param {Number} operationId
 * @param {*} operationName
 */
SendMessage.operationSelectedCallback = function (operationId, operationName) {
    $('#operationSelected').attr('data-operation-id', operationId);
    $('#operationSelected').text(operationName);
};

/**
 * template selected callback
 * @param {*} template
 */
SendMessage.templateSelectedCallback = function (template) {
    $('#messageContent').val(template);
};

/**
 * init data, action when screen onload
 */
SendMessage.init = function () {
    //Check if user is logged in
    COMMON.checkAuth(false);
    TEMPLATE.loadHeader('#includedHeader');
    TEMPLATE.loadConfirmModal();
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'sendMessage',
        },
    ];
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'sendMessage', navs, null);
    TEMPLATE.loadOperationSelect('#includeOperationSelect', SendMessage.operationSelectedCallback);
    TEMPLATE.loadNotificationSelect('#includeTemplateModal', SendMessage.templateSelectedCallback);
    $('#messageContent').attr('maxlength', SendMessage.contentMaxLength);
    //load lang for elements none class lang
    I18N.initi18n();
    $("label[for='sendTypeGroup']").append(I18N.i18nText('labelSendTypeGroup'));
    $("label[for='sendTypeAll']").append(I18N.i18nText('labelSendTypeAll'));

    COMMON.closeLoading();
};
