//
//  ViewController.swift
//  Trolley App
//
//  Created by Kendrick Morales on 10/21/19.
//  Copyright © 2019 Kendrick Morales. All rights reserved.
//

import UIKit
import CoreLocation
import MapKit
import Alamofire
import SwiftyJSON
class ViewController: UIViewController,  CLLocationManagerDelegate {

    var estado = false
    let locationManager = CLLocationManager()
    
    var locations: CLLocation!
    var timer1 = Timer()

    var parada = ""
    var timer2 = Timer()
 
    @IBOutlet weak var texto: UILabel!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        locationManager.requestAlwaysAuthorization()
        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        }
        

        // Do any additional setup after loading the view.
    }
    
    //funcion para poder extraer latitud y longitud
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate
            else { return }
        print("locations = \(locValue.latitude) \(locValue.longitude)")
    }
    
    @IBOutlet weak var ImageCenter: UIImageView!
    @objc func update(){
      timer1 = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)
        AF.request("http://136.145.231.39/sender_test.php").responseJSON { response in
                        switch response.result {
                            
                        case .success(let value):
                            let json = JSON(value)
        //                    debugPrint(json)
        //                   debugPrint("Response: \(response)")
                            let parada = json["parada"].string
                            let tiempo = json["tiempo"]
                            self.texto.text = "Ultima Actualización: \(tiempo) "
                            if parada == "biblio_Bliblio" {
                                self.ImageCenter.image = UIImage(named: "biblio_Biblio")
                                                       }
                                                       else if parada == "biblio_Centro" {
                                self.ImageCenter.image = UIImage(named: "biblio_Centro")
                                                       }
                                                       else if parada == "biblio_Educacion" {
                                self.ImageCenter.image = UIImage(named: "biblio_Educacion")
                                                       }
                                                       else if parada == "biblio_Facundo" {
                                self.ImageCenter.image = UIImage(named: "biblio_Facundo")
                                                       }
                                                       else if parada == "biblio_MultiP" {
                                self.ImageCenter.image = UIImage(named: "biblio_MultiP")
                                                       }
                                                       else if parada == "biblio_Museo" {
                                self.ImageCenter.image = UIImage(named: "biblio_Museo")
                                                       }
                                                       else if parada == "biblio_Nat1" {
                                self.ImageCenter.image = UIImage(named: "biblio_Nat1")
                                                       }
                                                       else if parada == "biblio_natu2" {
                                self.ImageCenter.image = UIImage(named: "biblio_Nat2")
                                                       }
                                                       else if parada == "centro_Biblio" {
                                self.ImageCenter.image = UIImage(named: "centro_Biblio")
                                                       }
                                                       else if parada == "centro_Centro" {
                                self.ImageCenter.image = UIImage(named: "centro_Biblio")
                                                       }
                                                       else if parada == "centro_Centro" {
                                self.ImageCenter.image = UIImage(named: "centro_Biblio")
                                                       }
                                                       else if parada == "centro_Educacion" {
                                self.ImageCenter.image = UIImage(named: "centro_Educacion")
                                                       }
                                                       else if parada == "centro_FacundoB" {
                                self.ImageCenter.image = UIImage(named: "centro_FacundoB")
                                                       }
                                                       else if parada == "centro_MultiP" {
                                self.ImageCenter.image = UIImage(named: "centro_MultiP")
                                                       }
                                                       else if parada == "centro_Museo" {
                                self.ImageCenter.image = UIImage(named: "centro_Museo")
                                                       }
                                                       else if parada == "centro_Nat1" {
                                self.ImageCenter.image = UIImage(named: "centro_Nat1")
                                                       }
                                                       else if parada == "centro_natu2" {
                                self.ImageCenter.image = UIImage(named: "centro_Nat2")
                                                       }
                                                       else if parada == "complejo_Biblio" {
                                self.ImageCenter.image = UIImage(named: "complejo_Biblio")
                                                       }
                                                       else if parada == "complejo_Centro" {
                                self.ImageCenter.image = UIImage(named: "complejo_Centro")
                                                       }
                                                       else if parada == "complejo_Educacion" {
                                self.ImageCenter.image = UIImage(named: "complejo_Educacion")
                                                       }
                                                       else if parada == "complejo_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "complejo_FacundaB")
                                                       }
                                                       else if parada == "complejo_MultiP" {
                                self.ImageCenter.image = UIImage(named: "complejo_MultiP")
                                                       }
                                                       else if parada == "complejo_Museo" {
                                self.ImageCenter.image = UIImage(named: "complejo_Museo")
                                                       }
                                                       else if parada == "complejo_Nat1" {
                                self.ImageCenter.image = UIImage(named: "complejo_Nat1")
                                                       }
                                                       else if parada == "complejo_natu2" {
                                self.ImageCenter.image = UIImage(named: "complejo_Nat2")
                                                       }
                                                       else if parada == "generales_Biblio" {
                                self.ImageCenter.image = UIImage(named: "generales_Biblio")
                                                       }
                                                       else if parada == "generales_Centro" {
                                self.ImageCenter.image = UIImage(named: "generales_Centro")
                                                       }
                                                       else if parada == "generales_Educacion" {
                                self.ImageCenter.image = UIImage(named: "generales_Educacion")
                                                       }
                                                       else if parada == "generales_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "generales_FacundaB")
                                                       }
                                                       else if parada == "generales_MultiP" {
                                self.ImageCenter.image = UIImage(named: "generales_MultiP")
                                                       }
                                                       else if parada == "generales_Museo" {
                                self.ImageCenter.image = UIImage(named: "generales_Museo")
                                                       }
                                                       else if parada == "generales_Nat1" {
                                self.ImageCenter.image = UIImage(named: "generales_Nat1")
                                                       }
                                                       else if parada == "generales_natu2" {
                                self.ImageCenter.image = UIImage(named: "generales_Nat2")
                                                       }
                                                       else if parada == "leyes_Centro" {
                                self.ImageCenter.image = UIImage(named: "leyes_Centro")
                                                       }
                                                       else if parada == "leyes_Educacion" {
                                self.ImageCenter.image = UIImage(named: "leyes_Educacion")
                                                       }
                                                       else if parada == "leyes_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "leyes_FacundaB")
                                                       }
                                                       else if parada == "leyes_MultiP" {
                                self.ImageCenter.image = UIImage(named: "leyes_MultiP")
                                                       }
                                                       else if parada == "leyes_Museo" {
                                self.ImageCenter.image = UIImage(named: "leyes_Museo")
                                                       }
                                                       else if parada == "leyes_Nat1" {
                                self.ImageCenter.image = UIImage(named: "leyes_Nat1")
                                                       }
                                                       else if parada == "leyes_natu2" {
                                self.ImageCenter.image = UIImage(named: "leyes_Nat2")
                                                       }
                                                       else if parada == "merendero_Biblio" {
                                self.ImageCenter.image = UIImage(named: "merendero_Biblio")
                                                       }
                                                       else if parada == "merendero_Centro" {
                                self.ImageCenter.image = UIImage(named: "merendero_Centro")
                                                       }
                                                       else if parada == "merendero_Educacion" {
                                self.ImageCenter.image = UIImage(named: "merendero_Educacion")
                                                       }
                                                       else if parada == "merendero_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "merendero_FacundaB")
                                                       }
                                                       else if parada == "merendero_MultiP" {
                                self.ImageCenter.image = UIImage(named: "merendero_MultiP")
                                                       }
                                                       else if parada == "merendero_Museo" {
                                self.ImageCenter.image = UIImage(named: "merendero_Museo")
                                                       }
                                                       else if parada == "merendero_Nat1" {
                                self.ImageCenter.image = UIImage(named: "merendero_Nat1")
                                                       }
                                                       else if parada == "merendero_natu2" {
                                self.ImageCenter.image = UIImage(named: "merendero_Nat2")
                                                       }
                                                       else if parada == "natu1_Biblio" {
                                self.ImageCenter.image = UIImage(named: "nat1_Biblio")
                                                       }
                                                       else if parada == "natu1_Centro" {
                                self.ImageCenter.image = UIImage(named: "nat1_Centro")
                                                       }
                                                       else if parada == "natu1_Educacion" {
                                self.ImageCenter.image = UIImage(named: "nat1_Educacion")
                                                       }
                                                       else if parada == "natu1_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "nat1_FacundaB")
                                                       }
                                                       else if parada == "natu1_MultiP" {
                                self.ImageCenter.image = UIImage(named: "nat1_MultiP")
                                                       }
                                                       else if parada == "natu1_Museo" {
                                self.ImageCenter.image = UIImage(named: "nat1_Museo")
                                                       }
                                                       else if parada == "natu1_Nat1" {
                                self.ImageCenter.image = UIImage(named: "nat1_Nat1")
                                                       }
                                                       else if parada == "natu1_Natu2" {
                                self.ImageCenter.image = UIImage(named: "nat1_Nat2")
                                                       }
                                                       else if parada == "natu2_Biblio" {
                                self.ImageCenter.image = UIImage(named: "nat2_Biblio")
                                                       }
                                                       else if parada == "natu2_Centro" {
                                self.ImageCenter.image = UIImage(named: "nat2_Centro")
                                                       }
                                                       else if parada == "natu2_Educacion" {
                                self.ImageCenter.image = UIImage(named: "nat2_Educacion")
                                                       }
                                                       else if parada == "natu2_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "nat2_FacundaB")
                                                       }
                                                       else if parada == "natu2_MultiP" {
                                self.ImageCenter.image = UIImage(named: "nat2_MultiP")
                                                       }
                                                       else if parada == "natu2_Museo" {
                                self.ImageCenter.image = UIImage(named: "nat2_Museo")
                                                       }
                                                       else if parada == "natu2_Nat1" {
                                self.ImageCenter.image = UIImage(named: "nat2_Nat1")
                                                       }
                                                       else if parada == "natu2_Natu2" {
                                self.ImageCenter.image = UIImage(named: "nat2_Nat2")
                                                       }
                                                       else if parada == "rotc_Ent_Biblio" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Biblio")
                                                       }
                                                       else if parada == "rotc_Ent_Centro" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Centro")
                                                       }
                                                       else if parada == "rotc_Ent_Educacion" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Educacion")
                                                       }
                                                       else if parada == "rotc_Ent_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_FacundaB")
                                                       }
                                                       else if parada == "rotc_Ent_MultiP" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_MultiP")
                                                       }
                                                       else if parada == "rotc_Ent_Museo" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Museo")
                                                       }
                                                       else if parada == "rotc_Ent_Nat1" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Nat1")
                                                       }
                                                       else if parada == "rotc_Ent_natu2" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_Nat2")
                                                       }
                                                       else if parada == "rotcEntR_Biblio" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_Biblio")
                                                       }
                                                       else if parada == "rotcEntR_Centro" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_Centro")
                                                       }
                                                       else if parada == "rotcEntR_Educacion" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_Educacion")
                                                       }
                                                       else if parada == "rotcEntR_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_FacundaB")
                                                       }
                                                       else if parada == "rotcEntR_MultiP" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_MultiP")
                                                       }
                                                       else if parada == "rotcEntR_Museo" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_Museo")
                                                       }
                                                       else if parada == "rotcEntR_Nat1" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_Nat1")
                                                       }
                                                       else if parada == "rotcEntR_natu2" {
                                self.ImageCenter.image = UIImage(named: "rotcEntR_natu2")
                                                       }
                                                       else if parada == "rotc_Est_Biblio" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Biblio")
                                                       }
                                                       else if parada == "rotc_Est_Centro" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Centro")
                                                       }
                                                       else if parada == "rotcEst_Educacion" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Educacion")
                                                       }
                                                       else if parada == "rotcEst_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_FacundaB")
                                                       }
                                                       else if parada == "rotc_Est_MultiP" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_MultiP")
                                                       }
                                                       else if parada == "rotc_Est_Museo" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Museo")
                                                       }
                                                       else if parada == "rotc_Est_Nat1" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Nat1")
                                                       }
                                                       else if parada == "rotc_Est_natu2" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_Nat2")
                                                       }
                                                       else if parada == "complejo_Complejo" {
                                self.ImageCenter.image = UIImage(named: "complejo._Complejo")
                                                       }
                                                       else if parada == "generales_Generales" {
                                self.ImageCenter.image = UIImage(named: "generales_Generales")
                                                       }
                                                       else if parada == "leyes_Leyes" {
                                self.ImageCenter.image = UIImage(named: "leyes_Leyes")
                                                       }
                                                       else if parada == "merendero_Merendero" {
                                self.ImageCenter.image = UIImage(named: "merendero_Merendero")
                                                       }
                                                       else if parada == "rotcEnt_RotcEnt" {
                                self.ImageCenter.image = UIImage(named: "rotcEnt_RotcEnt")
                                                       }
                                                       else if parada == "complejo_Complejo" {
                                self.ImageCenter.image = UIImage(named: "complejo._Complejo")
                                                       }
                                                       else if parada == "rotc_Est_Rotc_Est" {
                                self.ImageCenter.image = UIImage(named: "rotcEst_RotcEst")
                                                       }
                                                       else if parada == "educacion_Educacion" {
                                self.ImageCenter.image = UIImage(named: "educacion")
                                                       }
                                                       else if parada == "facundaB_FacundaB" {
                                self.ImageCenter.image = UIImage(named: "facundaB")
                                                       }
                                else if parada == "multiP_MultiP" {
                                self.ImageCenter.image = UIImage(named: "multiP")
                                                       }
                                else if parada == "museo_Museo" {
                                self.ImageCenter.image = UIImage(named: "museo")
                                                       }

                                                       else {
                                self.ImageCenter.image = UIImage(named: "map")
                                                       }
                            
                                debugPrint(parada)
                            
        //                        var parada = parada
        //                        print(parada)

                        case .failure(let error):
                            print(error)
                        }

                        switch response.result {
                        case.success(let value1):
                            let json = JSON(value1)
                            if let tiempo = json["tiempo"].string{
                                debugPrint(tiempo)
                            }
                        case.failure(let error):
                            print(error)
                        }


                        }
                       
                    
              //  print("Parada: \()")
                            //parada = "bilbio_Biblio"
                    //
                    //cambiar imagen del mapa, según trolley cambie de parada
                           
    }
    
    func sender(){
        
        locationManager.startUpdatingLocation()
        
        locationManager.stopUpdatingLocation() // paramos el metodo de capturacion de localizacion
                   locations = locationManager.location
        
        let location: [String: Any] = ["latitude": locations.coordinate.latitude, "longitude": locations.coordinate.longitude]
                   //conect to server and send lat y long
                   AF.request("http://136.145.231.39/json-receiver.php", method:.post, parameters: location, encoding: JSONEncoding.default).response{ (response) in print(response)
                   }
        
    }

    
    //botton de las tracking
    @IBAction func trackingButton(_ sender: UIButton) {
        
        // si el estado esta en falso
        if estado != true{
        
         sender.setTitle("Stop", for: .normal) // cambia el texto del boton
         estado = true // cambiamos el estado a activado
         locationManager.startUpdatingLocation()
        timer2 = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)// activamos el metodo de capturacion de localizacion
         
        }
        else{ // si el estado esta en cierto
             sender.setTitle("Track", for: .normal) // cambiamos el texto del boton
            locationManager.stopUpdatingLocation() // paramos el metodo de capturacion de localizacion
            locations = locationManager.location
            timer2.invalidate()
            timer2 = Timer.scheduledTimer(timeInterval: 5.0, target: self, selector: #selector(update), userInfo: nil, repeats: true)// activamos el metodo de capturacion de localizacion
            
           
            estado = false// se cambia estado de nuevo a falso
        }
        
       
    }
    
    //conexión con la foto del mapa
//    @IBOutlet weak var ImageCenter: UIImageView!
    
}

