//
//  WiredTransferTimeSidebar.swift
//  suixinkan
//

import SwiftUI

/// 有线传图时间侧栏。
struct WiredTransferTimeSidebar: View {
    let groups: [WiredTransferDateGroup]
    let selectedSlotID: String?
    let onToggleSidebar: () -> Void
    let onTimeSlotSelected: (String) -> Void

    var body: some View {
        VStack(spacing: 0) {
            Button(action: onToggleSidebar) {
                HStack(spacing: 2) {
                    Image(systemName: "chevron.left")
                        .font(.system(size: 12))
                    Text("收起")
                        .font(.system(size: 11))
                }
                .foregroundStyle(WiredTransferDesign.text666)
                .frame(maxWidth: .infinity)
                .padding(.horizontal, 8)
                .padding(.vertical, 10)
            }
            .buttonStyle(.plain)

            ScrollView {
                VStack(alignment: .leading, spacing: 0) {
                    ForEach(groups, id: \.dateLabel) { group in
                        Text(group.dateLabel)
                            .font(.system(size: 12))
                            .foregroundStyle(WiredTransferDesign.text999)
                            .padding(.leading, 10)
                            .padding(.top, 8)
                            .padding(.bottom, 4)
                        ForEach(group.slots) { slot in
                            timeSlotButton(slot)
                        }
                    }
                }
                .padding(.bottom, 12)
            }
        }
        .frame(width: WiredTransferDesign.sidebarWidth)
        .background(WiredTransferDesign.sidebarBackground)
    }

    private func timeSlotButton(_ slot: WiredTransferTimeSlot) -> some View {
        let selected = slot.id == selectedSlotID
        return Button {
            onTimeSlotSelected(slot.id)
        } label: {
            VStack(spacing: 2) {
                HStack(spacing: 2) {
                    Image(systemName: "play.fill")
                        .font(.system(size: 8))
                        .foregroundStyle(selected ? AppDesign.primary : WiredTransferDesign.text333)
                    Text(slot.slotStartLabel)
                        .font(.system(size: 13, weight: selected ? .semibold : .regular))
                        .foregroundStyle(selected ? AppDesign.primary : WiredTransferDesign.text333)
                }
                Text("\(slot.photoCount)张")
                    .font(.system(size: 10))
                    .foregroundStyle(WiredTransferDesign.text999)
            }
            .frame(maxWidth: .infinity)
            .padding(.horizontal, 8)
            .padding(.vertical, 8)
            .background(selected ? Color.white : Color.clear)
        }
        .buttonStyle(.plain)
    }
}

/// 侧栏收起后的展开按钮。
struct WiredTransferSidebarExpandTab: View {
    let onExpand: () -> Void

    var body: some View {
        Button(action: onExpand) {
            Image(systemName: "chevron.right")
                .font(.system(size: 14))
                .foregroundStyle(WiredTransferDesign.text666)
                .padding(.horizontal, 4)
                .padding(.vertical, 16)
                .background(WiredTransferDesign.sidebarBackground)
                .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
        }
        .buttonStyle(.plain)
        .padding(.leading, 4)
    }
}
