/*global Modernizr:true*/

// Initializes an object that lets us know about our capabilities.
export default {
  name: "sniff-capabilities",
  initialize(container, application) {
    const $html = $('html'),
          touch = $html.hasClass('touch') || (Modernizr.prefixed("MaxTouchPoints", navigator) > 1),
          caps = {touch};

    // Store the touch ability in our capabilities object
    $html.addClass(touch ? 'discourse-touch' : 'discourse-no-touch');

    // Detect Devices
    if (navigator) {
      const ua = navigator.userAgent;
      if (ua) {
        caps.isAndroid = ua.indexOf('Android') !== -1;
        caps.isWinphone = ua.indexOf('Windows Phone') !== -1;

        caps.isOpera = !!window.opera || ua.indexOf(' OPR/') >= 0;
        caps.isFirefox =  typeof InstallTrigger !== 'undefined';
        caps.isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
        caps.isChrome = !!window.chrome && !caps.isOpera;
        caps.canPasteImages = caps.isChrome || caps.isFirefox;
      }

      caps.isIOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;
    }

    // We consider high res a device with 1280 horizontal pixels. High DPI tablets like
    // iPads should report as 1024.
    caps.highRes = window.screen.width >= 1280;

    // Inject it
    application.register('capabilities:main', caps, { instantiate: false });
    application.inject('view', 'capabilities', 'capabilities:main');
    application.inject('controller', 'capabilities', 'capabilities:main');
    application.inject('component', 'capabilities', 'capabilities:main');
  }
};
