import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'ember-twiddle/tests/helpers/start-app';

module('Acceptance | routing', {
  beforeEach: function() {
    this.application = startApp();

    this.registerWaiter = () => {
      window.messagesWaiting = 1;
      this._waiter = () => {
        return window.messagesWaiting === 0;
      };
      Ember.Test.registerWaiter(this._waiter);
    };
  },

  afterEach: function() {
    window.messagesWaiting = 0;
    if (this._waiter) {
      Ember.Test.unregisterWaiter(this._waiter);
      this._waiter = null;
    }

    Ember.run(this.application, 'destroy');
  }
});

const aboutLink = '.test-about-link';
const indexLink = '.test-index-link';
const addressBar = '.url-bar input';
const outletText = 'p';

const TWIDDLE_WITH_ROUTES = [
  {
    filename: "about.template.hbs",
    content: "<p>About Page</p>"
  },
  {
    filename: "index.template.hbs",
    content: "<p>Main Page</p>"
  },
  {
    filename: "application.template.hbs",
    content: `{{#link-to "index" class="test-index-link"}}Index{{/link-to}}
              {{#link-to "about" class="test-about-link"}}About{{/link-to}}

              {{outlet}}`
  },
  {
    filename: "router.js",
    content: `import Ember from 'ember';
              import config from './config/environment';

              var Router = Ember.Router.extend({
                location: config.locationType
              });

              Router.map(function() {
                this.route("about");
              });

              export default Router;`
  },
  {
    filename: "twiddle.json",
    content: `{
                "version": "0.4.0",
                "dependencies": {
                  "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
                  "ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.10/ember.js",
                  "ember-data": "https://cdnjs.cloudflare.com/ajax/libs/ember-data.js/1.13.13/ember-data.js",
                  "ember-template-compiler": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.13.10/ember-template-compiler.js"
                }
              }`
  }
];

test('Able to do routing in a gist', function(assert) {
  let iframe_window;

  runGist(TWIDDLE_WITH_ROUTES);

  andThen(() => {
    this.registerWaiter();
  });

  andThen(() => {
    assert.equal(find(addressBar).val(), '/', "Correct URL is shown in address bar 0");
    assert.ok(decodeURIComponent(window.location.search).indexOf("route=") === -1, "URL is not added to route query string parameter 0");

    this.registerWaiter();
    iframe_window = outputPane();
    iframe_window.click(iframe_window.find(aboutLink));
  });

  andThen(() => {
    assert.equal(outputContents(outletText), 'About Page', 'About Link leads to About Page being displayed');
    assert.equal(find(addressBar).val(), '/about', "Correct URL is shown in address bar 1");
    assert.ok(decodeURIComponent(window.location.search).indexOf("route=/about") > 0, "URL is added to route query string parameter 1");

    this.registerWaiter();
    iframe_window.click(iframe_window.find(indexLink));
  });

  andThen(() => {
    assert.equal(outputContents(outletText), 'Main Page', 'Index Link leads to Main Page being displayed');
    assert.equal(find(addressBar).val(), '/', "Correct URL is shown in address bar 2");
    assert.ok(decodeURIComponent(window.location.search).indexOf("route=") === -1, "URL is not added to route query string parameter 2");
  });
});

test('URL can be changed via the address bar', function(assert) {
  runGist(TWIDDLE_WITH_ROUTES);

  andThen(() => {
    this.registerWaiter();
  });

  andThen(function() {
    assert.equal(find(addressBar).val(), '/', "Correct URL is shown in address bar 0");

    click(addressBar);
    fillIn(addressBar, '/about');
    keyEvent(addressBar, 'keyup', 13);
  });

  andThen(function() {
    assert.equal(outputContents(outletText), 'About Page', 'Changing the URL to /about and pressing enter leads to the About Page being displayed');
    assert.equal(find(addressBar).val(), '/about', "Correct URL is shown in address bar 1");

    click(addressBar);
    fillIn(addressBar, '/');
    keyEvent(addressBar, 'keyup', 13);
  });

  andThen(function() {
    assert.equal(outputContents(outletText), 'Main Page', 'Changing the URL to / and pressing enter leads to the Main Page being displayed');
    assert.equal(find(addressBar).val(), '/', "Correct URL is shown in address bar 2");
  });
});

test('URL can be set via route query parameter', function(assert) {
  runGist({
    files: TWIDDLE_WITH_ROUTES,
    initialRoute: "/about"
  });

  andThen(() => {
    this.registerWaiter();
  });

  andThen(function() {
    assert.equal(find(addressBar).val(), "/about", "Correct URL appears when set via query parameter");
    assert.equal(outputContents(outletText), 'About Page', 'Initializing the URL to /about leads to the About Page being displayed');
  });
});

@@ -2293,32 +2293,69 @@
 andThen(() =%3E %7B%0A
+    if (!find(addressBar).val()) %7B%0A  
     this.registe
@@ -2357,32 +2357,38 @@
 gisterWaiter();%0A
+    %7D%0A
   %7D);%0A%0A  andThen
@@ -3668,32 +3668,69 @@
 andThen(() =%3E %7B%0A
+    if (!find(addressBar).val()) %7B%0A  
     this.registe
@@ -3732,32 +3732,38 @@
 gisterWaiter();%0A
+    %7D%0A
   %7D);%0A%0A  andThen
@@ -4756,24 +4756,61 @@
 hen(() =%3E %7B%0A
+    if (!find(addressBar).val()) %7B%0A  
     this.reg
@@ -4824,16 +4824,22 @@
 iter();%0A
+    %7D%0A
   %7D);%0A%0A 

Fix test timing out in chrome