import SwiftUI

struct B1GuideView: View {
    var onSingle: () -> Void
    var onMulti: () -> Void
    var onSkip: () -> Void

    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Button(action: onSkip) {
                    Image(systemName: "xmark")
                        .font(.system(size: 18, weight: .semibold))
                        .foregroundStyle(Tj.Palette.text)
                        .frame(width: 36, height: 36)
                }
                Spacer()
                Button(action: onSkip) {
                    Text("跳过")
                        .font(.system(size: 12))
                        .foregroundStyle(Tj.Palette.text3)
                        .padding(8)
                }
            }
            .padding(.horizontal, 12)

            VStack(alignment: .leading, spacing: 0) {
                ZStack {
                    RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
                        .fill(Tj.Palette.ink)
                    Image(systemName: "doc.text.fill")
                        .font(.system(size: 26, weight: .medium))
                        .foregroundStyle(Tj.Palette.paper)
                }
                .frame(width: 60, height: 60)
                .padding(.bottom, 18)

                Text("归档一份\n关键报告")
                    .font(.system(size: 30, weight: .bold))
                    .lineSpacing(6)
                    .foregroundStyle(Tj.Palette.text)
                    .padding(.bottom, 12)

                Text("推荐拍清晰的\(Text("整张图").underline())，多页报告可一次完成扫描。原图与解读全部本地加密保存，永不上传。")
                    .font(.system(size: 13))
                    .foregroundStyle(Tj.Palette.text2)
                    .lineSpacing(6)
                    .padding(.bottom, 26)

                VStack(spacing: 12) {
                    OptCard(title: String(appLoc: "单张报告"), sub: String(appLoc: "一张图，几秒搞定"), hint: String(appLoc: "化验单 · 处方"), badge: nil, action: onSingle)
                    OptCard(title: String(appLoc: "多页报告"), sub: String(appLoc: "像扫描文档一样翻页拍摄"), hint: String(appLoc: "体检报告 · 影像报告"), badge: String(appLoc: "推荐"), action: onMulti)
                }

                Spacer(minLength: 18)

                HStack(alignment: .top, spacing: 10) {
                    Image(systemName: "lock.fill")
                        .font(.system(size: 12))
                        .foregroundStyle(Tj.Palette.text2)
                        .padding(.top, 2)
                    Text("所有照片以 AES 加密存于本机沙盒。康康 服务端无法访问。")
                        .font(.system(size: 11))
                        .foregroundStyle(Tj.Palette.text2)
                        .lineSpacing(4)
                        .frame(maxWidth: .infinity, alignment: .leading)
                }
                .padding(12)
                .background(
                    RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
                        .fill(Tj.Palette.sand2)
                )
            }
            .padding(.horizontal, 24)
            .padding(.top, 20)
            .padding(.bottom, 20)
        }
        .background(Tj.Palette.sand.ignoresSafeArea())
    }
}

private struct OptCard: View {
    let title: String
    let sub: String
    let hint: String
    let badge: String?
    let action: () -> Void

    var body: some View {
        Button(action: action) {
            HStack(spacing: 14) {
                ZStack {
                    RoundedRectangle(cornerRadius: Tj.Radius.sm, style: .continuous)
                        .fill(Tj.Palette.sand2)
                    Image(systemName: "doc.text")
                        .font(.system(size: 18, weight: .regular))
                        .foregroundStyle(Tj.Palette.ink)
                }
                .frame(width: 44, height: 44)
                VStack(alignment: .leading, spacing: 3) {
                    HStack(spacing: 8) {
                        Text(title)
                            .font(.system(size: 15, weight: .semibold))
                            .foregroundStyle(Tj.Palette.text)
                        if let badge {
                            TjBadge(text: badge, style: .ink)
                        }
                    }
                    Text("\(sub) · \(hint)")
                        .font(.system(size: 11))
                        .foregroundStyle(Tj.Palette.text3)
                }
                Spacer()
                Image(systemName: "chevron.right")
                    .font(.system(size: 14, weight: .medium))
                    .foregroundStyle(Tj.Palette.text3)
            }
            .padding(16)
            .tjCard(bordered: true)
        }
        .buttonStyle(.plain)
    }
}

#Preview {
    B1GuideView(
        onSingle: { print("单张报告") },
        onMulti: { print("多页报告") },
        onSkip: { print("跳过") }
    )
}
