/*!
 * Copyright (c) 2014-present Cliqz GmbH. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

import {
  blurUrlBar,
  $cliqzResults,
  expect,
  fillIn,
  mockGetSearchEngines,
  mockSearch,
  newTab,
  press,
  sleep,
  testServer,
  urlbar,
  waitFor,
  waitForPopup,
  withHistory,
} from './helpers';

import { getResourceUrl } from '../../../tests/core/integration/helpers';

export default function () {
  context('last query box', function () {
    const freshtabUrl = getResourceUrl('freshtab/home.html');

    context('for "Search with"', function () {
      const url = 'https://url_test.com';
      const query = 'test';
      let unmockGetSearchEngines;

      beforeEach(async function () {
        await testServer.registerPathHandler('/search', { result: '<html><body><p>success</p></body></html>' });

        unmockGetSearchEngines = mockGetSearchEngines([
          {
            alias: '#te',
            default: true,
            description: 'Test Search',
            encoding: 'UTF-8',
            icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyppVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMTMyIDc5LjE1OTI4NCwgMjAxNi8wNC8xOS0xMzoxMzo0MCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp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uQmCC',
            identifier: 'test-b-d',
            name: 'Test',
            searchForm: testServer.getBaseUrl('search?q={searchTerms}'),
            urls: {
              'text/html': {
                method: 'GET',
                template: testServer.getBaseUrl('search?q={searchTerms}'),
                params: []
              },
              'application/x-suggestions+json': {
                method: 'GET',
                template: testServer.getBaseUrl('search?q={searchTerms}'),
                params: []
              }
            }
          }
        ]);

        await newTab(freshtabUrl, { focus: true });
        await blurUrlBar();
        await mockSearch({ results: [{ url }] });
        withHistory([]);
        fillIn(query);
        await waitForPopup(1, 1000);
      });

      afterEach(function () {
        unmockGetSearchEngines();
      });

      it('on Enter renders last query box with the query', async function () {
        press({ key: 'Enter' });
        await waitFor(async () => {
          const lastQueryBox = await urlbar.lastQuery;
          expect(lastQueryBox).to.have.property('visible').that.equal(true);
          return expect(lastQueryBox).to.have.property('text').that.equal(query);
        }, 1000);
      });
    });

    context('for backend result without autocomplete', function () {
      const url = testServer.getBaseUrl('url-test');
      const query = 'test';

      beforeEach(async function () {
        await testServer.registerPathHandler('/url-test', { result: '<html><body><p>success</p></body></html>' });

        await newTab(freshtabUrl, { check: true, focus: true });

        await blurUrlBar();
        await mockSearch({ results: [{ url }] });
        withHistory([]);
        fillIn(query);
        await waitForPopup(1, 2000);
      });

      it('on Enter renders last query box with the query', async function () {
        press({ key: 'ArrowDown' });
        await waitFor(async () => {
          const $result = await $cliqzResults.querySelector(`a.result[data-url="${url}"]`);
          return $result.classList.contains('selected');
        });
        press({ key: 'Enter' });
        await waitFor(async () => {
          const lastQueryBox = await urlbar.lastQuery;
          expect(lastQueryBox).to.have.property('visible').that.equal(true);
          return expect(lastQueryBox).to.have.property('text').that.equal(query);
        }, 1000);
      });
    });

    context('for backend result with autocomplete + navigation', function () {
      const url1 = 'https://test_url1.com';
      const url2 = testServer.getBaseUrl('url-test');
      const query = 'local';

      beforeEach(async function () {
        await testServer.registerPathHandler('/url-test', { result: '<html><body><p>success</p></body></html>' });

        await newTab(freshtabUrl, { focus: true });

        await blurUrlBar();
        await mockSearch({
          results: [
            { url: url1 },
            { url: url2 }
          ]
        });
        withHistory([]);
        fillIn(query);
        await waitForPopup(2, 2000);
      });

      it('on Enter renders last query box with the query', async function () {
        press({ key: 'ArrowDown' });
        await waitFor(async () => {
          const $result1 = await $cliqzResults.querySelector(`a.result[data-url="${url1}"]`);
          return $result1.classList.contains('selected');
        });
        press({ key: 'ArrowDown' });
        await waitFor(async () => {
          const $result2 = await $cliqzResults.querySelector(`a.result[data-url="${url2}"]`);
          return $result2.classList.contains('selected');
        });
        press({ key: 'Enter' });
        await waitFor(async () => {
          const lastQueryBox = await urlbar.lastQuery;
          expect(lastQueryBox).to.have.property('visible').that.equal(true);
          return expect(lastQueryBox).to.have.property('text').that.equal(query);
        }, 1000);
      });
    });

    context('for backend result with autocomplete', function () {
      const url = testServer.getBaseUrl('url-test');
      const query = 'local';

      beforeEach(async function () {
        await testServer.registerPathHandler('/url-test', { result: '<html><body><p>success</p></body></html>' });
        await newTab(freshtabUrl, { focus: true });
        await blurUrlBar();
        await mockSearch({ results: [{ url }] });
        withHistory([]);
        fillIn(query);
        await waitForPopup(1, 1000);
      });

      it('on Enter doesn\'t render last query box', async function () {
        press({ key: 'Enter' });
        await sleep(1000);
        const lastQueryBox = await urlbar.lastQuery;
        expect(lastQueryBox).to.have.property('visible').that.equal(false);
        expect(lastQueryBox).to.have.property('text').that.equal(null);
      });
    });
  });
}
