//
//  SongView.swift
//  Suno
//
//  Created by Martin Camacho on 1/7/24.
//

import Foundation
import SwiftUI


struct SongView: View {
    var clip: Clip
    @Environment(\.dismiss) var dismiss
    
    var body: some View {
        ScrollView {
            VStack(alignment:.center) {
                Button("Back to Main") {
                    dismiss()
                    
                }
                
                if let urlString = clip.imageUrl, let imageUrl = URL(string: urlString) {
                    AsyncImage(url: imageUrl) { image in
                        image.resizable()
                    } placeholder: {
                        ProgressView() // Show a progress indicator while loading
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .aspectRatio(contentMode: .fit)
                    .clipped()
                    .cornerRadius(10)
                }
                Text(clip.title ?? "Untitled")
                    .font(.title)
                    .fontWeight(.bold)
                    .foregroundStyle(Color.white)
                Text(clip.metadata.prompt ?? "No lyrics")
                    .foregroundStyle(Color.white)
                    .multilineTextAlignment(.leading)
            }
        }
        .padding()
    }
}


struct SongView_Previews: PreviewProvider {
    static var previews: some View {
        let clip = Clip(
            id: UUID(uuidString: "76D08F0A-6FB1-41E2-B0CB-12741AA2CCAB") ?? UUID(),
            audioUrl: "https://cdn1.suno.ai/76d08f0a-6fb1-41e2-b0cb-12741aa2ccab.mp3",
            title: "Celestial Dreams",
            imageUrl: "https://cdn1.suno.ai/image_76d08f0a-6fb1-41e2-b0cb-12741aa2ccab.png",
            metadata: ClipMetadata(
                tags: "pop",
                prompt: "[Verse]\nIn the darkness of the night, I look up to the sky\nMysterious lights shining bright, catching my eye (ooh)\nGazing at the moon, wishing upon a star\nLost in the wonders, wondering how far (ooh-yeah)\n\n[Chorus]\nCelestial dreams, taking me away (away)\nTo a world unknown, where fantasies play (play)\nFloating through galaxies, weightless and free\nIn celestial dreams, where I wanna be (ooh, yeah, yeah)"
            )
        )
        
        return SongView(clip: clip)
            .background(Color.black)
            .previewLayout(.sizeThatFits)
    }
}
