<commit_before>//
//  Facade.swift
//  JFWindguru
//
//  Created by javierfuchs on 7/9/17.
//  Copyright © 2017 CocoaPods. All rights reserved.
//

import Foundation
import JFCore
import JFWindguru

/*
 *  WDFacade
 *
 *  Discussion:
 *    Manager of synchronization between the watch/iphone/ipad it shares plist files using a common group.
 *    It is subscribed to notifications of the LocationManager to update the plist file with the new location/country.
 */


public class Facade: NSObject {
    
    //
    // Attributes only modified by this class
    //
    private var currentLocality: String?
    private var currentCountry: String?
    
    //
    // Singleton
    //
    public static let instance = Facade()
    
    
    //
    // start service manager
    //
    public func start()
    {
        startLocationServices()
        startForecastServices()
    }
    
    //
    // stop service manager
    //
    public func stop()
    {
        stopLocationServices()
        stopForecastServices()
    }
    
    
    //
    // restore information (used by watch)
    //
    public func restoreInfo() -> Bool
    {
        guard let filePath = absoluteFilePath() else {
            return false
        }
        
        if FileManager.default.fileExists(atPath: filePath.path) {

            guard let dict = NSDictionary.init(contentsOf: filePath) as? Dictionary<String, NSDictionary> else {
                return false
            }
            
            let location : NSDictionary = dict[JFCore.Constants.Notification.locationUpdated]!
            if (location != NSNull())
            {
                // Treat location update
                
                currentLocality = String(describing: location[kWDKeyLocality]!)
                currentCountry = String(describing: location[kWDKeyCountry]!)
                
                return true
            }
        } else {
            print("No last location")
        }
        return false
    }
    
    //
    // Location code
    //
    private func startLocationServices()
    {
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: JFCore.Constants.Notification.locationUpdated), object: nil, queue: OperationQueue.main, using: {
            [weak self] note in
//                if (TARGET_OS_WATCH == 1)
//                {
//                    if let ret = self?.restoreInfo(), ret == true {
//                        self?.updateForecast()
//                    }
//                }
//                else
//                {
                    guard let locations = LocationManager.instance.locations,
                          let location = locations.first else {
                        return
                    }
                    LocationManager.instance.reverseLocation(location: location, didFailWithError: { (error) in
                        print("error: \(error)")
                    }, didUpdatePlacemarks: { (placemarks) in
                        guard let placemark = placemarks.first,
                            let locality = placemark.locality,
                            let country = placemark.country else {
                            return
                        }
                        self?.currentLocality = locality
                        self?.currentCountry = country
                        
                        let dict = [kWDKeyLocality: locality, kWDKeyCountry: country]
                        self?.writeNote(note: JFCore.Constants.Notification.locationUpdated, location: dict)
                        self?.updateForecast()
                    })
//                }
        })
        
    }
    
    private func stopLocationServices()
    {
        LocationManager.instance.stop()
    }
    
    
    public func getCurrentLocation() -> String?
    {
        return currentLocality
    }
    
    //<commit_msg>Hardcoded for now in watch version<commit_after>//
//  Facade.swift
//  JFWindguru
//
//  Created by javierfuchs on 7/9/17.
//  Copyright © 2017 CocoaPods. All rights reserved.
//

import Foundation
import JFCore
import JFWindguru

/*
 *  WDFacade
 *
 *  Discussion:
 *    Manager of synchronization between the watch/iphone/ipad it shares plist files using a common group.
 *    It is subscribed to notifications of the LocationManager to update the plist file with the new location/country.
 */


public class Facade: NSObject {
    
    //
    // Attributes only modified by this class
    //
    private var currentLocality: String?
    private var currentCountry: String?
    
    //
    // Singleton
    //
    public static let instance = Facade()
    
    
    //
    // start service manager
    //
    public func start()
    {
        startLocationServices()
        startForecastServices()
    }
    
    //
    // stop service manager
    //
    public func stop()
    {
        stopLocationServices()
        stopForecastServices()
    }
    
    
    //
    // restore information (used by watch)
    //
    public func restoreInfo() -> Bool
    {
        guard let filePath = absoluteFilePath() else {
            return false
        }
        
        if FileManager.default.fileExists(atPath: filePath.path) {

            guard let dict = NSDictionary.init(contentsOf: filePath) as? Dictionary<String, NSDictionary> else {
                return false
            }
            
            let location : NSDictionary = dict[JFCore.Constants.Notification.locationUpdated]!
            if (location != NSNull())
            {
                // Treat location update
                
                currentLocality = String(describing: location[kWDKeyLocality]!)
                currentCountry = String(describing: location[kWDKeyCountry]!)
                
                return true
            }
        } else {
            print("No last location")
        }
        return false
    }
    
    //
    // Location code
    //
    private func startLocationServices()
    {
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: JFCore.Constants.Notification.locationUpdated), object: nil, queue: OperationQueue.main, using: {
            [weak self] note in
//                if (TARGET_OS_WATCH == 1)
//                {
//                    if let ret = self?.restoreInfo(), ret == true {
//                        self?.updateForecast()
//                    }
//                }
//                else
//                {
                    guard let locations = LocationManager.instance.locations,
                          let location = locations.first else {
                        return
                    }
                if (TARGET_OS_WATCH == 1) {
                    let locality = "Cupertino"
                    let country = "US"
                    self?.currentLocality = locality
                    self?.currentCountry = country
                    
                    let dict = [kWDKeyLocality: locality, kWDKeyCountry: country]
                    self?.writeNote(note: JFCore.Constants.Notification.locationUpdated, location: dict)
                    self?.updateForecast()
                }
                else
                {
                    LocationManager.instance.reverseLocation(location: location, didFailWithError: { (error) in
                        print("error: \(error)")
                    }, didUpdatePlacemarks: { (placemarks) in
                        guard let placemark = placemarks.first,
                            let locality = placemark.locality,
                            let country = placemark.country else {
                                return
                        }
                        self?.currentLocality = locality
                        self?.currentCountry = country
                        
                        let dict = [kWDKeyLocality: locality, kWDKeyCountry: country]
                        self?.writeNote(note: JFCore.Constants.Notification.locationUpdated, location: dict)
                        self?.updateForecast()
                    })
                    
                }
        })
        
    }
    
    private func stopLocationServices()
    {
        LocationManager.instance.stop()
    }
    
    
    public func getCurrentLocation() -> String?
    {
        return currentLocality
    }
    
    //
{'fraction_non_alphanumeric': 0.08154250238777459, 'fraction_numerical': 0.002268385864374403, 'mean_word_length': 1.3121722329561136, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3927560', 'n_tokens_mistral': 2097, 'n_tokens_neox': 1978, 'n_words': 494}