//
//  PositionView.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/15.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit
import CoreLocation

class PositionView: UIView,CLLocationManagerDelegate,UITextFieldDelegate{
    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
            locationManager.startUpdatingLocation()
        }
    }
    lazy var dituBtn : UIButton = {
        let dituBtn = UIButton.init(frame: CGRect(x:0, y: 0, width:15*WHIDTH_RATIO() , height:19 ))
//        dituBtn.backgroundColor = UIColor.yellow
        dituBtn.setBackgroundImage(UIImage.init(named: "pin_image"), for: .normal)
        return dituBtn;
    }()
    lazy var menhuLabel : UILabel = {
        let menhuLabel = UILabel.init(frame: CGRect(x:20*WHIDTH_RATIO(), y: 0, width:getTexWidth(textStr: "上药门户", font: UIFont.systemFont(ofSize: 15.0), height: 14) , height:14 ))
        menhuLabel.font = UIFont.systemFont(ofSize: 13.0)
        menhuLabel.textColor = UIColor.white
//        menhuLabel.backgroundColor = UIColor.white
        return menhuLabel;
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        addInitView()
        initLacationManager()
    }
    func addInitView() {
        self.backgroundColor = UIColor.clear
        dituBtn.centerY = self.centerY
        menhuLabel.centerY = self.centerY
        self.addSubview(dituBtn)
        self.addSubview(menhuLabel)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    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)
    }
    
}
