import Ember from 'ember';
import { purecloudEnvironmentTld } from '../utils/purecloud-environment';
import platformClient from 'platformClient';

export default Ember.Route.extend({
	purecloud: Ember.inject.service(),
	analyticsService: Ember.inject.service(),

	init() {
		let that = this;
		function receiveMessage(event) {
			if (event.origin !== 'null' && event.origin !== window.location.origin) {
				//return;
			}

			// Note: Initializing a V2 chat triggers an event without a data property
			if (!event.data || typeof event.data === 'object') {
				return;
			}
			let data = JSON.parse(event.data);

			if (data.action === 'anaytics') {
				that.get('analyticsService').logExporerExecution(data.httpMethod, data.url);
			}
		}
		window.addEventListener('message', receiveMessage, false);
	},

	model() {
		let search = window.location.search;

		if (window.location.hash.indexOf('share') > 0) {
			search = '?' + window.location.hash.substring(window.location.hash.indexOf('share'));
		} else if (window.location.hash.indexOf('filter') > 0) {
			search = '?' + window.location.hash.substring(window.location.hash.indexOf('filter'));
		}

		let purecloudEnvironment = purecloudEnvironmentTld();
		const match = /^developer\.(.+)$/i.exec(window.location.hostname);
		let siteHost = match ? match[1] : purecloudEnvironment;

		let swaggerUrl = `https://api.${purecloudEnvironment}/api/v2/docs/swagger`;
		// swaggerUrl = '/swagger-schema/publicapi-v2-latest.json';

		let swagger =
			`openApiUrl=${swaggerUrl}&host=api.${purecloudEnvironment}&shareUrl=${window.location.origin}` +
			encodeURIComponent('/developer-tools/#/api-explorer?');

		if (search == null || search.length === 0) {
			search = '?' + swagger;
		} else {
			search += '&' + swagger;
		}

		let openApiExplorerUrl = `https://developer.${siteHost}/openapi-explorer/index.html`;

		if (siteHost === 'aps1.pure.cloud') {
			//Mumbai region redirect to useast-1
			openApiExplorerUrl = `https://developer.mypurecloud.com/openapi-explorer/index.html`;
		}

		if (siteHost === 'ininsca.com') {
			//need to special case here
			openApiExplorerUrl = `https://apps.${siteHost}/openapi-explorer/`;
		}

		//openApiExplorerUrl = 'http://localhost:8081/';

		return `${openApiExplorerUrl}${search}#token_type=bearer&access_token=` + platformClient.ApiClient.instance.authData.accessToken;
	},
});

@@ -1309,30 +1309,48 @@
 %5D : 
-purecloudEnvironment;%0A
+'genesys.cloud';%0A%0A%09%09// Build swagger url
 %0A%09%09l
@@ -1428,69 +1428,325 @@
 r%60;%0A
+%0A
 %09%09// 
-swaggerUrl = '/swagger-schema/publicapi-v2-latest.json';
+Determine override%0A%09%09var query = window.location.search.substring(1);%0A%09%09var vars = query.split('&');%0A%09%09for (var i = 0; i %3C vars.length; i++) %7B%0A%09%09%09var pair = vars%5Bi%5D.split('=');%0A%09%09%09if (decodeURIComponent(pair%5B0%5D %7C%7C '').toLowerCase() === 'swaggeroverride') %7B%0A%09%09%09%09swaggerUrl = decodeURIComponent(pair%5B1%5D);%0A%09%09%09%7D%0A%09%09%7D
 %0A%0A%09%09
@@ -2130,182 +2130,8 @@
 %60;%0A%0A
-%09%09if (siteHost === 'aps1.pure.cloud') %7B%0A%09%09%09//Mumbai region redirect to useast-1%0A%09%09%09openApiExplorerUrl = %60https://developer.mypurecloud.com/openapi-explorer/index.html%60;%0A%09%09%7D%0A%0A
 %09%09if
@@ -2272,60 +2272,8 @@
 %09%7D%0A%0A
-%09%09//openApiExplorerUrl = 'http://localhost:8081/';%0A%0A
 %09%09re

allow swagger url override