//
//  HomeView.swift
//  SodaLive
//
//  Created by klaus on 2023/08/09.
//

import SwiftUI

import Firebase
import Kingfisher

struct HomeView: View {
    
    @StateObject var viewModel = HomeViewModel()
    @StateObject var appState = AppState.shared
    
    private let liveView = LiveView()
    private let explorer = ExplorerView()
    private let messageView = MessageView()
    private let contentView = ContentMainView()
    
    var body: some View {
        GeometryReader { proxy in
            ZStack(alignment: .bottom) {
                VStack(spacing: 0) {
                    ZStack {
                        contentView
                            .frame(width: viewModel.currentTab == .content ? proxy.size.width : 0)
                            .opacity(viewModel.currentTab == .content ? 1.0 : 0.01)
                        
                        liveView
                            .frame(width: viewModel.currentTab == .live ? proxy.size.width : 0)
                            .opacity(viewModel.currentTab == .live ? 1.0 : 0.01)
                        
                        explorer
                            .frame(width: viewModel.currentTab == .explorer ? proxy.size.width : 0)
                            .opacity(viewModel.currentTab == .explorer ? 1.0 : 0.01)
                        
                        messageView
                            .frame(width: viewModel.currentTab == .message ? proxy.size.width : 0)
                            .opacity(viewModel.currentTab == .message ? 1.0 : 0.01)
                        
                        if viewModel.currentTab == .mypage {
                            MyPageView()
                        }
                    }
                    .padding(.bottom, appState.isShowPlayer ? 72 : 0)
                    
                    Spacer()
                    
                    BottomTabView(width: proxy.size.width, currentTab: $viewModel.currentTab)
                    
                    if proxy.safeAreaInsets.bottom > 0 {
                        Rectangle()
                            .foregroundColor(Color(hex: "111111"))
                            .frame(width: proxy.size.width, height: 15.3)
                    }
                }
                .onAppear {
                    pushTokenUpdate()
                    viewModel.getMemberInfo()
                    viewModel.getEventPopup()
                }
                
                if appState.isShowNotificationSettingsDialog {
                    NotificationSettingsDialog()
                }
                
                if let eventItem = appState.eventPopup {
                    VStack(spacing: 0) {
                        Spacer()
                        
                        EventPopupDialogView(eventPopup: eventItem)
                        
                        if proxy.safeAreaInsets.bottom > 0 {
                            Rectangle()
                                .foregroundColor(Color(hex: "222222"))
                                .frame(width: proxy.size.width, height: 15.3)
                        }
                    }
                    .background(Color(hex: "222222").opacity(0.7))
                    .onTapGesture {
                        AppState.shared.eventPopup = nil
                    }
                }
            }
            .edgesIgnoringSafeArea(.bottom)
        }
    }
    
    private func pushTokenUpdate() {
        Messaging.messaging().token { token, error in
            if let error = error {
                DEBUG_LOG(error.localizedDescription)
            } else {
                if let token = token {
                    UserDefaults.set(token, forKey: .pushToken)
                    self.viewModel.pushTokenUpdate(pushToken: token)
                }
            }
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView()
    }
}
