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

import Foundation

public struct DailyWeather: 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 isToday: Bool {
        return Calendar.timeZoneCalendar(timeZone: timeZone).isDateInToday(date)
    }
    
    public var minTemp: Temperature?
    public var maxTemp: Temperature?
    public var windSpeed: WindSpeed?
    public var windDirection: WindDirection?
    public var precipitationProbability: Percent?
    
    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 DailyWeather: UpdatableModelObjectInTime {
    public func mergedWith(incrementalChanges: DailyWeather) -> DailyWeather {
        var result = self
        result.lastTimeUpdated = incrementalChanges.lastTimeUpdated
        result.date = incrementalChanges.date
        result.timeZone = incrementalChanges.timeZone
        result.weekDay = incrementalChanges.weekDay
        result.type = incrementalChanges.type
        
        // 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.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
    }
}
