/**
 * 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)) {
        alert(I18N.i18nText('msgContentRequired'));
        return false;
    }
    if (!ValidationUtil.CheckMaxLengthForByte(message, SendMessage.contentMaxLength)) {
        alert(COMMON.format(I18N.i18nText('msgContentInvalidLength'), SendMessage.contentMaxLength));
        return false;
    }
    const operationId = SendMessage.getCurrentOperationId();
    if (!ValidationUtil.IsNumber(operationId)) {
        alert(I18N.i18nText('msgOperationRequired'));
        return false;
    }
    const sendType = SendMessage.getCurrentSendType();
    if (!ValidationUtil.IsNumber(sendType)) {
        alert(I18N.i18nText('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();
    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) {
    let param = {
        sid: COMMON.getSid(),
        message: message,
        operationId: operationId,
        sendType: sendType,
    };
    COMMON.cmsAjax(
        SendMessage.baseApiUrl,
        param,
        false,
        function (json) {},
        function () {
            console.log('SendMessage.postMessage error');
        },
    );
};

/**
 * 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.loadHearder('#includedHeader');
    const navs = [
        {
            titleLang: 'dashboard',
            href: 'dashboard.html',
        },
        {
            titleLang: 'sendMessageTitle',
        },
    ];
    TEMPLATE.loadMainNavsTitle('#includedMainTitle', 'sendMessageTitle', 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();
};
