import SwiftUI
import MusicModel

/// Part colors shared across mixer chips and (later) note tinting.
enum PartPalette {
    static let colors: [Color] = [.blue, .green, .orange, .purple, .pink, .teal, .indigo, .brown]
    static func color(_ index: Int) -> Color {
        colors[index % colors.count]
    }
}

/// One chip per part: record, mute/solo, take status, detail sheet.
struct MixerStrip: View {
    @Bindable var model: ProjectModel
    var onPartDetail: (String) -> Void

    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 10) {
                ForEach(model.score.parts, id: \.name) { part in
                    PartChip(
                        model: model,
                        partName: part.name,
                        shortName: part.shortName,
                        color: PartPalette.color(part.colorIndex),
                        isRecordingThis: model.recordPhase == .recording(partName: part.name),
                        onDetail: { onPartDetail(part.name) }
                    )
                }
            }
            .padding(.horizontal, 12)
            .padding(.vertical, 8)
        }
    }
}

private struct PartChip: View {
    @Bindable var model: ProjectModel
    let partName: String
    let shortName: String
    let color: Color
    let isRecordingThis: Bool
    var onDetail: () -> Void

    private var isMuted: Bool { model.muted.contains(partName) }
    private var isSoloed: Bool { model.soloed.contains(partName) }

    var body: some View {
        VStack(spacing: 6) {
            HStack(spacing: 6) {
                Circle()
                    .fill(color)
                    .frame(width: 10, height: 10)
                Text(partName)
                    .font(.subheadline.weight(.medium))
                    .lineLimit(1)
                if model.hasActiveTake(partName) {
                    Image(systemName: "waveform.circle.fill")
                        .font(.caption)
                        .foregroundStyle(color)
                }
            }
            .contentShape(Rectangle())
            .onTapGesture(perform: onDetail)

            HStack(spacing: 8) {
                // Record
                Button {
                    if isRecordingThis {
                        model.stop()
                    } else {
                        model.startRecording(partName: partName)
                    }
                } label: {
                    Image(systemName: isRecordingThis ? "stop.circle.fill" : "record.circle")
                        .font(.title3)
                        .foregroundStyle(isRecordingThis ? .red : (model.canRecord ? Color.red : .gray))
                }
                .disabled(!model.canRecord && !isRecordingThis)

                // Mute
                Button {
                    if isMuted { model.muted.remove(partName) } else { model.muted.insert(partName) }
                } label: {
                    Text("M")
                        .font(.caption.bold())
                        .frame(width: 26, height: 26)
                        .background(isMuted ? Color.red.opacity(0.8) : Color(.systemGray5))
                        .foregroundStyle(isMuted ? .white : .primary)
                        .clipShape(Circle())
                }

                // Solo
                Button {
                    if isSoloed { model.soloed.remove(partName) } else { model.soloed.insert(partName) }
                } label: {
                    Text("S")
                        .font(.caption.bold())
                        .frame(width: 26, height: 26)
                        .background(isSoloed ? Color.yellow.opacity(0.9) : Color(.systemGray5))
                        .foregroundStyle(isSoloed ? .black : .primary)
                        .clipShape(Circle())
                }
            }
        }
        .padding(.horizontal, 10)
        .padding(.vertical, 6)
        .background(
            RoundedRectangle(cornerRadius: 10)
                .fill(isRecordingThis ? color.opacity(0.18) : Color(.systemGray6))
        )
        .overlay(
            RoundedRectangle(cornerRadius: 10)
                .strokeBorder(isRecordingThis ? Color.red : .clear, lineWidth: 1.5)
        )
        .buttonStyle(.plain)
    }
}
