import SwiftUI

// Slider for OrpheusCustomCreate
public struct CustomSliderV2: View {
    let title: String
    let infoPopoverText: String
    @Binding var sliderValue: Double
    let warningThreshold: Double?
    
    @State private var isDragging = false
    @State private var infoPopoverIsPresented = false
    private let barCount = 11
    
    public init(title: String, infoPopoverText: String, sliderValue: Binding<Double>, warningThreshold: Double? = nil) {
        self.title = title
        self.infoPopoverText = infoPopoverText
        self._sliderValue = sliderValue
        self.warningThreshold = warningThreshold
    }
    
    public var body: some View {
        HStack(spacing: 24) {
            HStack(spacing: 4) {
                Text(title)
                    .figmaMCPTypography(TypographyV1.FigmaMCP.xSmallTitle)
                    .foregroundColor(Color.FigmaMCP.Semantic.foregroundPrimary)
                    .tracking(0.24)
                
                Button {
                    infoPopoverIsPresented = true
                } label: {
                    Image.FigmaMCP.info
                        .figmaMCPIconStyle(size: 16, semanticColor: Color.FigmaMCP.Semantic.fogDense)
                        .frame(width: 16, height: 16)
                }
                .buttonStyle(PlainButtonStyle())
                .popover(
                    present: $infoPopoverIsPresented,
                    attributes: {
                        $0.position = .absolute(originAnchor: .top, popoverAnchor: .bottom)
                        $0.presentation.animation = .snappy.speed(2)
                        $0.dismissal.animation = .snappy.speed(2)
                    }
                ) {
                    SimpleTooltipView(text: infoPopoverText)
                }
            }
            .frame(width: 120, alignment: .leading)
            
            ZStack {
                HStack(spacing: 0) {
                    ForEach(0..<barCount, id: \.self) { index in
                        let barOpacity = getBarOpacity(for: index)
                        RoundedRectangle(cornerRadius: 100)
                            .fill(Color.FigmaMCP.Semantic.foregroundPrimary.opacity(barOpacity))
                            .frame(width: 2, height: 24)
                        
                        if index < barCount - 1 {
                            Spacer()
                        }
                    }
                }
                
                GeometryReader { geometry in
                    let thumbWidth: CGFloat = 10
                    let thumbPosition = geometry.size.width * sliderValue
                    
                    RoundedRectangle(cornerRadius: 100)
                        .fill(Color.FigmaMCP.Semantic.accentBrand)
                        .frame(
                            width: isDragging ? thumbWidth * 1.8 : thumbWidth,
                            height: isDragging ? 32 : 24
                        )
                        .shadow(color: .black.opacity(0.4), radius: 6, x: 0, y: 4)
                        .animation(.easeInOut(duration: 0.15), value: isDragging)
                        .position(x: thumbPosition, y: geometry.size.height / 2)
                        .gesture(
                            DragGesture(minimumDistance: 0)
                                .onChanged { gesture in
                                    if !isDragging {
                                        withAnimation(.easeInOut(duration: 0.15)) {
                                            isDragging = true
                                        }
                                    }
                                    
                                    let fingerX = gesture.location.x
                                    let rawValue = max(0, min(1, fingerX / geometry.size.width))
                                    let percentValue = round(rawValue * 100) / 100
                                    sliderValue = percentValue
                                }
                                .onEnded { _ in
                                    withAnimation(.easeInOut(duration: 0.15)) {
                                        isDragging = false
                                    }
                                }
                        )
                }
            }
            .frame(height: 24)
            
            Text("\(Int(sliderValue * 100))%")
                .figmaMCPTypography(TypographyV1.FigmaMCP.xSmallTitle)
                .foregroundColor(warningThreshold != nil && sliderValue > warningThreshold! ? Color.FigmaMCP.Semantic.accentError : Color.FigmaMCP.Semantic.foregroundPrimary)
                .tracking(0.24)
                .frame(width: 40, alignment: .trailing)
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 12)
        .background(Color.FigmaMCP.Semantic.backgroundPrimary)
        .clipShape(RoundedRectangle(cornerRadius: 12))
    }
    
    private func getBarOpacity(for index: Int) -> Double {
        let position = Double(index) / Double(barCount - 1)
        let distance = abs(position - sliderValue)
        
        if distance <= 0.1 {
            return 0.3
        } else if distance <= 0.2 {
            return 0.15
        } else if distance <= 0.3 {
            return 0.1
        } else {
            return 0.05
        }
    }
}

