import SwiftUI

struct ToggleButton: View {
    @Binding var isOn: Bool
    let title: String
    let showIcon: Bool
    
    init(title: String = "Instrumental", isOn: Binding<Bool>, showIcon: Bool = true) {
        self.title = title
        self._isOn = isOn
        self.showIcon = showIcon
    }
    
    // Convenience initializer for backward compatibility
    init(title: String = "Instrumental", isOn: Bool = false, showIcon: Bool = true) {
        self.title = title
        self._isOn = .constant(isOn)
        self.showIcon = showIcon
    }
    
    var body: some View {
        HStack(spacing: showIcon ? 4 : 0) {
            // Checkmark icon (optional)
            if showIcon {
                ZStack {
                    // Checkmark path
                    if isOn {
                        Image("Icon/success")
                            .resizable()
                            .renderingMode(.template)
                            .foregroundColor(Constants.Colors.Accent.brand)
                            .frame(width: 16, height: 16)
                    } else {
                        Image("Icon/success")
                            .resizable()
                            .renderingMode(.template)
                            .foregroundColor(Constants.Colors.Background.Fog.dense)
                            .frame(width: 16, height: 16)
                    }
                }
            }
            
            Text(title)
                .font(.custom("PP Neue Montreal", size: 12))
                .fontWeight(.medium)
                .foregroundColor(isOn ? Constants.Colors.Background.primary : Constants.Colors.Foreground.primary)
                .tracking(0.24)
        }
        .padding(.horizontal, 12)
        .padding(.vertical, 8)
        .frame(height: 40)
        .background(
            RoundedRectangle(cornerRadius: 100)
                .fill(isOn ? Color(hex: "#F7F4EF") : Color.clear)
                .overlay(
                    RoundedRectangle(cornerRadius: 100)
                        .stroke(isOn ? Constants.Colors.Border.primary : Constants.Colors.Border.primary, lineWidth: 1)
                )
        )
        .onTapGesture {
            withAnimation(.easeInOut(duration: 0.2)) {
                isOn.toggle()
            }
        }
    }
}


struct ToggleButton_Previews: PreviewProvider {
    static var previews: some View {
        ToggleButton()
            .previewLayout(.sizeThatFits)
    }
}
