//
//  ContentView.swift
//  Suno
//
//  Created by Martin Camacho on 1/5/24.
//

import SwiftUI
import AVFoundation
import ClerkSDK


struct SettingsView: View {
    var body: some View {
        VStack {
            UserButton()
        }
        .padding()
    }
}


struct CreateView: View {
    @State private var prompt = "" // State variable to hold the text input
    
    private var networkManager = NetworkManager()
    
    var body: some View {
        VStack {
            Text("Create").font(.title)
            // Text Editor for the text area
            TextEditor(text: $prompt)
                .border(Color.gray, width: 1) // Adds a border to the TextEditor
                .padding()
            
            // Create Button
            Button("Create") {
                // Action to perform when the button is pressed
                createButtonAction()
            }
            .padding()
            .background(Color.orange) // Background color of the button
            .foregroundColor(.white) // Text color of the button
            .cornerRadius(10) // Rounded corners for the button
        }
        .padding()
    }
    
    private func createButtonAction() {
        print("Create button pressed with text: \(prompt)")
        
        Task {
            do {
                let data = try await networkManager.runGenerate(text: prompt)
                print("Data received: \(data)")
                // If you expect a specific data format (like JSON), decode it here
                // For example, decoding JSON into a specific Swift type
                // let decodedData = try JSONDecoder().decode(YourExpectedResponseType.self, from: data)
                // Work with the decoded data
            } catch {
                print("Error: \(error.localizedDescription)")
            }
        }
    }
}

struct CreateButtonView: View {
    @State private var isModalPresented = false
    
    var body: some View {
        Button(action: {
            isModalPresented = true
        }) {
            Image(systemName: "plus.circle.fill")
                .resizable()                  // Makes the image resizable
                .aspectRatio(contentMode: .fit) // Maintains the aspect ratio
                .frame(width: 40, height: 40)
        }
        .sheet(isPresented: $isModalPresented) {
            // Your modal view content
            // You can create a separate SwiftUI view or a custom view here
            CreateView()
                .presentationDetents([.fraction(0.99), .medium])
        }
    }
}

struct TabScreenView: View {
    //enum for Tabs, add other tabs if needed
    enum Tab {
        case home, explore, feed
    }
    
    @StateObject private var playerViewModel = PlayerViewModel() // Shared view model
    @State private var selectedTab: Tab = .home
    
    var body: some View {
        
        ZStack(alignment: .bottom) {
            TabView(selection: $selectedTab) {
                
                FeedView(playerViewModel: playerViewModel)
                    .tabItem {
                        Label("Home", systemImage: "house")
                    }
                    .tag(Tab.home)
                
                ExploreView(playerViewModel:playerViewModel)
                    .tabItem {
                        Label("Explore", systemImage: "globe")
                    }
                    .tag(Tab.explore)
                
                
                SwipeFeedView()
                    .tabItem {
                        Label("Feed", systemImage: "gear")
                    }
                    .tag(Tab.feed)
                    .environmentObject(playerViewModel)
                
//                SettingsView()
//                    .tabItem {
//                        Label("Settings", systemImage: "gear")
//                    }
//                    .tag(Tab.settings)
            }
            PlaybarView(playerViewModel: playerViewModel)
                .padding(.bottom, 50)
            
            CreateButtonView()
                .position(x: UIScreen.main.bounds.width - 100, y: UIScreen.main.bounds.height - 180)
            
        }
    }
    
}

#Preview {
    TabScreenView()
        .environmentObject(Clerk.shared)
}
