/**
 * Copyright 2019 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import {loadScript, validateData} from '../3p/3p';
import {tryParseJson} from '../src/json';

/**
 * @param {!Window} global
 * @param {!Object} data
 */
export function blade(global, data) {
  // ensure mandatory fields
  validateData(data, [
    'width',
    'height',
    'blade_api_key',
    'blade_player_id',
    'blade_player_type',
  ]);

  const marcosObj = tryParseJson(data['blade_macros']) || {};
  marcosObj['rand'] = Math.random().toString();
  marcosObj['page_url'] = marcosObj['page_url'] || global.context.canonicalUrl;
  const macros = Object.assign({}, marcosObj);
  macros.width = data.width;
  macros.height = data.height;

  const containerId = `player-${data['blade_api_key']}-${
    data['blade_player_id']
  }`;
  createContainer(containerId);

  global[`_bladeConfig-${containerId}`] = {
    playerId: data['blade_player_id'],
    apiKey: data['blade_api_key'],
    version: '1.0',
    macros,
  };
  const ctx = global.context;

  global[`_bladeOnLoad-${containerId}`] = function(error, player) {
    if (error) {
      global.context.noContentAvailable();
      return;
    }
    ctx.reportRenderedEntityIdentifier(containerId);
    ctx.renderStart({
      width: player.width,
      height: player.height,
    });
  };

  const servingDomain = data.servingDomain
    ? encodeURIComponent(data.servingDomain)
    : 'ssr.streamrail.net';

  loadScript(
    global,
    `https://${servingDomain}/js/${data['blade_api_key']}/${
      data['blade_player_id']
    }/player.js?t=${
      data['blade_player_type']
    }&callback=_bladeOnLoad&config=_bladeConfig&c=${containerId}`,
    undefined,
    () => {
      global.context.noContentAvailable();
    }
  );
}

/**
 * @param {string} elemId
 */
function createContainer(elemId) {
  const d = global.document.createElement('div');
  d.id = elemId;
  d.classList.add('blade');
  global.document.getElementById('c').appendChild(d);
}

@@ -1390,31 +1390,44 @@
 inerId);%0A%0A  
-global%5B
+const bladeConfig = 
 %60_bladeConfi
@@ -1439,24 +1439,46 @@
 ontainerId%7D%60
+;%0A  global%5BbladeConfig
 %5D = %7B%0A    pl
@@ -1615,23 +1615,36 @@
 ext;%0A%0A  
-global%5B
+const bladeOnLoad = 
 %60_bladeO
@@ -1664,16 +1664,38 @@
 inerId%7D%60
+;%0A  global%5BbladeOnLoad
 %5D = func
@@ -2243,17 +2243,18 @@
 allback=
-_
+$%7B
 bladeOnL
@@ -2260,16 +2260,17 @@
 Load
+%7D
 &config=
 _bla
@@ -2265,17 +2265,18 @@
 &config=
-_
+$%7B
 bladeCon
@@ -2278,16 +2278,17 @@
 deConfig
+%7D
 &c=$%7Bcon

fix bladeOnLoad callback (#24683)