//
//  WiredTransferPhotoListPanel.swift
//  suixinkan
//

import SwiftUI

/// 有线传图照片列表面板（侧栏 + 分组列表）。
struct WiredTransferPhotoListPanel: View {
    let sidebarExpanded: Bool
    let sidebarGroups: [WiredTransferDateGroup]
    let photoSections: [WiredTransferPhotoSection]
    let selectedTimeSlotID: String?
    let selectUploadMode: Bool
    let selectedPhotoIDs: Set<String>
    let onToggleSidebar: () -> Void
    let onTimeSlotSelected: (String) -> Void
    let onPhotoClick: (String) -> Void
    let onRetry: (String) -> Void
    let onDelete: (String) -> Void
    let onTogglePhotoSelection: (String) -> Void

    var body: some View {
        HStack(spacing: 0) {
            if sidebarExpanded, !sidebarGroups.isEmpty {
                WiredTransferTimeSidebar(
                    groups: sidebarGroups,
                    selectedSlotID: selectedTimeSlotID,
                    onToggleSidebar: onToggleSidebar,
                    onTimeSlotSelected: onTimeSlotSelected
                )
                Divider().background(WiredTransferDesign.borderLight)
            }

            ZStack(alignment: .leading) {
                ScrollViewReader { proxy in
                    ScrollView {
                        LazyVStack(spacing: 0, pinnedViews: [.sectionHeaders]) {
                            ForEach(photoSections) { section in
                                Section {
                                    ForEach(section.photos) { photo in
                                        WiredTransferPhotoRow(
                                            photo: photo,
                                            selectUploadMode: selectUploadMode,
                                            selected: selectedPhotoIDs.contains(photo.id),
                                            onToggleSelection: { onTogglePhotoSelection(photo.id) },
                                            onPhotoClick: { onPhotoClick(photo.id) },
                                            onRetry: { onRetry(photo.id) },
                                            onDelete: { onDelete(photo.id) }
                                        )
                                        Divider()
                                            .background(WiredTransferDesign.borderLight)
                                            .padding(.horizontal, 10)
                                    }
                                } header: {
                                    sectionHeader(section.headerTitle)
                                        .id(section.slotId)
                                }
                            }
                        }
                        .padding(.bottom, 8)
                    }
                    .onChange(of: selectedTimeSlotID) { slotID in
                        guard let slotID else { return }
                        withAnimation {
                            proxy.scrollTo(slotID, anchor: .top)
                        }
                    }
                }

                if !sidebarExpanded, !sidebarGroups.isEmpty {
                    VStack {
                        Spacer()
                        WiredTransferSidebarExpandTab(onExpand: onToggleSidebar)
                        Spacer()
                    }
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
        .background(Color.white)
    }

    private func sectionHeader(_ title: String) -> some View {
        Text(title)
            .font(.system(size: 11))
            .foregroundStyle(WiredTransferDesign.text666)
            .lineLimit(1)
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding(.horizontal, 10)
            .padding(.vertical, 7)
            .background(WiredTransferDesign.pageBackground)
    }
}
