//
//  AddressConfig.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2020/1/14.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit
import CoreLocation

@objcMembers class AddressConfig: NSObject,CLLocationManagerDelegate {

    class func sharedInstance()->AddressConfig {
        struct SharedStatic {
            static var instance: AddressConfig = AddressConfig()
        }
        return SharedStatic.instance
    }
    var locationManager : CLLocationManager!
    func initLacationManager()  {
        if CLLocationManager .locationServicesEnabled() {
            locationManager = CLLocationManager.init()
            locationManager.delegate = self
            locationManager.requestAlwaysAuthorization()
            locationManager.requestWhenInUseAuthorization()
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.distanceFilter = 5.0
            
        }
    }
    func startUpdatingLocation(){
        locationManager.startUpdatingLocation()
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let curLoc:CLLocation = locations.first!
        print("经度："+String(curLoc.coordinate.longitude)+" 纬度："+String(curLoc.coordinate.latitude))
        AccountInfo.sharedInstance().latutide = String(curLoc.coordinate.longitude)+","+String(curLoc.coordinate.latitude)
        //print("高度："+String(curLoc.altitude)+"    垂直精确度："+String(curLoc.verticalAccuracy))
        //print("垂直精确度："+String(curLoc.verticalAccuracy))
        //print("楼层高度："+String(describing: curLoc.floor?.level))
        let geoCoder = CLGeocoder.init()
        geoCoder.reverseGeocodeLocation(curLoc) {[weak self] (placemarks, error) in
            if error != nil {
                return
            }
            for placemark in placemarks! {
                let address:NSDictionary = placemark.addressDictionary! as NSDictionary
                if (!JSONSerialization.isValidJSONObject(address)) {
                    print("无法解析出JSONString")
                    return
                }
                //                let data : NSData! = try? JSONSerialization.data(withJSONObject: address, options: []) as NSData!
                //                let JSONString = NSString(data:data as Data,encoding: String.Encoding.utf8.rawValue)
                //                print(JSONString ?? String())
                //print(address.object(forKey: "Country") ?? String())
                //print(address.object(forKey: "State") ?? String())
                //print(address.object(forKey: "City") ?? String())
                let arr = address.object(forKey: "FormattedAddressLines") as! NSArray
                //print(arr[0])
                AccountInfo.sharedInstance().address = arr[0] as? String
//                self?.menhuLabel.text = address.object(forKey: "City") as? String
                self?.locationManager.stopUpdatingLocation()
            }
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }
}
