// // ---------------------------------------------------------------------------//
// ---------------------------------  INPUT VALIDACION -----------------------//
// ---------------------------------------------------------------------------//

//const TOKEN_API_KEY = "G6zZC2bo9dZ3gw23B1oIjZdDBAVnLbxa8afLRagp";

const icon = {
  eye: '<i style="font-size:22px;" class="fa fa-eye fa-lg"></i>',
  'trash-o': `<i style="font-size:22px;" class="fa fa-trash-o fa-lg"></i>`,
  printer: `<i style="font-size:22px;" class="fa fa-print fa-lg"></i>`,
  file: `<i style="font-size:22px;" class="fa fa-file-text-o fa-lg"></i>`
}

const printLeyend = obj => {
  let el = obj.el
  let data = obj.data
  let actions = ''
  for (let i in data) {
    actions += `
              <li class="media">
                <div class="media-left">
                  <a href="#" class="btn border-${data[i]["color"]} text-${data[i]["color"]} btn-flat btn-icon btn-rounded btn-sm">
                    <i style="font-size:22px;" class="fa fa-${data[i]["icon"]}"></i>
                  </a>
                </div>
                <div class="media-body">
                  <span class="text-semibold">${data[i]["title"]}</span>
                  <div class="media-annotation">${data[i]["description"]}</div>
                </div>
              </li>`
  }
  document.querySelector(el).innerHTML = actions
}

function soloNumeros(input) {
  $('#' + input).on('keypress keyup blur', function (event) {
    $(this).val($(this).val().replace(/[^\d].+/, ""));
    if ((event.which < 48 || event.which > 57)) {
      event.preventDefault();
    }
  });
}

function soloLetras(input) {
  $('#' + input).on('keypress keyup blur', function (e) {
    var regex = new RegExp(/^[ña-zÑA-Z\s]*$/);
    var str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
      return true;
    }
    e.preventDefault();
    return false;
  });
}

function soloAlfanumerico(input) {
  $('#' + input).on('keypress keyup blur', function (e) {
    let regex = new RegExp("^[a-zA-Z0-9]+$");
    let str = String.fromCharCode(!e.charCode ? e.which : e.charCode);
    if (regex.test(str)) {
      return true;
    }
    e.preventDefault();
    return false;
  });
}

// ---------------------------------------------------------------------------//
// --------------------------------- /INPUT VALIDACION -----------------------//
// ---------------------------------------------------------------------------//

function popup(obj) {
  bootbox.dialog({
    title: obj.title,
    size: obj.size,
    message: obj.mensaje,
    className: obj.className,
    buttons: {
      confirm: {
        label: 'Yes',
        className: 'btn-success'
      },
      cancel: {
        label: 'No',
        className: 'btn-danger'
      }
    }
  });
}

// ---------------------------------------------------------------------------//
// --------------------------------- EFECTO CARGAR ---------------------------//
// ---------------------------------------------------------------------------//

// cierra el efecto cargar
function unload() {
  $.unblockUI();
}

// efecto cargar
function load(msg) {
  $.blockUI({
    message: '<h1><span class="text-semibold"><i class="fa fa-spinner fa-lg spinner position-left"></i>&nbsp; ' + msg + '...</span></h1>',
    timeout: 20000 * 100, //unblock after 2 seconds
    overlayCSS: {
      backgroundColor: '#1b2024',
      opacity: 0.9,
      zIndex: 1200,
      cursor: 'wait'
    },
    css: {
      border: 0,
      color: '#fff',
      padding: 0,
      zIndex: 1201,
      backgroundColor: 'transparent'
    }
  });
}

// ---------------------------------------------------------------------------//
// --------------------------------- /EFECTO CARGAR --------------------------//
// ---------------------------------------------------------------------------//

// ----------------MENSAJES DE CONFIRMACION
function successMessage(msg, callback) {
  $.confirm({
    icon: 'fa fa-check fa-lg',
    title: 'Registro correcto!',
    content: `<b>${msg}</b>`,
    type: 'green',
    scrollToPreviousElement: false,
    scrollToPreviousElementAnimate: false,
    buttons: {
      Aceptar: {
        text: 'Aceptar',
        btnClass: 'btn-green',
        action: function () {
          callback;
        }
      }
    }
  });
}

function errorMessage(msg, callback) {
  $.confirm({
    icon: "fa fa-warning fa-lg",
    theme: 'material',
    animation: 'scale',
    title: '¡Error!',
    content: `<b>${msg}</b>`,
    type: 'red',
    draggable: false,
    backgroundDismissAnimation: 'glow',
    scrollToPreviousElement: false,
    scrollToPreviousElementAnimate: false,
    typeAnimated: true,
    buttons: {
      Cerrar: {
        text: 'Cerrar',
        btnClass: 'btn-red',
        action: function () {
          callback;
        }
      }
    }
  });
}

function warningMessage(msg, callback) {
  $.confirm({
    icon: "fa fa-warning fa-lg",
    theme: 'material',
    animation: 'scale',
    title: '¡Alerta!',
    content: `<b>${msg}</b>`,
    type: 'orange',
    draggable: false,
    backgroundDismissAnimation: 'glow',
    scrollToPreviousElement: false,
    scrollToPreviousElementAnimate: false,
    typeAnimated: true,
    buttons: {
      Cerrar: {
        text: 'Cerrar',
        btnClass: 'btn-orange',
        action: function () {
          callback;
        }
      }
    }
  });
}
// ----------------MENSAJES DE CONFIRMACION


// ----------------MENSAJES DE CONFIRMACION (PROMISE)

const getSelectedText = node => document.querySelector(node).selectedIndex === -1 ? null : document.querySelector(node).options[document.querySelector(node).selectedIndex].text

//configuracion por defecto datatable 
function defaultConfigDataTable() {
  $.extend($.fn.dataTable.defaults, {
    autoWidth: false,
    searching: false,
    lengthChange: false,
    responsive: false,
    bSort: false,
    dom: '<"datatable-header"fl><"datatable-scroll"t><"datatable-footer"ip>',
    language: {
      "paginate": {'first': 'First', 'last': 'Last', 'next': '&rarr;', 'previous': '&larr;'},
      "sProcessing": "Procesando...",
      "sLengthMenu": "Mostrar _MENU_ registros",
      "sZeroRecords": "No se encontraron resultados",
      "sEmptyTable": "Ningún dato disponible en esta tabla",
      "sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_ registros",
      "sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0 registros",
      "sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
      "sInfoPostFix": "",
      "sSearch": "Buscar:",
      "sUrl": "",
      "sInfoThousands": ",",
      "sLoadingRecords": "Cargando...",
      "oPaginate": {
        "sFirst": "Primero",
        "sLast": "Último",
        "sNext": "Siguiente",
        "sPrevious": "Anterior"
      },
      "oAria": {
        "sSortAscending": ": Activar para ordenar la columna de manera ascendente",
        "sSortDescending": ": Activar para ordenar la columna de manera descendente"
      }
    },
    drawCallback: function () {
      $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').addClass('dropup');
    },
    preDrawCallback: function () {
      $(this).find('tbody tr').slice(-3).find('.dropdown, .btn-group').removeClass('dropup');
    },
    rowCallback: function (row, data, index) {
      $(row).children().each(function (i, v) {
        $(v).addClass('text-semibold')
      })
      if ($(row).hasClass('odd')) {
//        $(row).css('background-color', '#e8f4f4');
        $(row).css('background-color', '#4592b11c');
      }
    }
  });
}

// smoothScrolling
function smoothScrolling(nodeIdentifier) {
  var scroll = new SmoothScroll();
  var anchor = document.querySelector(nodeIdentifier);
  scroll.animateScroll(anchor);
}



// eliminar un elemento del array por llave
function removeByKey(array, params) {
  array.some(function (item, index) {
    return (array[index][params.key] === params.value) ? !!(array.splice(index, 1)) : false;
  });
  return array;
}


// validar existencia del valor en un array
function validarExistenciaValorDeArray(value, array, x_value) {
  for (let i = 0; i < array.length; i++) {
    if (value === array[i][x_value]) {
      return true;
    }
  }
  return false;
}

// generar identificador unico
function guidGenerator() {
  let S4 = function () {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  };
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

// FUNCION FECHA QUE FORMATEA LA FECHA
function toDate(dateStr) {
  var parts = dateStr.split("/");
  return new Date(parts[2], parts[1] - 1, parts[0]);
}

function getActualDate() {
  return new Date();
}

// FUNCION PARA OBTENER LA FECHA ACTUAL
//is valid date format
function calculateAge(dateOfBirth, dateToCalculate) {
  let calculateYear = dateToCalculate.getFullYear();
  let calculateMonth = dateToCalculate.getMonth();
  let calculateDay = dateToCalculate.getDate();

  let birthYear = dateOfBirth.getFullYear();
  let birthMonth = dateOfBirth.getMonth();
  let birthDay = dateOfBirth.getDate();

  let age = calculateYear - birthYear;
  let ageMonth = calculateMonth - birthMonth;
  let ageDay = calculateDay - birthDay;

  if (ageMonth < 0 || (ageMonth === 0 && ageDay < 0)) {
    age = parseInt(age) - 1;
  }
  return age;
}

let getLocal = (keyName) => JSON.parse(window.localStorage.getItem(keyName));
let setLocal = (keyName, obj) => window.localStorage.setItem(keyName, JSON.stringify(obj));
let removeLocal = (keyName) => window.localStorage.removeItem(keyName);
let clearLocal = () => localStorage.clear();

const jAlert = (obj) => {
  return new Promise((resolve) => {
    obj = obj || {
      title: 'Titulo por defecto',
      content: 'Mensaje por defecto',
      buttons: {
        left: 'Left',
        right: 'Right'
      }
    }

    let title = ``
    let icon = ``
    let type = ``
    let buttonLeft = ``
    let buttonRight = ``

    switch (obj.type) {
      case 'confirm':
        title = `Confirmación`
        icon = `fa fa-question fa-lg`
        type = `blue`
        buttonLeft = `bg-primary`
        buttonRight = `bg-danger`
        break
      case 'success':
        title = `¡Muy bien!`
        icon = `fa fa-check fa-lg`
        type = `green`
        buttonLeft = `bg-success`
        break
      case 'warning':
        title = `¡Alerta!`
        icon = `fa fa-exclamation fa-lg`
        type = `orange`
        buttonLeft = `bg-yellow`
        break
      case 'error':
        title = `¡Error!`
        icon = `fa fa-close fa-lg`
        type = `red`
        buttonLeft = `bg-danger`
        break
      default:
        title = `¡Prueba!`
        icon = `fa fa-leaf fa-lg`
        type = `green`
        buttonLeft = `bg-success`
        buttonRight = `bg-slate-300`

    }

    let buttons = {}

    if (Object.keys(obj.buttons).length > 0 && Object.keys(obj.buttons).length === 1) {
      buttons = {
        si: {
          btnClass: buttonLeft,
          text: `<span class="text-semibold"><i class="fa fa-check"></i> ${obj.buttons.left}</span>`,
          action: function () {
            resolve(true)
          }
        }
      };
    } else {
      buttons = {
        si: {
          btnClass: buttonLeft,
          text: `<span class="text-semibold"><i class="fa fa-check"></i> ${obj.buttons.left}</span>`,
          action: function () {
            resolve(true)
          }
        },
        no: {
          btnClass: buttonRight,
          text: `<span class="text-semibold"><i class="fa fa-close"></i> ${obj.buttons.right}</span>`,
          action: function () {
            resolve(false)
          }
        }
      };
    }

    obj.content = obj.content || `Mensaje por defecto`
    obj.title = obj.title || `Titulo por defecto`

    $.confirm({
      theme: 'material',
      closeIcon: false,
      animation: 'scale',
      draggable: false,
      scrollToPreviousElement: false,
      scrollToPreviousElementAnimate: false,

      icon: icon,
      type: type,
      title: title,
      content: `<span class="text-semibold text-size-large">${obj.content}</span>`,
      buttons: buttons
    })
  })
}


(() => {
//  console.log('%c(╯°□°)╯︵ ┻━┻!', 'color: red; font-size: 100px; font-weight: bold;');
//  console.log('%cTodos los cambios son registrados en nuestra base de datos.', 'background: #222; font-size: 25px; color: #bada55');
//  console.log('%cobservando...', 'background: #222; font-size: 15px; color: #bada55');
//  console.log('%cMejor cierra la consola prro o valdrás ( ͡° ͜ʖ ͡°) ...', 'background: #222; font-size: 18px; color: #bada55;font-weight: bold;');
//
//  Object.getOwnPropertyNames(console).filter(function (property) {
//    return typeof console[property] === 'function';
//  }).forEach(function (verb) {
//    console[verb] = function () {
//      return 'Te estoy mirando!';
//    };
//  });
})();

function isEmpty(obj) {

  // null and undefined are "empty"
  if (obj == null)
    return true;

  // Assume if it has a length property with a non-zero value
  // that that property is correct.
  if (obj.length > 0)
    return false;
  if (obj.length === 0)
    return true;

  // If it isn't an object at this point
  // it is empty, but it can't be anything *but* empty
  // Is it empty?  Depends on your application.
  if (typeof obj !== "object")
    return true;

  // Otherwise, does it have any properties of its own?
  // Note that this doesn't handle
  // toString and valueOf enumeration bugs in IE < 9
  for (var key in obj) {
    if (hasOwnProperty.call(obj, key))
      return false;
  }

  return true;
}