import React,{useState} from 'react'

const tarifaBase = 3600
const costo78metros = 120
const costoPorMinuto = 220

export default function App() {
  
//Calcular distancia recorrida en linea recta
function getKilometros(lat1,lon1,lat2,lon2){
  var rad = function(x) {return x*Math.PI/180;}
  var R = 6378.137; //Radio de la tierra en km
   var dLat = rad( lat2 - lat1 );
   var dLong = rad( lon2 - lon1 );
  var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(rad(lat1)) * Math.cos(rad(lat2)) * Math.sin(dLong/2) * Math.sin(dLong/2);
   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
   var d = R * c;
  return d.toFixed(2); //Retorna tres decimales
}

function calcularRecorrido(){
  // Datos originales
const coords = [
  {"laitude":6.241366,"lon":-75.587633},
  {"laitude":6.241679, "lon":-75.587293},
  {"laitude":6.241805, "lon":-75.587280}, 
  {"laitude":6.242359, "lon":-75.586735},
  {"laitude":6.242960, "lon":-75.586136},
  {"laitude":6.243147, "lon":-75.586107},
  {"laitude":6.242689, "lon":-75.586569},
  {"laitude":6.242205, "lon":-75.587042},
  {"laitude":6.241540, "lon":-75.587691},
  {"laitude":6.240761, "lon":-75.588484},
  {"laitude":6.239744, "lon":-75.589498},
  {"laitude":6.238655, "lon":-75.590560},
  {"laitude":6.237480, "lon":-75.591699},
  {"laitude":6.236257, "lon":-75.593010},
  {"laitude":6.233321, "lon":-75.595930},
  {"laitude":6.232852, "lon":-75.596360},
  {"laitude":6.232819, "lon":-75.598165},
  {"laitude":6.232140, "lon":-75.598193},
  {"laitude":6.231987, "lon":-75.601645},
  {"laitude":6.23317, "lon":-75.6063687}
];

var lat = 6.23317; // donde estoy actualmente
var lon = -75.6063687; // donde estoy actualmente
let B = [];
let kilometrajeRecorrido = [];

 for (var i=0; i<coords.length; i++)
 {
   if(parseInt(getKilometros(lat,lon,coords[i].laitude,coords[i].lon))<= 5)
   {
     B.push(coords[i]);
     kilometrajeRecorrido.push(getKilometros(lat,lon,coords[i].laitude,coords[i].lon))
     //alert(getKilometros(lat,lon,coords[i].laitude,coords[i].lon));
   }
 }

// Mostrar resultados
alert(kilometrajeRecorrido[0])
console.log('Resultado km: ', kilometrajeRecorrido);
console.log('Resultado suma: ', B);
}

//Calcular tiempo transcurrido
function calcularTiempo(horaInicio, horaFinalizacion){
  ////////Imprime el tiempo en segundos
   var horaInicial = new Date(horaInicio); // hora inicial
   var horaFinal = new Date(horaFinalizacion);//esta es la hora final
   //La diferencia se da en milisegundos así que debes dividir entre 1000
   var c = ((horaFinal-horaInicial)/1000);
   alert(c)
/////////////////////////////////////////////////////////////
}

  return (
    <div>
      {getKilometros(6.2331443,-75.6044848,6.2410822,-75.5895597)}
      <br></br>
      <button onClick={()=> calcularRecorrido()} >Calcular recorrido</button>
      <button onClick={()=> calcularTiempo("February 12, 2014 03:28:00", "February 12, 2014 03:31:00")} >Calcular tiempo</button>
    </div>
  )
}