import SwiftUI

struct ProfileContentTabbedGrid: View {
    let isContentLoaded: Bool
    @Binding var allPosts: [Post]
    @Binding var isLoading: Bool
    let onPostTapped: (Post, [Post]) -> Void
    @State private var selectedTabIndex = 0
    @State private var selectedCategory = "#все"
    @State private var searchQuery = ""
    @State private var selectedSort = "По дате"

    var body: some View {
        VStack(spacing: 0) {
            // Вкладки профиля
            HStack(spacing: 32) {
                menuTab(index: 0)
                tabButton(index: 1, systemIcon: "lock")
                tabButton(index: 2, systemIcon: "bookmark")
                tabButton(index: 3, systemIcon: "heart")
            }
            .padding(.vertical, 2)

            // Фильтры
            HStack {
                if selectedTabIndex == 0 {
                    Menu {
                        Button("#все") { selectedCategory = "#все" }
                        Button("#влог") { selectedCategory = "#влог" }
                        Button("#игры") { selectedCategory = "#игры" }
                    } label: {
                        Label(selectedCategory, systemImage: "tag")
                            .font(.subheadline)
                            .padding(8)
                            .background(Color.gray.opacity(0.2))
                            .cornerRadius(8)
                    }

                    TextField("Поиск", text: $searchQuery)
                        .padding(.horizontal, 10)
                        .padding(.vertical, 6)
                        .background(Color.gray.opacity(0.15))
                        .cornerRadius(8)
                        .font(.subheadline)

                } else {
                    TextField("Поиск", text: $searchQuery)
                        .padding(.horizontal, 10)
                        .padding(.vertical, 6)
                        .background(Color.gray.opacity(0.15))
                        .cornerRadius(8)
                        .font(.subheadline)
                }
            }
            .padding(.horizontal)
            .padding(.vertical, 12)

            // Контент с табами
            ProfileContentGrid(
                isContentLoaded: isContentLoaded,
                selectedTabIndex: selectedTabIndex,
                searchQuery: searchQuery,
                selectedCategory: selectedCategory,
                allPosts: allPosts,
                isLoading: isLoading,
                onPostTapped: onPostTapped
            )
        }
    }

    // MARK: - Вкладка с меню
    @ViewBuilder
    func menuTab(index: Int) -> some View {
        if selectedTabIndex == index {
            Menu {
                Button("По дате") { selectedSort = "По дате" }
                Button("По популярности") { selectedSort = "По популярности" }
            } label: {
                VStack(spacing: 4) {
                    HStack(spacing: 4) {
                        Image(systemName: "rectangle.grid.3x2")
                            .font(.system(size: 18, weight: .medium))
                            .foregroundColor(.primary)
                        Image(systemName: "chevron.down")
                            .font(.system(size: 10))
                            .foregroundColor(.gray)
                    }
                    Rectangle()
                        .frame(height: 2)
                        .foregroundColor(.primary)
                        .padding(.horizontal)
                }
                .frame(maxWidth: .infinity)
            }
        } else {
            Button {
                selectedTabIndex = index
            } label: {
                VStack(spacing: 4) {
                    HStack(spacing: 4) {
                        Image(systemName: "rectangle.grid.3x2")
                            .font(.system(size: 18, weight: .medium))
                            .foregroundColor(.gray)
                    }
                    Rectangle()
                        .frame(height: 2)
                        .foregroundColor(.clear)
                        .padding(.horizontal)
                }
                .frame(maxWidth: .infinity)
            }
        }
    }

    // MARK: - Остальные вкладки
    @ViewBuilder
    func tabButton(index: Int, systemIcon: String) -> some View {
        VStack(spacing: 4) {
            Image(systemName: systemIcon)
                .font(.system(size: 18, weight: .medium))
                .foregroundColor(selectedTabIndex == index ? .primary : .gray)
            Rectangle()
                .frame(height: 2)
                .foregroundColor(selectedTabIndex == index ? .primary : .clear)
                .padding(.horizontal)
        }
        .frame(maxWidth: .infinity)
        .onTapGesture {
            selectedTabIndex = index
        }
    }
}
