//
//  Measurement+String.swift
//  1Weather
//
//  Created by Dmitry Stepanets on 03.03.2021.
//

import Foundation

extension Measurement {
    private static func formatter(style: Formatter.UnitStyle) -> MeasurementFormatter {
        let fmt = MeasurementFormatter()
        fmt.locale = Settings.shared.locale
        fmt.unitStyle = style
        fmt.unitOptions = .providedUnit
        fmt.numberFormatter.maximumFractionDigits = 0
        return fmt
    }
    
    var string:String {
        return Measurement.formatter(style: .medium).string(from: self)
    }
    
    var shortString:String {
        return Measurement.formatter(style: .short).string(from: self)
    }
    
    var longString:String {
        return Measurement.formatter(style: .long).string(from: self)
    }
}

extension Temperature {
    var settingsConvertedValue:Double {
        return self.converted(to: Settings.shared.temperatureType).value.rounded(.down)
    }
    
    var settingsConverted:Measurement {
        return self.converted(to: Settings.shared.temperatureType)
    }
}

extension WindSpeed {
    var settingsConvertedValue:Double {
        return self.converted(to: Settings.shared.windSpeedType).value.rounded(.down)
    }
    
    var settingsConverted:Measurement {
        return self.converted(to: Settings.shared.windSpeedType)
    }
}

extension Visibility {
    var settingsConvertedValue:Double {
        return self.converted(to: Settings.shared.distanceType).value.rounded(.down)
    }
    
    var settingsConverted:Measurement {
        return self.converted(to: Settings.shared.distanceType)
    }
}

extension Pressure {
    var settingsConvertedValue:Double {
        return self.converted(to: Settings.shared.pressureType).value.rounded(.down)
    }
    
    var settingsConverted:Measurement {
        return self.converted(to: Settings.shared.pressureType)
    }
}

