import SwiftUI

struct ProfileTab: View {
    @ObservedObject var viewModel: LoginViewModel
    @State private var isContentLoaded = true
    
    // Привязки к состояниям в MainView
    @Binding var sheetType: SheetType?
    @Binding var selectedAccount: String
    @Binding var accounts: [String]
    
    var onScroll: ((CGPoint) -> Void)?

    let followers = ["@alice", "@bob", "@charlie"]
    let following = ["@dev", "@design", "@ios"]
    
    enum SheetType: Identifiable {
        case accountShare
        var id: Int { self.hashValue }
    }

    @State private var allPosts: [Post] = []
    @State private var isLoading = true
    @State private var isRefreshing = false

    @State private var selectedPostData: (Post, [Post])?

    @State private var isShowingFollowers = false
    @State private var isShowingFollowing = false

    var body: some View {
        VStack {
            if !isContentLoaded {
                SplashScreenView()
            } else {
                VStack(spacing: 0) {
                    // Скрытые NavigationLink для программного перехода
                    NavigationLink(destination: FollowersView(followers: followers), isActive: $isShowingFollowers) { EmptyView() }
                    NavigationLink(destination: FollowingView(following: following), isActive: $isShowingFollowing) { EmptyView() }
                    
                    if let (post, posts) = selectedPostData {
                        NavigationLink(
                            destination: PostFeedView(posts: posts, selectedPostID: post.id),
                            isActive: Binding(
                                get: { selectedPostData != nil },
                                set: { if !$0 { selectedPostData = nil } }
                            ),
                            label: { EmptyView() }
                        )
                    }

                    RefreshableScrollView(
                        isRefreshing: $isRefreshing,
                        onRefresh: { fetchData() },
                        onScroll: onScroll
                    ) {
                        VStack(spacing: 12) {
                            header
                                .frame(maxWidth: .infinity)
                                .background(Color(.systemBackground))
                            
                            ProfileContentTabbedGrid(
                                isContentLoaded: isContentLoaded,
                                allPosts: $allPosts,
                                isLoading: $isLoading,
                                onPostTapped: { post, posts in
                                    self.selectedPostData = (post, posts)
                                }
                            )
                        }
                    }
                }
            }
        }
        .onAppear {
            if allPosts.isEmpty {
                fetchData(isInitialLoad: true)
            }
        }
    }

    private func fetchData(isInitialLoad: Bool = false) {
        if isInitialLoad {
            isLoading = true
        } else {
            isRefreshing = true
        }
        
        PostService.shared.fetchAllPosts { fetchedPosts in
            self.allPosts = fetchedPosts
            
            if isInitialLoad {
                self.isLoading = false
            }
            self.isRefreshing = false
        }
    }

    // MARK: - Шапка профиля
    private var header: some View {
        VStack(spacing: 12) {
            // Аватар и имя
            VStack(spacing: 6) {
                Image(systemName: "person.crop.circle.fill")
                    .resizable()
                    .frame(width: 72, height: 72)
                    .foregroundColor(.gray)

                Text("custom_user_name")
                    .font(.headline)
            }
            .padding(.top, 16)

            Text("iOS разработчик, делаю интерфейсы, иногда снимаю блоги и делюсь кодом. Всегда рад новым подписчикам и интересным проектам.")
                .font(.subheadline)
                .foregroundColor(.secondary)
                .multilineTextAlignment(.center)
                .padding(.horizontal)

            // Статистика
            HStack(spacing: 32) {
                statView("24", "Посты")
                Button(action: { isShowingFollowers = true }) {
                    statView("1.2k", "Подписчики")
                }
                Button(action: { isShowingFollowing = true }) {
                    statView("156", "Подписки")
                }
            }
        }
        .padding(.horizontal)
    }

    // MARK: - Статистика
    func statView(_ value: String, _ label: String) -> some View {
        VStack {
            Text(value)
                .font(.headline)
            Text(label)
                .font(.caption)
        }
        .foregroundColor(.primary)
    }
}
