import SwiftUI

struct EditorView: View {
    let lyrics: LyricsStructure
    let songTitle: String
    let currentTime: String
    let totalTime: String
    let progress: Double
    let artworkGradient: LinearGradient
    let isPlaying: Bool
    let onBackTap: () -> Void
    let onPlayPause: () -> Void
    let onPlayerExpand: () -> Void
    let onPlayerClose: () -> Void
    let onSendChatMessage: (String) -> Void
    
    @State private var editableLyrics: String = ""
    
    init(
        lyrics: LyricsStructure,
        songTitle: String = "Summertime (#1)",
        currentTime: String = "0:20",
        totalTime: String = "2:00",
        progress: Double = 0.167,
        artworkGradient: LinearGradient? = nil,
        isPlaying: Bool = true,
        onBackTap: @escaping () -> Void,
        onPlayPause: @escaping () -> Void,
        onPlayerExpand: @escaping () -> Void,
        onPlayerClose: @escaping () -> Void,
        onSendChatMessage: @escaping (String) -> Void
    ) {
        self.lyrics = lyrics
        self.songTitle = songTitle
        self.currentTime = currentTime
        self.totalTime = totalTime
        self.progress = progress
        self.artworkGradient = artworkGradient ?? Self.defaultGradient
        self.isPlaying = isPlaying
        self.onBackTap = onBackTap
        self.onPlayPause = onPlayPause
        self.onPlayerExpand = onPlayerExpand
        self.onPlayerClose = onPlayerClose
        self.onSendChatMessage = onSendChatMessage
        
        // Initialize the editable lyrics state
        self._editableLyrics = State(initialValue: Self.lyricsToString(lyrics))
    }
    
    var body: some View {
        VStack(spacing: 0) {
            // Header
            EditorHeader(
                title: "Edit Lyrics",
                songTitle: songTitle,
                artworkGradient: artworkGradient,
                isPlaying: isPlaying,
                progress: progress,
                onBackTap: onBackTap,
                onPlayPause: onPlayPause,
                onProgressChange: { newProgress in
                    // Handle progress change - could trigger seek in audio player
                    print("Progress changed to: \(newProgress)")
                }
            )
            .background(Constants.Colors.Background.secondary)
            .onTapGesture {
                // Dismiss keyboard when header is tapped
                UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
            }
            
            // Main content - Simple lyrics text field
            TextField("Edit lyrics here", text: $editableLyrics, axis: .vertical)
                .font(Constants.Typography.mediumRegular)
                .foregroundColor(Constants.Colors.Foreground.primary)
                .textFieldStyle(PlainTextFieldStyle())
                .padding(.horizontal, 16)
                .padding(.top, 16)
                .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
            
            // Footer
            EditorFooter(
                buttonTitle: "Create new version",
                buttonIconName: "Icon/create",
                onButtonTap: {
                    // Create chat message for new version request
                    let chatMessage = "Can you create a new version of \(songTitle) with the following lyrics:\n\n\(editableLyrics)"
                    onSendChatMessage(chatMessage)
                    // Close the editor
                    onBackTap()
                }
            )
            .background(Constants.Colors.Background.primary)
        }
        .background(Constants.Colors.Background.primary)
    }
    
    private static var defaultGradient: LinearGradient {
        LinearGradient(
            colors: [
                Color(red: 0.8, green: 0.4, blue: 0.9),
                Color(red: 0.4, green: 0.6, blue: 1.0)
            ],
            startPoint: .topLeading,
            endPoint: .bottomTrailing
        )
    }
    
    private static func lyricsToString(_ lyrics: LyricsStructure) -> String {
        return lyrics.sections.map { section in
            "[\(section.type)]\n\(section.content)"
        }.joined(separator: "\n\n")
    }
}

#Preview {
    let sampleLyrics = LyricsStructure(sections: [
        LyricsSection(type: "Chorus", content: "My friend, my friend, a heart of gold\nA story whispered, never getting old\nThrough stormy weather, you're always there\nA bond unbreakable, beyond compare"),
    ])
    
    EditorView(
        lyrics: sampleLyrics,
        songTitle: "Summertime (#1)",
        currentTime: "0:20",
        totalTime: "2:00",
        progress: 0.167,
        isPlaying: true,
        onBackTap: {
            print("Back tapped")
        },
        onPlayPause: {
            print("Play/Pause tapped")
        },
        onPlayerExpand: {
            print("Player expand tapped")
        },
        onPlayerClose: {
            print("Player close tapped")
        },
        onSendChatMessage: { message in
            print("Send chat message: \(message)")
        }
    )
}
