//
//  SeriesContentAllView.swift
//  SodaLive
//
//  Created by klaus on 4/30/24.
//

import SwiftUI

struct SeriesContentAllView: View {
    
    @ObservedObject var viewModel = SeriesContentAllViewModel()
    
    let seriesId: Int
    let seriesTitle: String
    
    var body: some View {
        VStack(spacing: 0) {
            BaseView(isLoading: $viewModel.isLoading) {
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "\(seriesTitle) - 전체회차 듣기")
                    
                    HStack(spacing: 13.3) {
                        Spacer()
                        
                        Text("최신순")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(
                                Color.graye2
                                    .opacity(viewModel.sortType == .NEWEST ? 1 : 0.5)
                            )
                            .onTapGesture {
                                if viewModel.sortType != .NEWEST {
                                    viewModel.sortType = .NEWEST
                                }
                            }
                        
                        Text("등록순")
                            .font(.custom(Font.medium.rawValue, size: 13.3))
                            .foregroundColor(
                                Color.graye2
                                    .opacity(viewModel.sortType == .OLDEST ? 1 : 0.5)
                            )
                            .onTapGesture {
                                if viewModel.sortType != .OLDEST {
                                    viewModel.sortType = .OLDEST
                                }
                            }
                    }
                    .padding(.vertical, 13.3)
                    .padding(.horizontal, 20)
                    .background(Color.gray16)
                    
                    ScrollView(.vertical, showsIndicators: false) {
                        LazyVStack(spacing: 12) {
                            ForEach(0..<viewModel.seriesContentList.count, id: \.self) { index in
                                let item = viewModel.seriesContentList[index]
                                
                                SeriesContentListItemView(item: item)
                                    .contentShape(Rectangle())
                                    .onTapGesture {
                                        AppState.shared
                                            .setAppStep(step: .contentDetail(contentId: item.contentId))
                                    }
                                    .onAppear {
                                        if index == viewModel.seriesContentList.count - 1 {
                                            viewModel.getSeriesContentList()
                                        }
                                    }
                            }
                        }
                        .padding(13.3)
                        .padding(.top, 12)
                    }
                }
            }
        }
        .onAppear {
            viewModel.seriesId = seriesId
            viewModel.getSeriesContentList()
        }
    }
}
