/*
 * Inspinia js helpers:
 *
 * correctHeight() - fix the height of main wrapper
 * detectBody() - detect windows size
 * smoothlyMenu() - add smooth fade in/out on navigation show/ide
 *
 */

// import * as jQuery_ from 'jquery';
// const jQuery = jQuery_;
declare var jQuery: any;

export function correctHeight() {

  const pageWrapper = jQuery('#page-wrapper');
  const navbarHeight = jQuery('nav.navbar-default').height();
  const wrapperHeight = pageWrapper.height();

  if (navbarHeight > wrapperHeight) {
    pageWrapper.css('min-height', navbarHeight + 'px');
  }

  if (navbarHeight <= wrapperHeight) {
    if (navbarHeight < jQuery(window).height()) {
      pageWrapper.css('min-height', jQuery(window).height() + 'px');
    } else {
      pageWrapper.css('min-height', navbarHeight + 'px');
    }
  }

  if (jQuery('body').hasClass('fixed-nav')) {
    if (navbarHeight > wrapperHeight) {
      pageWrapper.css('min-height', navbarHeight + 'px');
    } else {
      pageWrapper.css('min-height', jQuery(window).height() - 60 + 'px');
    }
  }
}

export function detectBody() {
  if (jQuery(document).width() < 769) {
    jQuery('body').addClass('body-small');
  } else {
    jQuery('body').removeClass('body-small');
  }
}

export function smoothlyMenu() {
  if (!jQuery('body').hasClass('mini-navbar') || jQuery('body').hasClass('body-small')) {
    // Hide menu in order to smoothly turn on when maximize menu
    jQuery('#side-menu').hide();
    // For smoothly turn on menu
    setTimeout(
      () => {
        jQuery('#side-menu').fadeIn(400);
      }, 200);
  } else if (jQuery('body').hasClass('fixed-sidebar')) {
    jQuery('#side-menu').hide();
    setTimeout(
      () => {
        jQuery('#side-menu').fadeIn(400);
      }, 100);
  } else {
    // Remove all inline style from jquery fadeIn function to reset menu state
    jQuery('#side-menu').removeAttr('style');
  }
}

export function fixHeight() {
  const heightWithoutNavbar = jQuery('body > #wrapper').height() - 62;

  jQuery('.sidebar-panel').css('min-height', heightWithoutNavbar + 'px');

  const navbarheight = jQuery('nav.navbar-default').height();
  const wrapperHeight = jQuery('#page-wrapper').height();

  if (navbarheight > wrapperHeight) {
    jQuery('#page-wrapper').css('min-height', navbarheight + 'px');
  }

  if (navbarheight < wrapperHeight) {
    jQuery('#page-wrapper').css('min-height', jQuery(window).height() + 'px');
  }

  if (jQuery('body').hasClass('fixed-nav')) {
    if (navbarheight > wrapperHeight) {
      jQuery('#page-wrapper').css('min-height', navbarheight + 'px');
    } else {
      jQuery('#page-wrapper').css('min-height', jQuery(window).height() - 60 + 'px');
    }
  }
}
