//
//  WildPhotographerReportListViews
//  suixinkan
//
//  Created by Codex on 2026/7/1.
//

import MapKit
import SwiftUI
import UIKit

private struct WildReportRiskDetailView: View {
    let point: WildReportRiskPoint

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 14) {
                WildReportSectionHeader(icon: "mappin.and.ellipse", title: point.title, subtitle: point.location)

                detailCard {
                    detailRow("位置", point.location)
                    detailRow("风险等级", point.riskLevel)
                    detailRow("最近举报时间", point.latestTime)
                    detailRow("举报次数", "\(point.evidenceCount)次")
                }

                detailCard {
                    Text("风险说明")
                        .font(.system(size: 17, weight: .bold))
                        .foregroundStyle(AppDesign.textPrimary)
                    Text(point.description)
                        .font(.system(size: 14))
                        .foregroundStyle(AppDesign.textSecondary)
                        .lineSpacing(5)
                }
            }
            .padding(16)
        }
        .navigationTitle("风险点详情")
        .navigationBarTitleDisplayMode(.inline)
        .background(AppDesign.background.ignoresSafeArea())
    }

    private func detailCard<Content: View>(@ViewBuilder content: () -> Content) -> some View {
        VStack(alignment: .leading, spacing: 12) {
            content()
        }
        .padding(16)
        .frame(maxWidth: .infinity, alignment: .leading)
        .background(.white, in: RoundedRectangle(cornerRadius: 14))
    }

    private func detailRow(_ title: String, _ value: String) -> some View {
        HStack {
            Text(title)
                .font(.system(size: 14))
                .foregroundStyle(AppDesign.textSecondary)
            Spacer()
            Text(value)
                .font(.system(size: 14, weight: .semibold))
                .foregroundStyle(AppDesign.textPrimary)
        }
    }
}

struct WildReportRiskPointListView: View {
    @ObservedObject var homeViewModel: WildPhotographerReportHomeViewModel
    @Environment(\.horizontalSizeClass) private var horizontalSizeClass

    var body: some View {
        ScrollView {
            VStack(spacing: 12) {
                WildReportSectionHeader(
                    icon: "map.fill",
                    title: "附近风险地图",
                    subtitle: "查看附近摄影师风险点位"
                )

                ForEach(homeViewModel.riskPoints) { point in
                    NavigationLink {
                        WildReportRiskDetailView(point: point)
                    } label: {
                        riskPointRow(point)
                    }
                    .buttonStyle(.plain)
                }
            }
            .frame(maxWidth: contentMaxWidth)
            .padding(.horizontal, 18)
            .padding(.top, 14)
            .padding(.bottom, 28)
        }
        .navigationTitle("附近风险地图")
        .navigationBarTitleDisplayMode(.inline)
        .background(Color(hex: 0xF8FAFF).ignoresSafeArea())
    }

    private func riskPointRow(_ point: WildReportRiskPoint) -> some View {
        HStack(spacing: 12) {
            Image(systemName: "mappin.and.ellipse")
                .font(.system(size: 21, weight: .semibold))
                .foregroundStyle(AppDesign.primary)
                .frame(width: 44, height: 44)
                .background(AppDesign.primarySoft, in: RoundedRectangle(cornerRadius: 12))

            VStack(alignment: .leading, spacing: 6) {
                Text(point.title)
                    .font(.system(size: 16, weight: .bold))
                    .foregroundStyle(AppDesign.textPrimary)
                    .lineLimit(1)
                Text(point.location)
                    .font(.system(size: 13))
                    .foregroundStyle(AppDesign.textSecondary)
                    .lineLimit(1)
                Text("\(point.distance) · \(point.evidenceCount)条线索 · \(point.latestTime)")
                    .font(.system(size: 12, weight: .medium))
                    .foregroundStyle(AppDesign.primary)
            }

            Spacer()

            Image(systemName: "chevron.right")
                .font(.system(size: 13, weight: .semibold))
                .foregroundStyle(Color(hex: 0xB6BECA))
        }
        .padding(14)
        .background(.white, in: RoundedRectangle(cornerRadius: 16))
        .overlay(
            RoundedRectangle(cornerRadius: 16)
                .stroke(AppDesign.border, lineWidth: 1)
        )
    }

    private var contentMaxWidth: CGFloat {
        horizontalSizeClass == .regular ? 760 : .infinity
    }
}

struct WildPhotographerReportListView: View {
    @ObservedObject var homeViewModel: WildPhotographerReportHomeViewModel
    @StateObject private var viewModel: WildPhotographerReportListViewModel

    init(homeViewModel: WildPhotographerReportHomeViewModel) {
        self.homeViewModel = homeViewModel
        _viewModel = StateObject(wrappedValue: WildPhotographerReportListViewModel(homeViewModel: homeViewModel))
    }

    var body: some View {
        ScrollView {
            WildPhotographerReportListContent(homeViewModel: homeViewModel, viewModel: viewModel)
                .padding(.horizontal, 18)
                .padding(.top, 14)
                .padding(.bottom, 24)
        }
        .navigationTitle("我的举报")
        .navigationBarTitleDisplayMode(.inline)
        .background(Color(hex: 0xF8FAFF).ignoresSafeArea())
    }
}

struct WildPhotographerReportListContent: View {
    @ObservedObject var homeViewModel: WildPhotographerReportHomeViewModel
    @ObservedObject var viewModel: WildPhotographerReportListViewModel
    @EnvironmentObject private var toastCenter: ToastCenter
    @State private var selectedRecord: WildReportRecord?

    var body: some View {
        VStack(spacing: 14) {
            filterTabBar

            if viewModel.filteredReports.isEmpty {
                emptyState
            } else {
                ForEach(viewModel.filteredReports) { record in
                    WildReportListCard(
                        record: record,
                        onOpen: {
                            selectedRecord = record
                        },
                        onShare: {
                            viewModel.shareReport(record)
                        }
                    )
                }
            }

            Text(viewModel.footerTip)
                .font(.system(size: 12))
                .foregroundStyle(Color(hex: 0x9CA3AF))
                .multilineTextAlignment(.center)
                .frame(maxWidth: .infinity)
                .padding(.top, 8)
        }
        .appNavigationDestination(item: $selectedRecord) { record in
            WildPhotographerReportDetailView(record: record, homeViewModel: homeViewModel)
        }
        .onChange(of: viewModel.shareMessage) { message in
            guard let message else { return }
            toastCenter.show(message)
            viewModel.shareMessage = nil
        }
        .animation(.easeInOut(duration: 0.22), value: viewModel.selectedFilter)
        .animation(.easeInOut(duration: 0.22), value: viewModel.filteredReports.count)
    }

    private var filterTabBar: some View {
        HStack(spacing: 0) {
            ForEach(WildReportListFilter.allCases) { filter in
                Button {
                    viewModel.selectFilter(filter)
                } label: {
                    Text(filter.rawValue)
                        .font(.system(size: 13, weight: viewModel.selectedFilter == filter ? .bold : .medium))
                        .foregroundStyle(viewModel.selectedFilter == filter ? AppDesign.primary : Color(hex: 0x6B7280))
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 10)
                        .background(
                            viewModel.selectedFilter == filter
                                ? Color(hex: 0xEAF3FF)
                                : Color.clear,
                            in: RoundedRectangle(cornerRadius: 8)
                        )
                }
                .buttonStyle(.plain)
            }
        }
        .padding(4)
        .background(.white, in: RoundedRectangle(cornerRadius: 12))
        .overlay(
            RoundedRectangle(cornerRadius: 12)
                .stroke(AppDesign.border, lineWidth: 1)
        )
    }

    private var emptyState: some View {
        VStack(spacing: 10) {
            Image(systemName: "doc.text.magnifyingglass")
                .font(.system(size: 36))
                .foregroundStyle(Color(hex: 0xB6BECA))
            Text("暂无相关举报记录")
                .font(.system(size: 15, weight: .semibold))
                .foregroundStyle(AppDesign.textSecondary)
        }
        .frame(maxWidth: .infinity)
        .padding(.vertical, 48)
        .background(.white, in: RoundedRectangle(cornerRadius: 16))
    }
}

struct WildReportListCard: View {
    let record: WildReportRecord
    let onOpen: () -> Void
    let onShare: () -> Void

    private var scenicName: String {
        WildReportLocationParser.split(record.location).scenic
    }

    private var detailAddress: String {
        WildReportLocationParser.split(record.location).detail
    }

    private var iconTint: Color {
        record.status.displayColor
    }

    var body: some View {
        VStack(alignment: .leading, spacing: 13) {
            HStack(alignment: .top, spacing: 12) {
                ZStack {
                    RoundedRectangle(cornerRadius: 12)
                        .fill(iconTint.opacity(0.12))
                    Image(systemName: "camera.fill")
                        .font(.system(size: 20, weight: .semibold))
                        .foregroundStyle(iconTint)
                }
                .frame(width: 44, height: 44)

                VStack(alignment: .leading, spacing: 8) {
                    HStack(alignment: .top) {
                        Text(record.title)
                            .font(.system(size: 16, weight: .bold))
                            .foregroundStyle(AppDesign.textPrimary)
                            .lineLimit(2)
                        Spacer(minLength: 8)
                        HStack(spacing: 6) {
                            Text(record.status.listDisplayText)
                                .font(.system(size: 11, weight: .semibold))
                                .foregroundStyle(record.status.listDisplayColor)
                                .padding(.horizontal, 8)
                                .frame(height: 24)
                                .background(record.status.listDisplayColor.opacity(0.12), in: Capsule())

                            Button {
                                onShare()
                            } label: {
                                Image(systemName: "square.and.arrow.up")
                                    .font(.system(size: 13, weight: .semibold))
                                    .foregroundStyle(AppDesign.primary)
                                    .frame(width: 28, height: 28)
                                    .background(AppDesign.primarySoft, in: Circle())
                            }
                            .buttonStyle(.plain)
                            .accessibilityLabel("分享举报")
                        }
                    }

                    Text(scenicName)
                        .font(.system(size: 12, weight: .semibold))
                        .foregroundStyle(AppDesign.primary)
                        .padding(.horizontal, 8)
                        .frame(height: 22)
                        .background(AppDesign.primary.opacity(0.10), in: Capsule())
                }
            }

            VStack(spacing: 8) {
                listMetaRow(title: "举报编号", value: record.id)
                listMetaRow(title: "举报时间", value: record.submitTime)
            }

            HStack(spacing: 6) {
                Image(systemName: "mappin.and.ellipse")
                    .font(.system(size: 13, weight: .semibold))
                    .foregroundStyle(Color(hex: 0x9CA3AF))
                Text(detailAddress)
                    .font(.system(size: 13))
                    .foregroundStyle(AppDesign.textSecondary)
                    .lineLimit(1)
                Spacer()
                Image(systemName: "chevron.right")
                    .font(.system(size: 13, weight: .semibold))
                    .foregroundStyle(Color(hex: 0xC4CAD4))
            }
            .padding(.top, 2)
        }
        .padding(15)
        .background(.white, in: RoundedRectangle(cornerRadius: 16))
        .overlay(
            RoundedRectangle(cornerRadius: 16)
                .stroke(AppDesign.border, lineWidth: 1)
        )
        .contentShape(Rectangle())
        .onTapGesture {
            onOpen()
        }
    }

    private func listMetaRow(title: String, value: String) -> some View {
        HStack {
            Text(title)
                .font(.system(size: 13))
                .foregroundStyle(Color(hex: 0x9CA3AF))
            Spacer()
            Text(value)
                .font(.system(size: 13, weight: .medium))
                .foregroundStyle(AppDesign.textPrimary)
        }
    }
}

