import SwiftUI

// MARK: - Refreshable ScrollView with load-more

struct ZXRefreshableScrollView<Content: View>: View {
    let onRefresh: () async -> Void
    let onLoadMore: (() async -> Void)?
    let hasMore: Bool
    let content: () -> Content

    init(
        onRefresh: @escaping () async -> Void,
        onLoadMore: (() async -> Void)? = nil,
        hasMore: Bool = false,
        @ViewBuilder content: @escaping () -> Content
    ) {
        self.onRefresh = onRefresh
        self.onLoadMore = onLoadMore
        self.hasMore = hasMore
        self.content = content
    }

    @State private var isRefreshing = false

    var body: some View {
        ScrollView {
            // Pull-to-refresh anchor
            if isRefreshing {
                VStack(spacing: 8) {
                    ZXLoadingView(size: 28, lineWidth: 2.5)
                    Text("刷新中…")
                        .font(.system(size: 12, weight: .medium))
                        .foregroundColor(Color.zxF04)
                }
                .frame(maxWidth: .infinity)
                .padding(.top, 8)
                .padding(.bottom, 4)
            }

            content()

            // Load-more footer
            if let onLoadMore, hasMore {
                ZXLoadMoreFooter(action: onLoadMore)
            }
        }
        .scrollIndicators(.hidden)
        .refreshable {
            await onRefresh()
        }
    }
}

// MARK: - Load-more footer

struct ZXLoadMoreFooter: View {
    let action: () async -> Void

    @State private var isLoading = false

    var body: some View {
        HStack(spacing: 10) {
            if isLoading {
                ZXLoadingView(size: 20, lineWidth: 2)
                Text("加载中…")
                    .font(.system(size: 13))
                    .foregroundColor(Color.zxF04)
            } else {
                Text("上拉加载更多")
                    .font(.system(size: 13))
                    .foregroundColor(Color.zxF04)
            }
        }
        .frame(maxWidth: .infinity)
        .padding(.vertical, 20)
        .padding(.bottom, 80)
        .task {
            isLoading = true
            await action()
            isLoading = false
        }
    }
}

// MARK: - Pull-to-refresh modifier (for plain ScrollView)

struct ZXPullToRefreshModifier: ViewModifier {
    let onRefresh: () async -> Void
    @State private var isRefreshing = false

    func body(content: Content) -> some View {
        VStack(spacing: 0) {
            if isRefreshing {
                HStack(spacing: 10) {
                    ZXLoadingView(size: 22, lineWidth: 2)
                    Text("正在刷新…")
                        .font(.system(size: 13, weight: .medium))
                        .foregroundColor(Color.zxF04)
                }
                .frame(maxWidth: .infinity)
                .padding(.vertical, 10)
                .transition(.move(edge: .top).combined(with: .opacity))
            }
            content
        }
        .animation(.easeInOut(duration: 0.25), value: isRefreshing)
        .refreshable {
            isRefreshing = true
            await onRefresh()
            isRefreshing = false
        }
    }
}

extension View {
    func zxPullToRefresh(_ action: @escaping () async -> Void) -> some View {
        modifier(ZXPullToRefreshModifier(onRefresh: action))
    }
}
