//
//  File.swift
//  USB Meter
//
//  Created by Bogdan Timofte on 08/05/2020.
//  Copyright © 2020 Bogdan Timofte. All rights reserved.
//

import SwiftUI

struct RoundedRectangleBackground: ViewModifier {
    
    var cornerRadius: CGFloat
    var foregroundColor: Color
    var opacity: Double
    var blurRadius: CGFloat

    func body(content: Content) -> some View {
        content.background(
            RoundedRectangle(cornerRadius: cornerRadius)
                .foregroundColor(foregroundColor)
                .opacity(opacity)
        .blur(radius: blurRadius)
        )
    }
}

struct RoundedRectangleBorder: ViewModifier {
    
    var cornerRadius: CGFloat
    var foregroundColor: Color
    var lineWidth: CGFloat
    var blurRadius: CGFloat
    
    func body(content: Content) -> some View {
        content.background(
            RoundedRectangle(cornerRadius: cornerRadius)
                .stroke(lineWidth: lineWidth)
                .foregroundColor(foregroundColor)
                .blur(radius: blurRadius)
        )
    }
}

extension View {
    func withRoundedRectangleBackground( cornerRadius: CGFloat, foregroundColor: Color, opacity: Double, blurRadius: CGFloat = 0 ) -> some View {
        self.modifier(RoundedRectangleBackground(cornerRadius: cornerRadius, foregroundColor: foregroundColor, opacity: opacity, blurRadius: blurRadius))
    }
    
    func withRoundedRectangleBorder( cornerRadius: CGFloat, foregroundColor: Color, lineWidth: CGFloat, blurRadius: CGFloat = 0 ) -> some View {
        self.modifier(RoundedRectangleBorder(cornerRadius: cornerRadius, foregroundColor: foregroundColor, lineWidth: lineWidth, blurRadius: blurRadius ))
    }
}

// MARK: Local
extension Button {
    func asEnableFeatureButton(state: Bool) -> some View {
        self
            .foregroundColor( state ? .primary : .blue )
            .padding(5)
            .withRoundedRectangleBackground(cornerRadius: 15, foregroundColor: state ? .blue : .clear, opacity: 0.5)
            .withRoundedRectangleBorder(cornerRadius: 15, foregroundColor: .blue, lineWidth: 1, blurRadius: 0.1)
    }
}

