//
//  SQButton.swift
//
//
//  Created by Victor on 12/06/2024.
//

import SwiftUI

struct SQButton: View {
    var title: String
    var color: Color = .sqNeutral(10)
    var textColor: Color = .black
    var textSize: CGFloat = 16
    var font: SQTextFont = .demiBold
    var isFull: Bool = true
    @Binding var isLoading: Bool
    let action: () -> Void

    init(_ title: String, color: Color = .sqNeutral(10), textColor: Color = .black, textSize: CGFloat = 16, font: SQTextFont = .demiBold, isFull: Bool = true, isLoading: Binding<Bool> = .constant(false), action: @escaping () -> Void) {
        self.title = title
        self.color = color
        self.textColor = textColor
        self.textSize = textSize
        self.font = font
        self.isFull = isFull
        self._isLoading = isLoading
        self.action = action
    }

    var body: some View {
        Button(action: {
            self.action()
        }, label: {
            if isLoading {
                ProgressView()
                    .padding(.horizontal, 30)
                    .padding(.vertical, 12)
                    .frame(height: 40, alignment: .center)
                    .background(isFull ? color : .clear)
                    .cornerRadius(100)
                    .overlay(
                        RoundedRectangle(cornerRadius: 100)
                            .inset(by: 0.5)
                            .stroke(color, lineWidth: 1)
                    )
            } else {
                SQText(title, size: textSize, font: font, textColor: textColor)
                    .padding(.horizontal, 30)
                    .padding(.vertical, 12)
                    .frame(height: 40, alignment: .center)
                    .background(isFull ? color : .clear)
                    .cornerRadius(100)
                    .overlay(
                        RoundedRectangle(cornerRadius: 100)
                            .inset(by: 0.5)
                            .stroke(color, lineWidth: 1)
                    )
            }
        })
        .buttonStyle(PlainButtonStyle())
    }
}

extension SQButton {
    func sqStyle(_ theme: SQButtonStyle = .neutral) -> some View {
        modifier(SQButtonModifier(theme: theme))
    }
}

struct SQButtonModifier: ViewModifier {
    let theme: SQButtonStyle

    func body(content: Content) -> some View {
        content
    }
}

enum SQButtonStyle {
    case royal
    case neutral
    case purple
    case orange
}

#Preview {
    VStack(spacing: 16) {
        SQButton("C'est parti !") {}
            .sqStyle()
        SQButton("C'est parti !", color: .sqOrange(50), textColor: .white) {}
            .sqStyle()
    }
}
