//
//  ContentBoxView.swift
//  SodaLive
//
//  Created by klaus on 12/7/24.
//

import SwiftUI

struct ContentBoxView: View {
    
    @StateObject var viewModel = ContentBoxViewModel()
    
    @State private var isShowDetailPlaylist = false
    @State private var isShowCreatePlaylist = false
    @State private var isReloadData = false
    
    @State private var selectedPlaylistId = 0
    
    let initCurrentTab: ContentBoxViewModel.CurrentTab
    
    var body: some View {
        ZStack {
            NavigationView {
                VStack(spacing: 13.3) {
                    DetailNavigationBar(title: "내 보관함")
                    
                    ScrollView(.horizontal, showsIndicators: false) {
                        HStack(spacing: 12) {
                            ContentBoxTabView(
                                title: "구매목록",
                                isSelected: viewModel.currentTab == .orderlist
                            )
                            .onTapGesture {
                                if viewModel.currentTab != .orderlist {
                                    viewModel.currentTab = .orderlist
                                }
                            }
                            
                            ContentBoxTabView(
                                title: "재생목록",
                                isSelected: viewModel.currentTab == .playlist
                            )
                            .onTapGesture {
                                if viewModel.currentTab != .playlist {
                                    viewModel.currentTab = .playlist
                                }
                            }
                        }
                        .padding(.horizontal, 13.3)
                    }
                    
                    if viewModel.currentTab == .playlist {
                        if isReloadData {
                            Color.clear
                                .onAppear {
                                    DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
                                        isReloadData = false
                                    }
                                }
                        } else {
                            ContentPlaylistListView(
                                onClickCreate: { isShowCreatePlaylist = true },
                                onClickItem: { playlistId in
                                    selectedPlaylistId = playlistId
                                    isShowDetailPlaylist = true
                                }
                            )
                            .padding(.bottom, 13.3)
                            .padding(.horizontal, 13.3)
                        }
                    } else {
                        OrderListAllInnerView()
                    }
                }
                .onAppear {
                    viewModel.currentTab = initCurrentTab
                }
            }
            
            if isShowCreatePlaylist {
                ContentPlaylistCreateView(
                    isShowing: $isShowCreatePlaylist,
                    reloadData: $isReloadData
                )
            }
            
            if isShowDetailPlaylist {
                ContentPlaylistDetailView(
                    playlistId: selectedPlaylistId,
                    isShowing: $isShowDetailPlaylist,
                    reloadData: $isReloadData
                )
            }
        }
    }
}

#Preview {
    ContentBoxView(initCurrentTab: .orderlist)
}
