/* global PhotoSwipe */
/* global PhotoSwipeUI_Default */

import Em from 'ember';

var run = Em.run;

export default Em.Component.extend({

  onInsert: Em.on('didInsertElement', function() {

    Em.run.scheduleOnce('afterRender', this, function() {
      this.set('pswpEl', this.$('.pswp')[0]);
      this.set('pswpTheme', PhotoSwipeUI_Default);

      this._buildOptions();

      /**
       * DEPRECATED
       * 
       * Code exists for backward compatability of block usage 
       * up to ember-cli-photoswipe versions 1.0.1. 
       */
      if (this.get('items')) {
        return this._initItemGallery();
      }
      console.log("WARNING: See https://github.com/poetic/ember-cli-photoswipe#usage");
      return this._calculateItems();
      /**
       * END DEPRECATED
       */
    });
  }),

  _buildOptions: function(getThumbBoundsFn) {
     var reqOpts = {
      history: false
    };

    if (Em.isPresent(getThumbBoundsFn)) {
      reqOpts.getThumbBoundsFn = getThumbBoundsFn;
    }

    var options = Em.merge(reqOpts, this.get('options') || {});
    this.set('options', options);
  },

  _initItemGallery: function() {
    this.set('gallery', new PhotoSwipe(
      this.get('pswpEl'),
      this.get('pswpTheme'),
      this.get('items'),
      this.get('options')
    ));
    this._reInitOnClose();
  },

  _reInitOnClose: function() {
    var component = this;
    this.get('gallery').listen('close', function() {
      run.next(function() {
        component._initItemGallery();
      });
    });
  },

  itemObserver: Em.observer('items', function(){
    var component = this;
    component._initItemGallery();
  }),
  
  /**
   * DEPRECATED
   * 
   * Code exists for backward compatability of block usage 
   * up to ember-cli-photoswipe versions 1.0.1. 
   */  
  click: function(evt) {

    if (this.get('items')) {
      return; // ignore - not using deprecated block form
    }

    var aElement = this.$(evt.target).parent();
    var index    = this.$("a.photo-item").index( aElement );

    if (!aElement.is('a')) { return; }

    evt.preventDefault();

    // setup options, such as index for index
    this._buildOptions(this._getBounds.bind(this));
    this.set('options.index', index);

    var pSwipe = new PhotoSwipe(
      this.get('pswpEl'),
      this.get('pswpTheme'),
      this.get('calculatedItems'),
      this.get('options')
    );
    this.set('gallery', pSwipe);
    this.get('gallery').init();
  }, 
  /**
   * END DEPRECATED
   */   

  _getBounds: function(i) {
    var img      = this.$('img').get(i),
        position = this.$(img).position(),
        width    = this.$(img).width();
    return {x: position.left, y: position.top, w: width};
  },

  actions: {
    launchGallery(item) {
      this._buildOptions(this._getBounds.bind(this));
      if (item !== undefined) {
        var index = this.get('items').indexOf(item);
        this.set('options.index', index);
      }
      var pSwipe = new PhotoSwipe(
        this.get('pswpEl'),
        this.get('pswpTheme'),
        this.get('items'),
        this.get('options')
      );
      this.set('gallery', pSwipe);
      this.get('gallery').init();
    }
  },


  /**
   * DEPRECATED
   * 
   * Code exists for backward compatability of block usage 
   * up to ember-cli-photoswipe versions 1.0.1. 
   */  
  _calculateItems: function() {
    var items           = this.$().find('a');
    var calculatedItems = Em.A(items).map(function(i, item) {
      return {
        src:   Em.$(item).attr('href'),
        w:     Em.$(item).data('width'),
        h:     Em.$(item).data('height'),
        msrc:  Em.$(item).children('img').attr('src'),
        title: Em.$(item).children('img').attr('alt')
      };
    });
    this.set('calculatedItems', calculatedItems);
  }  
  /**
   * END DEPRECATED
   */      

});

@@ -367,24 +367,104 @@
 Options();%0A%0A
+      if (this.get('items')) %7B%0A        return this._initItemGallery();%0A      %7D%0A%0A
       /**%0A  
@@ -485,25 +485,24 @@
 TED%0A       *
- 
 %0A       * Co
@@ -522,33 +522,33 @@
  backward compat
-a
+i
 bility of block 
@@ -548,25 +548,24 @@
  block usage
- 
 %0A       * up
@@ -600,25 +600,24 @@
 sions 1.0.1.
- 
 %0A       */%0A 
@@ -619,175 +619,8 @@
  */%0A
-      if (this.get('items')) %7B%0A        return this._initItemGallery();%0A      %7D%0A      console.log(%22WARNING: See https://github.com/poetic/ember-cli-photoswipe#usage%22);%0A
     
@@ -1546,18 +1546,16 @@
 ;%0A  %7D),%0A
-  
 %0A  /**%0A 
@@ -1565,33 +1565,32 @@
  DEPRECATED%0A   *
- 
 %0A   * Code exist
@@ -1602,33 +1602,33 @@
  backward compat
-a
+i
 bility of block 
@@ -1624,33 +1624,32 @@
 y of block usage
- 
 %0A   * up to embe
@@ -1680,25 +1680,22 @@
 s 1.0.1.
- 
 %0A   */
-  
 %0A  click
@@ -2346,17 +2346,16 @@
 ();%0A  %7D,
- 
 %0A  /**%0A 
@@ -2378,19 +2378,16 @@
 ED%0A   */
-   
 %0A%0A  _get
@@ -3090,17 +3090,16 @@
 TED%0A   *
- 
 %0A   * Co
@@ -3127,17 +3127,17 @@
 d compat
-a
+i
 bility o
@@ -3149,17 +3149,16 @@
 ck usage
- 
 %0A   * up
@@ -3201,17 +3201,14 @@
 0.1.
- 
 %0A   */
-  
 %0A  _
@@ -3228,32 +3228,283 @@
 s: function() %7B%0A
+    Em.deprecate(%0A      %22Using ember-cli-photoswipe without an items attribute is deprecated. %22+%0A      %22See https://github.com/poetic/ember-cli-photoswipe#usage%22,%0A      false,%0A      %7Bid: 'ember-cli-photoswipe.didInsertElement', until: '1.13'%7D%0A    );%0A%0A
     var items   
@@ -3912,18 +3912,16 @@
 ms);%0A  %7D
-  
 %0A  /**%0A 
@@ -3944,20 +3944,14 @@
 ED%0A   */
-      
 %0A%0A%7D);%0A

deprecate correctly