//
//  LiveReservationAllView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/14.
//

import SwiftUI

struct LiveReservationAllView: View {
    
    @ObservedObject var viewModel = LiveViewModel()
    @StateObject var liveAllViewModel = LiveAllViewModel()
    
    let onClickReservation: (Int) -> Void
    let onClickStart: (Int) -> Void
    let onClickCancel: () -> Void
    let onTapCreateLive: () -> Void
    
    var body: some View {
        BaseView(isLoading: $viewModel.isLoading) {
            GeometryReader { proxy in
                VStack(spacing: 0) {
                    DetailNavigationBar(title: "라이브, 예약 캘린더")
                    
                    WeekCalendarView { date in
                        viewModel.selectedDateString = date
                    }
                    .padding(.top, 20)
                    
                    if viewModel.liveReservationItems.count > 0 {
                        ScrollView(.vertical, showsIndicators: false) {
                            LazyVStack(spacing: 13.3) {
                                ForEach(0..<viewModel.liveReservationItems.count, id: \.self) { index in
                                    let item = viewModel.liveReservationItems[index]
                                    
                                    LiveReservationAllItemView(item: item)
                                        .contentShape(Rectangle())
                                        .onTapGesture {
                                            self.liveAllViewModel.selectedRoomId = item.roomId
                                            withAnimation {
                                                self.liveAllViewModel.isShowLiveDetail = true
                                            }
                                        }
                                        .onAppear {
                                            if index == viewModel.liveNowItems.count - 1 {
                                                viewModel.getLiveReservationList()
                                            }
                                        }
                                }
                            }
                        }
                        .padding(.vertical, 16.7)
                    } else {
                        VStack(spacing: 0) {
                            Image("ic_no_item")
                                .resizable()
                                .frame(width: 60, height: 60)
                            
                            Text("마이페이지에서 본인인증을 하거나\n다른 날짜의 라이브를 예약하고 참여해 보세요")
                                .font(.custom(Font.medium.rawValue, size: 13))
                                .foregroundColor(Color(hex: "bbbbbb"))
                                .multilineTextAlignment(.center)
                                .lineSpacing(8)
                                .padding(.top, 8)
                            
                            HStack(spacing: 0) {
                                Image("ic_plus_no_bg")
                                    .resizable()
                                    .frame(width: 33.3, height: 33.3, alignment: .center)
                                
                                Text("라이브 만들기")
                                    .font(.custom(Font.bold.rawValue, size: 13.3))
                                    .foregroundColor(Color.white)
                            }
                            .frame(width: 200, height: 33.3, alignment: .center)
                            .background(Color(hex: "3bb9f1"))
                            .cornerRadius(4.7)
                            .padding(.top, 10.7)
                            .onTapGesture {
                                AppState.shared.back()
                                onTapCreateLive()
                            }
                        }
                        .padding(.vertical, 16.7)
                        .frame(width: screenSize().width - 26.7)
                        .background(Color(hex: "13181b"))
                        .cornerRadius(4.7)
                        .padding(.top, 28.3)
                    }
                    
                    if proxy.safeAreaInsets.bottom > 0 {
                        Rectangle()
                            .foregroundColor(Color.black.opacity(0))
                            .frame(width: screenSize().width, height: 15.3)
                    }
                }
                .edgesIgnoringSafeArea(.bottom)
            }
            
            if liveAllViewModel.isShowLiveDetail {
                LiveDetailView(
                    roomId: liveAllViewModel.selectedRoomId,
                    onClickParticipant: {},
                    onClickReservation: {
                        onClickReservation(liveAllViewModel.selectedRoomId)
                    },
                    onClickStart: {
                        onClickStart(liveAllViewModel.selectedRoomId)
                    },
                    onClickCancel: {
                        viewModel.page = 1
                        viewModel.isLast = false
                        viewModel.getLiveReservationList()
                        onClickCancel()
                    },
                    onClickClose: {
                        withAnimation {
                            liveAllViewModel.isShowLiveDetail = false
                        }
                    }
                )
            }
        }
    }
}
