import SwiftUI

struct CustomTabBar: View {
    @Binding var selectedTab: Int
    let isMessengerModeEnabled: Bool
    var onCreate: () -> Void

    var body: some View {
        HStack {
            if isMessengerModeEnabled {

                TabBarButton(systemName: "person.2.fill", text: NSLocalizedString("Контакты", comment: ""), isSelected: selectedTab == 4) {
                    selectedTab = 4
                }

                TabBarButton(systemName: "bubble.left.and.bubble.right.fill", text: NSLocalizedString("Чаты", comment: ""), isSelected: selectedTab == 2) {
                    handleChatsTabTap()
                }

                TabBarButton(systemName: "gearshape.fill", text: NSLocalizedString("Настройки", comment: ""), isSelected: selectedTab == 5) {
                    selectedTab = 5
                }
            } else {
                TabBarButton(systemName: "list.bullet.rectangle", text: NSLocalizedString("Лента", comment: ""), isSelected: selectedTab == 0) {
                    selectedTab = 0
                }

                TabBarButton(systemName: "gamecontroller.fill", text: NSLocalizedString("Концепт", comment: "Tab bar: concept clicker"), isSelected: selectedTab == 1) {
                    selectedTab = 1
                }

                CreateButton {
                    onCreate()
                }

                TabBarButton(systemName: "bubble.left.and.bubble.right.fill", text: NSLocalizedString("Чаты", comment: ""), isSelected: selectedTab == 2) {
                    handleChatsTabTap()
                }

                TabBarButton(systemName: "person.crop.square", text: NSLocalizedString("Лицо", comment: ""), isSelected: selectedTab == 3) {
                    selectedTab = 3
                }
            }
        }
        .padding(.horizontal)
        .padding(.top, isMessengerModeEnabled ? 6 : 1)
        .padding(.bottom, 30) // Добавляем отступ снизу
//        .background(Color(.systemGray6))
    }
}

struct TabBarButton: View {
    let systemName: String
    let text: String
    let isSelected: Bool
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            VStack(spacing: 4) {
                Image(systemName: systemName)
                    .font(.system(size: 22))
                Text(text)
//                    .font(.caption)
                    .font(.system(size: 12))
            }
            .foregroundColor(isSelected ? .accentColor : .gray)
        }
        .frame(maxWidth: .infinity)
    }
}

struct CreateButton: View {
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            Image(systemName: "plus")
                .font(.system(size: 24, weight: .bold))
                .foregroundColor(.white)
                .padding(16)
//                .background(Color.accentColor)
                .background(
                    LinearGradient(
                        gradient: Gradient(colors: [.blue, Color(red: 0.0, green: 0.0, blue: 0.545) ]),
                        startPoint: .top,
                        endPoint: .bottom
                    )
                )
                .clipShape(Circle())
                .shadow(radius: 4)
        }
        .offset(y: -3)
    }
}

private extension CustomTabBar {
    func handleChatsTabTap() {
        if selectedTab == 2 {
            NotificationCenter.default.post(name: .chatsShouldScrollToTop, object: nil)
        } else {
            selectedTab = 2
        }
    }
}
