//
//  RemixExtendView.swift
//  vibes
//
//  Created by Yamill Vallecillo on 8/15/25.
//

import SwiftUI

struct RemixExtendView: View {
    @Binding var isPresented: Bool
    @State private var lyricsText: String = ""
    @State private var styleText: String = "electronic, pop, rock"
    @State private var extensionTime: String = "0:30s"
    @State private var isKeyboardVisible: Bool = false
    
    let originalTrackTitle: String
    let onBackTap: () -> Void
    
    init(isPresented: Binding<Bool> = .constant(true), originalTrackTitle: String = "Nocturnal Apparition", onBackTap: @escaping () -> Void = {}) {
        self._isPresented = isPresented
        self.originalTrackTitle = originalTrackTitle
        self.onBackTap = onBackTap
    }
    
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                VStack(spacing: 0) {
                    // Grabber
                    Grabber()
                    
                    // Header
                    HeaderRemix(
                        remixType: "Extend",
                        onBackTap: {
                            isPresented = false
                            onBackTap()
                        }
                    )
                    
                    ScrollView {
                        VStack(spacing: 16) {
                            // Create Extend Player
                            CreateExtendPlayer(
                                onExtensionTimeChanged: { timeString in
                                    extensionTime = timeString
                                }
                            )
                            
                            // Lyrics Section (Expanded)
                            LyricsDescription(
                                lyricsText: $lyricsText,
                                startExpanded: true,
                                placeholderText: "Continue the song — what lyrics come after\n\(extensionTime)"
                            )
                            
                            // Styles Section (Collapsed, Pre-filled)
                            StyleDescription(
                                styleText: $styleText,
                                startExpanded: false
                            )
                            
                            // Advanced Options Section (Collapsed)  
                            AdvancedOptions()
                            
                            // Extra padding at bottom to account for floating create button
                            Spacer()
                                .frame(height: 120)
                        }
                        .padding(16)
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                }
                
                // Floating Create Button with Gradient Background
                CreateFooter(
                    isVisible: true,
                    isKeyboardVisible: isKeyboardVisible,
                    createButtonTitle: "Create",
                    createButtonIcon: "Icon/create"
                ) {
                    // Handle create extend action
                    print("Create extend button tapped")
                }
            }
        }
        .ignoresSafeArea(.container, edges: .bottom)
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillShowNotification)) { _ in
            isKeyboardVisible = true
        }
        .onReceive(NotificationCenter.default.publisher(for: UIResponder.keyboardWillHideNotification)) { _ in
            isKeyboardVisible = false
        }
    }
}

#Preview {
    RemixExtendView(isPresented: .constant(true))
        .preferredColorScheme(.dark)
}