/**
 * Copyright (c) 2020
 *
 * CENTIRM HotelTV UI-UTILS module javascript.
 *
 * @summary short description for the file
 * @author Joel <joel.kim@centirm.com>
 *
 * Created at     : 2020-11-26 02:21:56
 * Last modified  : 2020-11-26 15:31:40
 */

HotelTV.namespace('HotelTV.ui_utils');
HotelTV.ui_utils = (function() {
    //// 의존 관계 선언

    //// 비공개 프로퍼티
    var g_uiVar_Clock = {
        time: '',
        date: ''
    };

    var g_uiVar_NotifyBallon = null;

    /// 초기화 루틴





    //// 비공개 매써드
    /**
     * HotelTV UI Wrapper Function::> Countdown timer for Error screen
     * @param {boolean} enable enable/disable
     */
    const __uiFxn_TmrCDnErrScreen = function(end_of_date, _cbFxn1, _cbFxn2) {
        var _vDate = new Date(end_of_date); // 전달 받은 일자
        var timer;

        function ____rn_cb() {
            var now = new Date();
            var distDt = _vDate - now;

            if (distDt < 0) {
                clearInterval(timer);
                if (_cbFxn2) {
                    _cbFxn2();
                }
                return;
            }

            if (_cbFxn1) {
                _cbFxn1();
            }
        }

        timer = setInterval(____rn_cb, 1000);
    }

    /**
     * HotelTV UI Wrapper Function::> Countdown timer for Error notification ballon
     * @param {boolean} enable enable/disable
     */
    const __uiFxn_TmrCDnNotiBallon = function(end_of_date, _cbClose) {
        var _vDate = new Date(end_of_date); // 전달 받은 일자

        function ____rn_cb() {
            var now = new Date();
            var distDt = _vDate - now;

            if (distDt < 0) {
                clearInterval(g_uiVar_NotifyBallon);
                if (_cbClose) {
                    _cbClose();
                }
                g_uiVar_NotifyBallon = null;
                return;
            }
        }

        g_uiVar_NotifyBallon = setInterval(____rn_cb, 1000);
    }

    /**
     * HotelTV UI Wrapper Function::> Show(Build) Busy(loading) animation
     * @param {boolean} enable enable/disable
     */
    function __uiFxn_MiscZeroPadding(num, digit) {
        var zero = '';
        for (var i = 0; i < digit; i++) {
            zero += '0';
        }
        return (zero + num).slice(-digit);
    }

    /**
     * HotelTV UI Wrapper Function::> Show(Build) Busy(loading) animation
     * @param {boolean} enable enable/disable
     */
    function __uifxn_BusyAnimation(enable) {
        if (enable) {
            //$('#busy_animation').css("background-image", "url('./images/icons/icon_loading_256x256')");
            $('.busy-frame').fadeIn(200, function() {
                $('.busy-frame #busy_animation').show();
            });
        } else {
            $('.busy-frame #busy_animation').fadeOut(300, function() {
                $('.busy-frame').fadeOut(500);
            });
        }
    }


    //// 공개 API
    return {
        SetBusy: function(show) {
            __uifxn_BusyAnimation(show);
        },

        ShowErrMsg: function(_show, _type, _title, _msg, _cdn_obj) {
            if (_show) {
                const typ_info = ['system.info.change-configuration'];
                if (typ_info.indexOf(_type) >= 0) {
                    $('.error #details #message').addClass("info");
                }
                $('.error').css("background-image", "url('./images/hoteltv_service_error.png')");
                $('.error #details #title').text(_title);
                $('.error #details #message').text(_msg);
                $('.error').fadeIn(500);
                if (_cdn_obj) {
                    var dt = new Date();
                    dt.setSeconds(dt.getSeconds() + Number(_cdn_obj.timeout));
                    __uiFxn_TmrCDnErrScreen(dt, _cdn_obj.cb_disp, _cdn_obj.cb_eoe);
                }
            } else {
                $('.error').fadeOut(500, function() {
                    $(this).css("background-image", "None");
                });
            }
        },

        ShowSysNoticeMsg: function(_show, _type, _title, _msg, _cdn_obj) {
            if (_show) {
                $('.error').css('background-color', '#000000').css('opacity', 0.8);
                if ( _type == "notice"){
                    $('.error #details #message').addClass("notice");
                }
                $('.error #details #title').text(_title);
                $('.error #details #message').text(_msg);
                $('.error').fadeIn(500);
                if (_cdn_obj) {
                    setTimeout(function(){
                        var dt = new Date();
                        dt.setSeconds(dt.getSeconds() + Number(_cdn_obj.timeout));
                        __uiFxn_TmrCDnErrScreen(dt, _cdn_obj.cb_disp, _cdn_obj.cb_eoe);
                    }, 5000);
                }
            } else {
                $('.error').fadeOut(500, function() {
                    $(this).css("background-image", "None");
                });
            }
        },

        GetCur_DateTime: function() {
            let week = ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
            let cd = new Date();
            g_uiVar_Clock.time = __uiFxn_MiscZeroPadding(cd.getHours(), 2) + ':' + __uiFxn_MiscZeroPadding(cd.getMinutes(), 2) + ':' + __uiFxn_MiscZeroPadding(cd.getSeconds(), 2);
            g_uiVar_Clock.date = __uiFxn_MiscZeroPadding(cd.getFullYear(), 4) + '-' + __uiFxn_MiscZeroPadding(cd.getMonth() + 1, 2) + '-' + __uiFxn_MiscZeroPadding(cd.getDate(), 2) + ' ' + week[cd.getDay()];
            return g_uiVar_Clock;
        },

        Get_Airline: function(_obj, _airline, _lang) {
            if (_obj) {
                if (_obj[_airline]) {
                    return _obj[_airline][_lang] || _airline;
                }
            }
            return _airline;
        },

        Get_Airport: function(_obj, _airport, _lang) {
            if (_obj) {
                if (_obj[_airport]) {
                    return _obj[_airport][_lang] || _airport;
                }
            }
            return _airport;
        },

        Get_FlightStatus: function(_obj, _status, _lang) {
            if (_obj && _obj.hasOwnProperty(_status) == true) {
                if (_status && _status != "") {
                    return _obj[_status][_lang] || _status;
                }
            }
            return _status;
        },

        Weather_GetWindReport: function(_str, _speed, _direction, _lang) {
            let _cvt_speed = parseFloat((_speed * 1000 / 3600)).toFixed(2);
            let _szDir = null;
            if (_direction >= 345 && _direction < 22) {
                _szDir = _str[4][_lang];
            } else if (_direction >= 23 && _direction < 68) {
                _szDir = _str[14][_lang];
            } else if (_direction >= 69 && _direction < 114) {
                _szDir = _str[1][_lang];
            } else if (_direction >= 115 && _direction < 160) {
                _szDir = _str[13][_lang];
            } else if (_direction >= 161 && _direction < 206) {
                _szDir = _str[3][_lang];
            } else if (_direction >= 207 && _direction < 252) {
                _szDir = _str[12][_lang];
            } else if (_direction >= 253 && _direction < 298) {
                _szDir = _str[2][_lang];
            } else {
                _szDir = _str[15][_lang];
            }
            return _cvt_speed.toString() + " " + _szDir;
        },

        ShowNotificationMsgBalloon: function(_msg, _tmoutInSec) {
            if (g_uiVar_NotifyBallon) {
                clearInterval(g_uiVar_NotifyBallon);
            }
            var dt = new Date();
            dt.setSeconds(dt.getSeconds() + Number(_tmoutInSec));

            if (_msg) {
                $('.popup>.notification .balloon #message').text(_msg);
                __uiFxn_TmrCDnNotiBallon(dt, function() {
                    $('.popup>.notification').fadeOut(300);
                });
                $('.popup>.notification').fadeIn(300);
            }
        },
    }
})();