//
//  NomadTools+Int.swift
//  NomadTools
//
//  Created by Justin Ackermann on 5/27/21.
//

import UIKit

public extension Int {
    
    /// Facts
    var isEven: Bool { self % 2 == 0 }
    var isOdd: Bool { !isEven }
    
    /// Base Conversions
    var double: Double      { Double(self) }
    var float: Float        { Float(self) }
    var cgfloat: CGFloat    { CGFloat(self) }
    var number: NSNumber    { NSNumber(value: self) }
    
    /// Date Conversions
    var date: Date
    { return Date(timeIntervalSince1970: TimeInterval(self)) }
    
    var interval: TimeInterval
    { return TimeInterval(self) }
    
    var ordinal: String? {
        let ordinalFormatter: NumberFormatter = {
            let formatter = NumberFormatter()
            formatter.numberStyle = .ordinal
            return formatter
        }()
        return ordinalFormatter.string(from: NSNumber(value: self))
    }
    
    /// Time
    func HMS() -> (Int, Int, Int)
    { return (self / 3600, (self % 3600) / 60, (self % 3600) % 60) }
    
    /// Distance
    var feet: Distance
    { Distance(is: self, in: .feet) }
    
    var meters: Distance
    { Distance(is: self, in: .meters) }
    
    var kilometers: Distance
    { Distance(is: self, in: .kilometers) }
    
    var miles: Distance
    { Distance(is: self, in: .miles) }
    
    var nautical: Distance
    { Distance(is: self, in: .nautical) }
    
    /// Stringify's
    var string: String
    { "\(self)" }
    
    var stringify: String {
        let numberFormatter = NumberFormatter()
        numberFormatter.numberStyle = .decimal
        return numberFormatter.string(from: NSNumber(value: self)) ?? "0"
    }
    
    var socialify: String {
        let num = abs(Double(self))
        let sign = (self < 0) ? "-" : ""

        switch num {
        case 1_000_000_000...:
            var formatted = num / 1_000_000_000
            formatted = formatted.truncate(places: 1)
            return "\(sign)\(formatted)B"

        case 1_000_000...:
            var formatted = num / 1_000_000
            formatted = formatted.truncate(places: 1)
            return "\(sign)\(formatted)M"

        case 1_000...:
            var formatted = num / 1_000
            formatted = formatted.truncate(places: 1)
            return "\(sign)\(formatted)K"

        case 0...:
            return "\(self)"

        default:
            return "\(sign)\(self)"

        }
    }
}
