//
//  TodayViewModel.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 01.03.2021.
//

import UIKit

class TodayViewModel: ViewModelProtocol {
    //Public
    public let todayCellFactory = TodayCellFactory()
    public weak var delegate:ViewModelDelegate?
    public private(set) var location: Location?
    
    //Private
    private var locationManager: LocationManager
    
    deinit {
        self.locationManager.remove(delegate: self)
    }
    
    public init(locationManager: LocationManager) {
        self.locationManager = locationManager
        locationManager.add(delegate: self)
        
        //Setup factory callback
        todayCellFactory.onGetLocation = {[weak self] in
            return self?.location
        }
    }
    
    public func updateWeather() {
        locationManager.updateWeather()
    }
}

//MARK:- LocationManager Delegate
extension TodayViewModel: LocationManagerDelegate {
    func locationManager(_ locationManager: LocationManager, changedCurrentLocation newLocation: Location?) {
        DispatchQueue.main.async {
            print("TVM")
            self.location = newLocation
            self.delegate?.viewModelDidChange(model: self)
        }
    }
}
