//
//  LocationMng.swift
//  Em-call
//
//  Created by Alex Sh on 22.02.2022.
//

import Foundation
import UIKit
import MapKit

class LocationMng: NSObject, CLLocationManagerDelegate {
   // var mapView: MKMapView? = nil
   // synthesized mapView
    var showAlert:ShowAlert = ShowAlert()
    let locationManager = CLLocationManager()
    /*convenience init( map_View: MKMapView) {
        self.init()
        mapView = map_View
    }*/
  /*  convenience init() {
        self.init()
      //  mapView = mainView.mapView
    }*/
    func checkLocationEnable() {
                if CLLocationManager.locationServicesEnabled() {
       // continue to implement here
                    setupManager()
                    checkAuthorization()
                } else {
       // Do something to let users know why they need to turn it on.
                    showAlert.showAlertLocation(title:"Location service turned off",                                      message:"Turn on?",url:URL(string: "App-Prefs:root=LOCATION_SERVICES"))
                }
    }
    
    func setupManager()  {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
    
    func checkAuthorization() {
      switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
           // mapView!.showsUserLocation = true
            locationManager.startUpdatingLocation()
            break
        case .denied:
            showAlert.showAlertLocation(title:"You have blocked location viewing", message:"Do you want to change it?",
                              url:URL(string: UIApplication.openSettingsURLString))
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            //mapView!.showsUserLocation = true
            break
        case .restricted: break
        case .authorizedAlways: break
      @unknown default:
        print("Error authorizationStatus")
      }
    }
    
    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
      // 3
      if status == .authorizedWhenInUse {
        // 4
        locationManager.startUpdatingLocation()
         //5
       // mapView.myLocationEnabled = true     // mapView.settings.myLocationButton = true
      }
    }
    
}
