//
//  LocationVC.swift
//  WoWonder
//
//  Created by Abdul Moid on 18/01/2021.
//  Copyright © 2021 ScriptSun. All rights reserved.
//

import UIKit
import CoreLocation
import GoogleMaps

class LocationVC: UIViewController {
    
    // MARK: - IBOutelets
    
    @IBOutlet weak var mapView: GMSMapView!
    
    // MARK: - Properties
    
    private let locationManager = CLLocationManager()
    var delegate: sendLocationProtocol?
    var latitude: Double? = 0.0
    var longitude: Double? = 0.0
    let marker = GMSMarker()
    
    // MARK: - View Life Cycels
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.initialConfig()
    }
    
    // MARK: - Selectors
    
    // Back Button Action
    @IBAction func backButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        if self.navigationController != nil {
            self.navigationController?.popViewController(animated: true)
        } else {
            self.dismiss(animated: true)
        }
    }
    
    // Send Button Action
    @IBAction func sendButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        self.delegate?.sendLocation(lat: self.latitude ?? 00, long: self.longitude ?? 0)
        if self.navigationController != nil {
            self.navigationController?.popViewController(animated: true)
        } else {
            self.dismiss(animated: true)
        }
    }
    
    // My Location Button Action
    @IBAction func myLocationButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
        guard let location = self.locationManager.location else { return }
        self.latitude = location.coordinate.latitude
        self.longitude = location.coordinate.longitude
        self.mapView.camera = GMSCameraPosition(latitude: self.latitude ?? 0, longitude: self.longitude ?? 0, zoom: 15)
        self.marker.position = CLLocationCoordinate2D(latitude: self.latitude ?? 0 , longitude: self.longitude ?? 0)
    }
    
    // Near By Button Action
    @IBAction func nearByButtonAction(_ sender: UIButton) {
        self.view.endEditing(true)
    }
    
    // MARK: - Helper Functions
    
    // Initial Config
    func initialConfig() {
        self.mapView.delegate = self
        self.locationManager.delegate = self
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.requestAlwaysAuthorization()
    }
    
}

// MARK: - Extensions

// MARK: CLLocationManagerDelegate
extension LocationVC: CLLocationManagerDelegate {
    
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        guard status == .authorizedWhenInUse || status == .authorizedAlways else { return }
        self.locationManager.startUpdatingLocation()
        self.mapView.isMyLocationEnabled = true
        self.mapView.settings.myLocationButton = true
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let location = locations.first else { return }
        print("Lat = \(location.coordinate.latitude)")
        print("long = \(location.coordinate.longitude)")
        self.latitude = location.coordinate.latitude
        self.longitude = location.coordinate.longitude
        self.mapView.camera = GMSCameraPosition(latitude: self.latitude ?? 0, longitude: self.longitude ?? 0, zoom: 15)
        self.marker.position = CLLocationCoordinate2D(latitude: self.latitude ?? 0 , longitude: self.longitude ?? 0)
        self.marker.map = self.mapView
        self.locationManager.stopUpdatingLocation()
    }
    
}

// MARK: GMSMapViewDelegate
extension LocationVC: GMSMapViewDelegate {
    
    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
        self.latitude = position.target.latitude
        self.longitude = position.target.longitude
        self.marker.position = position.target
    }
    
}
