'use strict';

/*
 * *******************************************************************************
 *  * Copyright (c) 2018 Edgeworx, Inc.
 *  *
 *  * This program and the accompanying materials are made available under the
 *  * terms of the Eclipse Public License v. 2.0 which is available at
 *  * http://www.eclipse.org/legal/epl-2.0
 *  *
 *  * SPDX-License-Identifier: EPL-2.0
 *  *******************************************************************************
 *
 */

$(document).ready(function () {

  var accTok = apwGetAccessToken();

  getFogInstancesListForUser(accTok);

  getTrackListForUser(accTok).then(function (result) {
    getElementCatalog(accTok).then(function (catalog) {
      $('.modal-trigger').leanModal();
      $('#element-instance-properties-config').trigger('autoresize');

      //Initialize the select boxes for element routing
      AppWorks_Start();
      activateHandlersForBuildInterface(jsPlumbInstance);

      //Activate dynamically generated handlers for non-optimal stuff
      $('.build-element-container').each(function (i, div) {
        var id = $(this).attr('data-element-type-id');
        var last = $('#element-type-hover-icon-' + id).attr('last');
        $(document).apwHoverPopUp({
          'hoverablesSelector': '#element-type-hover-icon-' + id,
          'width': 300,
          'height': 300,
          'offsetLeft': 60,
          'offsetTop': last == 0 ? -124 : -225,
          'border': 'N',
          'borderColor': '#ccc',
          'solidBackground': 'Y',
          'backgroundColor': '#eceff1',
          'fillFromID': 'element-type-description-' + id,
          'hoverPosition': 'right'
        });
      });

      $('#track-select-dropdown').change(function () {
        tracksDropDownChanged($('#track-select-dropdown').val());
      });
      $('#element-instance-properties-fog-instance').change(function () {
        fogInstanceDropDownClickHandler($('#element-instance-properties-fog-instance').val());
      });

      refreshSelectDropDowns();

      jsPlumb.ready(function () {
        jsPlumb.setContainer('build-canvas');

        jsPlumb.bind('connection', connectionMade);
        jsPlumb.bind('connectionDetached', connectionDetached);
        jsPlumb.bind('click', function (connection, originalEvent) {
          jsPlumb.detach(connection);
        });

        setSelectedTrackProperties();
        getElementInstances(selectedTrackId);
      });
    });
  });
});

var getElementCatalog = function getElementCatalog(accTok) {
  return $.ajax({
    url: fogControllerUrl + ioAuthoring.urls.getElementCatalog + '?t=' + accTok,
    dataType: 'json',
    success: function success(res) {
      if (res.status == 'ok') {
        $('#catalogElementContainer').html(ejs.render(ioAuthoring.templates.catalogElement, { 'catalogElements': res.elementCatalog }, {}));
      }
    }
  });
};

var getFogInstancesListForUser = function getFogInstancesListForUser(userId) {
  $.ajax({
    url: fogControllerUrl + ioAuthoring.urls.getFogInstancesForUser + userId,
    dataType: 'json',
    success: function success(res) {
      if (res.status == 'ok') {
        $('#element-instance-properties-fog-instance').append(ejs.render(ioAuthoring.templates.fogInstances, { 'fogInstances': res.instances }, {}));
      }
    }
  });
};

var getTrackListForUser = function getTrackListForUser(userId) {
  return $.ajax({
    url: fogControllerUrl + ioAuthoring.urls.getTrackListForUser + userId,
    dataType: 'json',
    success: function success(res) {
      if (res.status == 'ok') {
        $('#track-select-dropdown').html(ejs.render(ioAuthoring.templates.dataTracks, { 'dataTracks': res.dataTracks }, {}));
      }
      refreshSelectDropDowns();
    }
  });
};