//
//  Extensions.swift
//  Cloudlet
//
//  Created by Alexander Senin on 07.03.2021.
//

import SwiftUI
import UIKit

extension UIApplication {
    func endEditing() {
        sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
    }
}

extension View {
    func navigationBarColor(backgroundColor: UIColor?, titleColor: UIColor?) -> some View {
        self.modifier(NavigationBarModifier(backgroundColor: backgroundColor, titleColor: titleColor))
    }
    func eraseToAnyView() -> AnyView {
        AnyView(self)
    }
}

extension UserDefaults {
    static let group = UserDefaults(suiteName: "group.WidgetSupport")!
}

extension Int {
    
    var timestamp: TimeInterval {
        return TimeInterval(self)
    }
    
    var toString: String {
        return String(self)
    }
    
    var toDouble: Double {
        return Double(self)
    }
}

extension Double {
    
    var direction: Direction {
        return Direction(self)
    }
    
    var toString: String {
        return String(self)
    }
    
    var toInt: Int {
        return Int(self)
    }
}

extension String {
    func capitalizingFirstLetter() -> String {
        return prefix(1).capitalized + dropFirst()
    }
}

enum Direction: String {
    case N, NE, E, SE, S, SW, W, NW, null
}

extension Direction: CustomStringConvertible  {
    
    static let all: [Direction] = [.N, .NE, .E, .SE, .S, .SW, .W, .NW, .null]
    
    struct LocalizableStrings {
        static let N = NSLocalizedString("N", comment: "")
        static let NE = NSLocalizedString("NE", comment: "")
        static let E = NSLocalizedString("E", comment: "")
        static let SE = NSLocalizedString("SE", comment: "")
        static let S = NSLocalizedString("S", comment: "")
        static let SW = NSLocalizedString("SW", comment: "")
        static let W = NSLocalizedString("W", comment: "")
        static let NW = NSLocalizedString("NW", comment: "")
        static let null = NSLocalizedString("—", comment: "")
    }
    
    var description: String {
        get {
            switch(self) {
            case .N:
                return LocalizableStrings.N
            case .NE:
                return LocalizableStrings.NE
            case .E:
                return LocalizableStrings.E
            case .SE:
                return LocalizableStrings.SE
            case .S:
                return LocalizableStrings.S
            case .SW:
                return LocalizableStrings.SW
            case .W:
                return LocalizableStrings.W
            case .NW:
                return LocalizableStrings.NW
            case .null:
                return LocalizableStrings.null
            }
        }
    }
    
    init(_ direction: Double) {
        let index = Int((direction + 22.5).truncatingRemainder(dividingBy: 360) / 45)
        self = Direction.all[index]
    }
}

extension Color {
    init(_ hex: UInt32, opacity: Double = 1.0) {
        let red = Double((hex & 0xff0000) >> 16) / 255.0
        let green = Double((hex & 0xff00) >> 8) / 255.0
        let blue = Double((hex & 0xff) >> 0) / 255.0
        self.init(.sRGB, red: red, green: green, blue: blue, opacity: opacity)
    }
}

let hexColor: (UInt32) -> (Color) = {
    return Color($0)
}

extension URL {
    
    func withQueries(_ queries: [String: String]) -> URL? {
        guard var component = URLComponents(url: self, resolvingAgainstBaseURL: true) else {
            fatalError()
        }
        component.queryItems = queries.map {
            URLQueryItem(name: $0.key, value: $0.value)
        }
        return component.url
    }
}

extension DateFormatter {
    
    convenience init(dateFormat: String) {
        self.init()
        self.dateFormat = dateFormat
    }
    
    static var shared: DateFormatter = {
        return DateFormatter()
    }()
}

func timeFromTimestamp(_ timeStamp: TimeInterval) -> String {
    let date = Date(timeIntervalSince1970: timeStamp)
    let dateFormatter = DateFormatter(dateFormat: "HH:mm")
    return dateFormatter.string(from: date)
}

func intTimeFromTimestamp(_ timeStamp: TimeInterval) -> Int {
    let date = Date(timeIntervalSince1970: timeStamp)
    let dateFormatter = DateFormatter(dateFormat: "HHmm")
    let intHour = Int(dateFormatter.string(from: date)) ?? 0
    return intHour
}

func shortDayFromTimestamp(_ timeStamp: TimeInterval) -> String {
    let date = Date(timeIntervalSince1970: timeStamp)
    let dateFormatter = DateFormatter(dateFormat: "E")
    return dateFormatter.string(from: date)
}

func dayFromTimestamp(_ timeStamp: TimeInterval) -> String {
    let date = Date(timeIntervalSince1970: timeStamp)
    let dateFormatter = DateFormatter(dateFormat: "EEEE")
    return dateFormatter.string(from: date)
}

func longDayFromTimestamp(_ timeStamp: TimeInterval) -> String {
    let date = Date(timeIntervalSince1970: timeStamp)
    let dateFormatter = DateFormatter(dateFormat: "E, MMM d")
    return dateFormatter.string(from: date)
}
