//
//  PrivacyDetailView.swift
//  punchnet
//
//  Created by 安礼成 on 2026/3/20.
//
import SwiftUI

// MARK: - 2. 隐私政策主容器
struct PrivacyDetailView: View {
    @Environment(\.dismiss) var dismiss
    @AppStorage("hasAcceptedPrivacy") var hasAcceptedPrivacy: Bool = false
    @Binding var showPrivacy: Bool
    
    // 状态管理
    @State private var loadingProgress: Double = 0.0
    @State private var isPageLoading: Bool = true
    
    let privacyURL = URL(string: "https://www.baidu.com")! // 替换为真实地址

    var body: some View {
        VStack(spacing: 0) {
            // MARK: 自定义导航栏
            HStack {
                VStack(alignment: .leading, spacing: 2) {
                    Text("隐私政策与服务条款")
                        .font(.headline)
                    
                    Text("由 PunchNet 加密传输")
                        .font(.caption2)
                        .foregroundColor(.secondary)
                }
                Spacer()
                
                if isPageLoading {
                    ProgressView()
                        .controlSize(.small)
                }
                
                Button(action: { dismiss() }) {
                    Image(systemName: "xmark.circle.fill")
                        .foregroundColor(.secondary)
                        .font(.title3)
                }
                .buttonStyle(.plain)
            }
            .padding()
            .background(.ultraThinMaterial)

            // MARK: 线性进度条
            if isPageLoading {
                ProgressView(value: loadingProgress, total: 1.0)
                    .progressViewStyle(.linear)
                    .tint(.blue)
                    .frame(height: 2)
                    .transition(.opacity)
            } else {
                Divider().frame(height: 2)
            }

            // MARK: WebView 内容
            PunchNetWebView(url: privacyURL, progress: $loadingProgress, isLoading: $isPageLoading)
                .background(Color(NSColor.windowBackgroundColor))
            
            // MARK: 底部同意栏
            VStack(spacing: 12) {
                Divider()
                HStack {
                    Text("继续使用即表示您同意我们的全部条款。")
                        .font(.caption)
                        .foregroundColor(.secondary)
                    
                    Spacer()
                    
                    Button("拒绝") {
                        self.showPrivacy = false
                        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                            NSApplication.shared.terminate(nil)
                        }
                    }
                    .buttonStyle(.bordered)
                    
                    Button("同意并继续") {
                        hasAcceptedPrivacy = true
                        dismiss()
                    }
                    .buttonStyle(.borderedProminent)
                    .tint(.blue)
                }
                .padding(.horizontal, 20)
                .padding(.vertical, 16)
            }
            .background(.ultraThinMaterial)
        }
        .frame(minWidth: 600, minHeight: 700)
    }
    
}
