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

import Foundation

public struct CurrentWeather: 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 minTemp: Temperature?
    public var maxTemp: Temperature?
    public var windSpeed: WindSpeed?
    public var windDirection: WindDirection?
    public var precipitationProbability: Percent?
    
    public var temp: Temperature?
    public var apparentTemp: Temperature?
    public var humidity: Percent?
    public var visibility: Visibility?
    public var pressure: Pressure?
    
    public var sunrise: Date?
    public var sunset: Date?
    public var sunState: CelestialState? = .normal
    public var moonrise: Date?
    public var moonset: Date?
    public var moonState: CelestialState? = .normal
    public var moonPhase: MoonPhase? = .unknown
}

extension CurrentWeather: UpdatableModelObjectInTime {
    public func mergedWith(incrementalChanges: CurrentWeather) -> CurrentWeather {
        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.minTemp = incrementalChanges.minTemp ?? result.minTemp
        result.maxTemp = incrementalChanges.maxTemp ?? result.maxTemp
        result.windSpeed = incrementalChanges.windSpeed ?? result.windSpeed
        result.windDirection = incrementalChanges.windDirection ?? result.windDirection
        result.precipitationProbability = incrementalChanges.precipitationProbability ?? result.precipitationProbability
        
        result.temp = incrementalChanges.temp ?? result.temp
        result.apparentTemp = incrementalChanges.apparentTemp ?? result.apparentTemp
        result.humidity = incrementalChanges.humidity ?? result.humidity
        result.visibility = incrementalChanges.visibility ?? result.visibility
        result.pressure = incrementalChanges.pressure ?? result.pressure
        
        result.sunrise = incrementalChanges.sunrise ?? result.sunrise
        result.sunset = incrementalChanges.sunset ?? result.sunset
        result.sunState = incrementalChanges.sunState ?? result.sunState
        result.moonrise = incrementalChanges.moonrise ?? result.moonrise
        result.moonset = incrementalChanges.moonset ?? result.moonset
        result.moonState = incrementalChanges.moonState ?? result.moonState
        result.moonPhase = incrementalChanges.moonPhase ?? result.moonPhase
        
        return result
    }
}
