import SwiftUI

struct MainView: View {
    @ObservedObject var viewModel: LoginViewModel
    @State private var selectedTab: Int = 0
    @StateObject private var newHomeTabViewModel = NewHomeTabViewModel()
    
    // Состояния для TopBarView
    @State private var selectedAccount = "@user1"
    @State private var accounts = ["@user1", "@user2", "@user3"]
    @State private var sheetType: ProfileTab.SheetType? = nil
    
    // Состояние для бокового меню
    @State private var isSideMenuPresented = false
    
    private var tabTitle: String {
        switch selectedTab {
        case 0: return "Home"
        case 1: return "Search"
        case 2: return "Chats"
        case 3: return "Profile"
        default: return "Home"
        }
    }

    var body: some View {
        NavigationView {
            ZStack {
                // Основной контент
                VStack(spacing: 0) {
                    TopBarView(
                        title: tabTitle,
                        selectedAccount: $selectedAccount,
                        sheetType: $sheetType,
                        accounts: accounts,
                        viewModel: viewModel,
                        isSideMenuPresented: $isSideMenuPresented
                    )
                    
                    ZStack {
                        NewHomeTab(viewModel: newHomeTabViewModel)
                            .opacity(selectedTab == 0 ? 1 : 0)
                        
                        SearchTab()
                            .opacity(selectedTab == 1 ? 1 : 0)
                        
                        ChatsTab()
                            .opacity(selectedTab == 2 ? 1 : 0)
                        
                        ProfileTab(viewModel: viewModel, sheetType: $sheetType, selectedAccount: $selectedAccount, accounts: $accounts, onScroll: { _ in })
                            .opacity(selectedTab == 3 ? 1 : 0)
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    
                    CustomTabBar(selectedTab: $selectedTab) {
                        print("Create button tapped")
                    }
                }
                .ignoresSafeArea(edges: .bottom)
                .navigationBarHidden(true)
                .sheet(item: $sheetType) { type in
                    // ... sheet presentation logic
                }
                // Затемнение фона при открытом меню
                .background(isSideMenuPresented ? Color.black.opacity(0.4) : Color.clear)
                .onTapGesture {
                    if isSideMenuPresented {
                        withAnimation {
                            isSideMenuPresented = false
                        }
                    }
                }

                // Боковое меню
                if isSideMenuPresented {
                    HStack {
                        SideMenuView(isPresented: $isSideMenuPresented)
                            .frame(width: UIScreen.main.bounds.width * 0.8)
                        Spacer()
                    }
                    .ignoresSafeArea(edges: .vertical) // Игнорируем safe area
                    .transition(.move(edge: .leading))
                    .zIndex(1) // Убедимся, что меню поверх всего
                }
            }
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct MainView_Previews: PreviewProvider {
    static var previews: some View {
        let mockViewModel = LoginViewModel()
        MainView(viewModel: mockViewModel)
    }
}
