'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
 *  *******************************************************************************
 *
 */

var IOFABRIC_VERSION = '0.50';
$(document).ready(function () {

  var accTok = apwGetAccessToken();

  getFogTypesList(accTok).then(function (result) {
    AppWorks_Start();

    $('.modal-trigger').leanModal();
    $('.tooltipped').tooltip({ delay: 50 });
    $('#proxy-close-button').hide();
    $('#accordion-fog-info').on('shown.bs.collapse', function (e) {
      var id = $(e.target).parent().find("[id]")[0].id;
      navigateToElement(id);
    });

    loadInstances().then(function (res) {
      setupChangeMonitors();
      activateHandlersForFogInterface();

      $('#fog-instance-provision-button').click(function () {
        if (selectedInstance != '') {
          getProvisioningKey(selectedInstance);
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#delete-confirm-modal-yes-button').click(function () {
        if (selectedInstance != '') {
          $('#deleteconfirmmodal').closeModal();
          var id = $('.fog-instance-selected').attr('data-fog-instance-id');
          deleteFogInstance(id);
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#proxy-modal-open-button').click(function () {
        if (selectedInstance != '') {
          $('#opensshtunnelmodal').closeModal();
          var id = $('.fog-instance-selected').attr('data-fog-instance-id');
          openSshTunnel(id);
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#proxy-close-button').click(function () {
        if (selectedInstance != '') {
          var id = $('.fog-instance-selected').attr('data-fog-instance-id');
          closeSshTunnel(id);
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#reboot-confirm-modal-yes-button').click(function () {
        if (selectedInstance != '') {
          $('#rebootmodal').closeModal();
          $(".fog-instance-container.fog-instance-selected").attr('fog-instance-reboot', true);
          updateInstanceSetting();
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#fog-instance-delete-button').click(function () {
        //showDeleteConfirmModal();
      });

      $('#version-upgrade-modal-yes-button').click(function () {
        if (selectedInstance != '') {
          $('#versionupgrademodal').closeModal();
          var id = $('.fog-instance-selected').attr('data-fog-instance-id');
          changeIoFogVersion(id, 'upgrade');
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });

      $('#version-rollback-modal-yes-button').click(function () {
        if (selectedInstance != '') {
          $('#versionupgrademodal').closeModal();
          var id = $('.fog-instance-selected').attr('data-fog-instance-id');
          changeIoFogVersion(id, 'rollback');
        } else {
          Materialize.toast('No Fog instance is selected.', 5000);
        }
      });
    });
  });
});

var navigateToElement = function navigateToElement(id) {
  $('.fog-properties').animate({
    scrollTop: $('.fog-properties').scrollTop() + $("#" + id).offset().top - $(".header-bar").height()
  }, 400);
};

var getFogTypesList = function getFogTypesList(accTok) {
  return $.ajax({
    url: fogControllerUrl + ioAuthoring.urls.getFogTypesList + '?t=' + accTok,
    dataType: "json",
    success: function success(res) {
      if (res.status == 'ok') {
        $("#fogTypeContainer").html(ejs.render(ioAuthoring.templates.fogTypeList, { "fogTypes": res.fogTypes }, {}));
      }
    }
  });
};