//
//  ViewModifiers.swift
//  LeCountdown
//
//  Created by Laurent Morvillier on 27/01/2023.
//

import Foundation
import SwiftUI

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

struct RoundedCornerSelection: ViewModifier {
    
    var selected: Bool
    
    func body(content: Content) -> some View {
        if selected {
            content
                .overlay(
                    RoundedRectangle(cornerRadius: 40)
                        .stroke(Color.blue, lineWidth: 5)
                )
        } else {
            content
        }
    }
    
}

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)
    }
}
