//
//  SongLibraryView.swift
//  vibes
//
//  Song library list view (Version 3)
//

import SwiftUI

struct SongLibraryView: View {
    let songs: [(albumArt: String, name: String)]
    @Binding var showNewSongAnimation: Bool
    var onCreateNew: () -> Void = {}
    @State private var previousSongCount: Int = 0

    // MARK: - Light Mode Color Overrides
    private struct LightColors {
        static let backgroundPrimary = Color.white
        static let backgroundSecondary = Color(hex: "#f7f4ef")  // Light beige
        static let foregroundPrimary = Color(hex: "#101012")    // Dark/black
        static let foregroundSecondary = Color(hex: "#5b5b62")  // Medium gray
        static let borderPrimary = Color.black.opacity(0.10)
        static let accentOrange = Color(hex: "#ff6a00")
    }

    private var isNewSongAtTop: Bool {
        songs.count > previousSongCount
    }

    var body: some View {
        VStack(spacing: 0) {
            // Title at the top
            HStack {
                Text("Library")
                    .font(.system(size: 34, weight: .bold))
                    .foregroundColor(LightColors.foregroundPrimary)

                Spacer()

                // Create new button
                Button(action: {
                    onCreateNew()
                }) {
                    Image(systemName: "plus")
                        .font(.system(size: 18, weight: .semibold))
                        .foregroundColor(LightColors.foregroundPrimary)
                        .frame(width: 36, height: 36)
                        .background(Circle().fill(LightColors.backgroundSecondary))
                        .overlay(
                            Circle()
                                .stroke(LightColors.borderPrimary, lineWidth: 1)
                        )
                }
            }
            .padding(.horizontal, 20)
            .padding(.top, 60)
            .padding(.bottom, 16)
            .background(LightColors.backgroundPrimary)

            // Song list
            ScrollView {
                LazyVStack(spacing: 16) {
                    ForEach(Array(songs.enumerated()), id: \.element.name) { index, song in
                        HStack(spacing: 12) {
                            // Album art
                            if let uiImage = UIImage(named: song.albumArt) {
                                Image(uiImage: uiImage)
                                    .resizable()
                                    .scaledToFill()
                                    .frame(width: 60, height: 60)
                                    .clipShape(RoundedRectangle(cornerRadius: 8))
                            } else {
                                RoundedRectangle(cornerRadius: 8)
                                    .fill(LightColors.backgroundSecondary)
                                    .frame(width: 60, height: 60)
                            }

                            // Song name
                            Text(song.name)
                                .font(.system(size: 17, weight: .semibold))
                                .foregroundColor(LightColors.foregroundPrimary)

                            Spacer()
                        }
                        .padding(.horizontal, 20)
                        .contentShape(Rectangle())
                        .blur(radius: (index == 0 && isNewSongAtTop && !showNewSongAnimation) ? 10 : 0)
                        .animation(.easeOut(duration: 0.6), value: showNewSongAnimation)
                        .transition(.asymmetric(
                            insertion: .move(edge: .top).combined(with: .opacity),
                            removal: .opacity
                        ))
                    }
                }
                .padding(.vertical, 16)
            }
            .background(LightColors.backgroundPrimary)
        }
        .background(LightColors.backgroundPrimary)
        .ignoresSafeArea(edges: .top)
        .preferredColorScheme(.light)
        .onAppear {
            previousSongCount = songs.count
        }
        .onChange(of: songs.count) { oldCount, newCount in
            if newCount > oldCount {
                previousSongCount = oldCount
                // Reset after animation completes
                DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                    previousSongCount = newCount
                }
            }
        }
    }
}

#Preview {
    SongLibraryView(
        songs: [
            ("cry cried crying.png", "Midnight Tears"),
            ("crystal healing.png", "Crystal Waves"),
            ("daylight.png", "Morning Light")
        ],
        showNewSongAnimation: .constant(false),
        onCreateNew: {}
    )
}
