import './jstree';
import { store } from './statsStore';
import jQuery from 'jquery';
import types from './types.json';
window.$ = window.jQuery = jQuery;
import { jstreeSelected } from './reducers/jstree.js';

$(function () {
  $('#close_jstree').on('click', function () {
    // $('#jstree_demo').jstree('clear_state');
    $.jstree.reference('#jstree_demo').jstree('close_all');
  });
});

const globalHelpID = '3.0';
var path = '/ibad/export/';
$('#jstree_demo')
  .jstree({
    core: {
      animation: 0,
      // "check_callback" : true,
      data: {
        url: function (node) {
          return node.id === '#'
            ? '/TreeRoot'
            : '/TreeNode/' + node.type + node.id;
        },
        dataType: 'json',
      },
      worker: false,
    },
    types: types,
    contextmenu: {
      items: customMenu,
    },
    search: {
      case_sensitive: false,
      show_only_matches: true,
      show_only_matches_children: true,
      ajax: {
        url: '/search',
      },
    },
    plugins: ['state', 'types', 'wholerow', 'json_data', 'massload', 'search'],
  })
  .on('select_node.jstree', function (e, data) {
    var resourceType = data.node.original.type.substring(0, 3);
    const resourceId = data.node.original.id.substr(3);
    if (
      resourceType == 'pdf' ||
      resourceType == 'xls' ||
      resourceType == 'htm' ||
      resourceType == 'mob'
    ) {
      resourceType = 'REP';
    }

    store.dispatch(jstreeSelected(resourceId, resourceType));
  })
  .on('show_contextmenu.jstree', function (e, data) {
    // Liegt sonst hinter den Bootstrp componennten
    $('.jstree-contextmenu').css('z-index', '2000');
  })
  .on('hover_node.jstree', function (e, data) {
    // $("#tooltip").text(data.node.text);
    // $("#tooltip").show();
  })
  .on('dehover_node.jstree', function (e, data) {
    $('#tooltip').hide();
  })
  .on('search.jstree', function () {
    $('#jstree-stats-searchicon').show();
    $('#jstree-stats-searchingicon').hide();
  });

const jstreeElement = $('#jstree_demo');
$('#jstree-stats-search').change(() => {
  const searchString = $('#jstree-stats-search').val().toString().toLowerCase();
  if (searchString.length === 0) {
    jstreeElement.jstree(true).clear_search();
  }
  let to = 0;
  if (to) {
    clearTimeout(to);
  }
  to = setTimeout(() => {
    if (searchString.length < 3) {
      return;
    }

    $('#jstree-stats-searchicon').hide();
    $('#jstree-stats-searchingicon').show();
    jstreeElement.jstree(true).search(searchString);
  }, 250);
});

function customMenu(node) {
  var items = {
    MyItem: {
      label: 'MWT',
      action: function () {},
      submenu: {
        SubItem: {
          label: 'Neu',
          action: function () {},
        },
        SubItem2: {
          label: 'Extra',
          action: function () {},
        },
      },
    },
    item2: {
      label: 'VBG',
      action: function () {
        /* action */
      },
    },
    type: {
      label: node.id,
      icon: 'glyphicon glyphicon-file',
      action: function () {
        alert('event gedrückt');
      },
      submenu: {
        SubItem: {
          label: 'Edit',
          action: function () {},
        },
      },
    },
  };

  if (node.type === 'MWT') {
    delete items.item2;
  } else if (node.type === 'VBG') {
    delete items.item1;
  }

  return items;
}
