//
//  HourlyWeather.swift
//  1Weather
//
//  Created by Demid Merzlyakov on 19.02.2021.
//

import Foundation

public struct HourlyWeather: Equatable, Hashable {
    public var lastTimeUpdated: Date
    public var date: Date
    public var timeZone: TimeZone
    public var weekDay: WeekDay
    public var type: WeatherType = .unknown
    public var isDay: Bool
    
    public var temp: Temperature?
    public var apparentTemp: Temperature?
    
    public var windSpeed: WindSpeed?
    public var windDirection: WindDirection?
    public var precipitationProbability: Percent?
    
    public var humidity: Percent?
}

extension HourlyWeather: UpdatableModelObjectInTime {
    public func mergedWith(incrementalChanges: HourlyWeather) -> HourlyWeather {
        var result = self
        result.lastTimeUpdated = incrementalChanges.lastTimeUpdated
        result.date = incrementalChanges.date
        result.timeZone = incrementalChanges.timeZone
        result.weekDay = incrementalChanges.weekDay
        result.type = incrementalChanges.type
        result.isDay = incrementalChanges.isDay
        
        // Since this class is meant to contain frequently updated info, the preference is given to incrementalChanges data.
        
        result.temp = incrementalChanges.temp ?? result.temp
        result.apparentTemp = incrementalChanges.apparentTemp ?? result.apparentTemp
        result.windSpeed = incrementalChanges.windSpeed ?? result.windSpeed
        result.windDirection = incrementalChanges.windDirection ?? result.windDirection
        result.precipitationProbability = incrementalChanges.precipitationProbability ?? result.precipitationProbability
        result.humidity = incrementalChanges.humidity ?? result.humidity
        return result
    }
}
