//
//  Random.swift
//  Feels
//
//  Created by Trey Tartt on 1/9/22.
//

import Foundation
import SwiftUI

struct Constants {
    static let groupShareId = "group.com.88oak.ifeel"
}

struct GroupUserDefaults {
    static var groupDefaults: UserDefaults {
        return UserDefaults(suiteName: Constants.groupShareId)!
    }
}

class Random {
    static var tomorrowMidnightThirty: Date {
        let components = DateComponents(hour: 0, minute: 30, second: 0)
        var updateTime = Date()
        if let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date()),
           let tomorrowMorning = Calendar.current.date(byAdding: components, to: tomorrow) {
            updateTime = tomorrowMorning
        }
        return updateTime
    }
}

extension Date: RawRepresentable {
    public var rawValue: String {
        self.timeIntervalSinceReferenceDate.description
    }
    
    public init?(rawValue: String) {
        self = Date(timeIntervalSinceReferenceDate: Double(rawValue) ?? 0.0)
    }
}


struct RoundedCorner: Shape {
    
    var radius: CGFloat = .infinity
    var corners: UIRectCorner = .allCorners
    
    func path(in rect: CGRect) -> Path {
        let path = UIBezierPath(roundedRect: rect, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        return Path(path.cgPath)
    }
}

extension View {
    func cornerRadius(_ radius: CGFloat, corners: UIRectCorner) -> some View {
        clipShape( RoundedCorner(radius: radius, corners: corners) )
    }
}
