define([], function() {

  // 定义资源目录
  var assetsDir = 'assets/advance/';

  /**
   * 获取模板相对路径
   * 
   * @returns {string} 模板路径
   * 
   */
  var templatePath = (function() {
    var src = document.getElementById('require').getAttribute('src');
    var corePathIndex = src.indexOf('js/core/');

    if (corePathIndex > -1) {
      var basePath = src.substring(0, corePathIndex);
      if (basePath && basePath.charAt(basePath.length - 1) !== '/') {
        basePath += '/';
      }

      return basePath;
    }

    var lastSlashIndex = src.lastIndexOf('/');
    return lastSlashIndex > -1 ? src.substring(0, lastSlashIndex + 1) : '';
  })();

  /**
   * 获取浏览器信息
   * 
   * @returns {{isIE: boolean, version: number|string, name: string, fullInfo: string}} 信息对象
   * 
   */
  var browserInfo = (function() {
    var ua = navigator.userAgent.toLowerCase();

    var browserInfo = {
      isIE: false,
      version: null,
      name: null,
      fullInfo: ua
    };

    if (/msie|trident/.test(ua)) {
      browserInfo.isIE = true;
      browserInfo.name = 'Internet Explorer';

      var msieMatch = ua.match(/msie\s(\d+)/);

      if (msieMatch) {
        browserInfo.version = parseInt(msieMatch[1], 10);
      } else {
        var tridentMatch = ua.match(/rv:(\d+)/);

        if (tridentMatch) {
          browserInfo.version = parseInt(tridentMatch[1], 10);
        }
      }
      return browserInfo;
    }

    var browserMatchers = [
      { name: '360 Browser', regex: /(?:360se|360ee)/ },
      { name: 'QQ Browser', regex: /(?:qq|mqqb)browser\/(\d+)/ },
      { name: 'UC Browser', regex: /(?:ucbrowser|uc)\/(\d+)/ },
      { name: 'Baidu Browser', regex: /baidu(?:hd|mobile)?\/(\d+)/ },
      { name: 'Samsung Browser', regex: /samsungbrowser\/(\d+)/ },
      { name: 'Edge', regex: /edg\/(\d+)/ },
      { name: 'Edge-legacy', regex: /edge\/(\d+)/ },
      { name: 'Opera', regex: /(?:opr|opera)\/(\d+)/ },
      { name: 'Chrome', regex: /chrome\/(\d+)/ },
      { name: 'Firefox', regex: /firefox\/(\d+)/ },
      { name: 'Safari', regex: /version\/(\d+).*?safari/ }
    ];

    for (var i = 0; i < browserMatchers.length; i++) {
      var match = ua.match(browserMatchers[i].regex);

      if (match) {
        browserInfo.name = browserMatchers[i].name;
        browserInfo.version = parseInt(match[1], 10);
        break;
      }
    }

    return browserInfo;
  })();

  /**
   * 监听页面加载事件
   * 
   * @param {Function} callback - 事件的回调函数
   * @returns {{remove: Function}|undefined} 提供移除监听器方法
   * 
   */
  function executePageLoaded(callback) {
    var isLegacy = !window.addEventListener;

    if (document.readyState === 'complete') {
      setTimeout(callback, 0);
      return;
    }

    if (isLegacy) {
      window.attachEvent('onload', callback);
    } else {
      window.addEventListener('load', callback, false);
    }

    return {
      remove: function() {
        if (isLegacy) {
          window.detachEvent('onload', callback);
        } else {
          window.removeEventListener('load', callback, false);
        }
      }
    };
  }

  /**
   * 渲染浏览器升级提示
   * 
   * @returns {void} 无返回值
   * 
   */
  function createUpdatePrompt() {
    var style = document.createElement('style');
    
    var cssText = 
      '#browser-modal {position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 9999;}' +
      '.browser-modal-cover {position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: #000; opacity: 0.7; filter: alpha(opacity=70);}' +
      '.browser-content {position: absolute; top: 50%; left: 50%; width: 660px; margin: -200px 0 0 -330px; padding: 24px; background: #fff; border-radius: 8px; box-shadow: 0 0 20px rgba(0,0,0,0.5);}' +
      '.browser-text {text-align: center; margin-bottom: 30px;}' +
      '.browser-dpc-logo {width: 60%; margin: 0 auto 24px;}' +
      '.browser-dpc-logo img {width: 100%;}' +
      '.browser-text-title {font-size: 24px; color: #333; margin-bottom: 10px;}' +
      '.browser-text-desc {font-size: 16px; color: #666;}' +
      '.browser-list {display: flex; justify-content: center; flex-wrap: wrap;}' +
      '.browser-item {width: 120px; text-align: center; margin: 0 15px;}' +
      '.browser-item a {display: block; color: #333; text-decoration: none;}' +
      '.browser-item a: hover {color: #0066cc;}' +
      '.browser-item img {width: 64px; margin-bottom: 6px;}' +
      '.iconfont {display: block; width: 64px; height: 64px; margin: 0 auto 10px; background-size: contain; background-repeat: no-repeat;}';
    
    if (style.styleSheet) {
      style.styleSheet.cssText = cssText;
    } else {
      style.appendChild(document.createTextNode(cssText));
    }
    
    document.getElementsByTagName('head')[0].appendChild(style);

    var browsers = [
      { name: 'Chrome', url: 'https://www.google.cn/intl/zh-CN/chrome', icon: 'chrome' },
      { name: 'Firefox', url: 'https://www.firefox.com/zh-CN', icon: 'firefox' },
      { name: 'Opera', url: 'https://www.opera.com/zh-cn/opera', icon: 'opera' },
      { name: 'Edge', url: 'https://www.microsoft.com/edge', icon: 'edge' }
    ];

    var browserListHTML = '';

    for (var i = 0; i < browsers.length; i++) {
      var browser = browsers[i];
      
      browserListHTML += 
        '<div class="browser-item">' +
        '  <a href="' + browser.url + '" target="_blank">' +
        '    <img src="' + templatePath + assetsDir + browser.icon + '.png" alt="Browser" />' +
        '    <h4>' + browser.name + '</h4>' +
        '  </a>' +
        '</div>';
    }

    var modalHTML = 
      '<div id="browser-modal">' +
        '<div class="browser-modal-cover"></div>' +
        '<div class="browser-content">' +
          '<div class="browser-text">' +
            '<div class="browser-dpc-logo">' +
              '<img src="' + templatePath + 'images/logo.png" alt="Web Site Logo" />' +
            '</div>' +
            '<h3 class="browser-text-title">请升级浏览器版本</h3>' +
            '<p class="browser-text-desc">你正在使用旧版本浏览器。请升级浏览器以获得更好的体验。</p>' +
          '</div>' +
          '<div class="browser-list">' +
            browserListHTML +
          '</div>' +
        '</div>' +
      '</div>';

    var body = document.body;
    body.innerHTML = modalHTML;
  }

  return { assetsDir, templatePath, browserInfo, executePageLoaded: executePageLoaded, createUpdatePrompt: createUpdatePrompt };

});
