//
//  WebFieldAutofillButton.swift
//  Beam
//
//  Created by Frank Lefebvre on 13/12/2021.
//

import SwiftUI

struct WebFieldAutofillButton: View {
    var imageName: String
    var renderingMode: Image.TemplateRenderingMode
    var action: () -> Void

    @State private var isHovering = false

    var body: some View {
        Button(action: action) {
            ZStack {
                backgroundColor
                    .frame(width: 24, height: 24)
                    .cornerRadius(3)
                Image(imageName)
                    .renderingMode(renderingMode)
                    .foregroundColor(isHovering ? BeamColor.WebFieldAutofill.fieldButtonIconHovered.swiftUI : BeamColor.WebFieldAutofill.fieldButtonIcon.swiftUI)
            }
            .onHover { isHovering = $0 }
        }
        .buttonStyle(.borderless)
        .environment(\.colorScheme, .light)
    }

    var backgroundColor: Color {
        isHovering ? BeamColor.WebFieldAutofill.fieldButtonBackgroundHovered.swiftUI : .clear
    }
}

struct WebFieldAutofillButton_Previews: PreviewProvider {
    static var previews: some View {
        WebFieldAutofillButton(imageName: "autofill-password", renderingMode: .template) {}
    }
}
