import SwiftUI
import SwiftData

struct RemindersListView: View {
    @Environment(\.modelContext) private var ctx
    @Environment(\.dismiss) private var dismiss
    @Query(sort: \CustomReminder.updatedAt, order: .reverse)
    private var customReminders: [CustomReminder]
    @Query(sort: \MetricReminder.updatedAt, order: .reverse)
    private var reminders: [MetricReminder]

    /// 以 sheet 形态呈现(从「新建」入口进入)时补一个「完成」按钮关闭;
    /// push 形态有系统返回,默认 false。
    var presentedAsSheet = false

    @State private var editingId: String?
    @State private var creatingNew = false
    @State private var editingCustom: CustomReminder?

    private var isEmpty: Bool { customReminders.isEmpty && reminders.isEmpty }

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 12) {
                header
                createButton

                if isEmpty {
                    emptyState
                } else {
                    ForEach(customReminders) { r in
                        CustomReminderRow(
                            reminder: r,
                            onTapEdit: { editingCustom = r },
                            onToggle: { Task { await syncCustom(r) } }
                        )
                    }

                    if !reminders.isEmpty {
                        sectionLabel(String(appLoc: "指标记录提醒"))
                        ForEach(reminders) { r in
                            ReminderRow(
                                reminder: r,
                                isEditing: editingId == r.metricId,
                                onTapEdit: { toggleEdit(r.metricId) },
                                onChange: { Task { await sync(r) } },
                                onDelete: { delete(r) }
                            )
                        }
                    }
                }
            }
            .padding(.horizontal, 16)
            .padding(.top, 12)
            .padding(.bottom, 32)
        }
        .background(Tj.Palette.sand.ignoresSafeArea())
        .navigationTitle("提醒")
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            if presentedAsSheet {
                ToolbarItem(placement: .topBarTrailing) {
                    Button(String(appLoc: "完成")) { dismiss() }
                }
            }
        }
        .sheet(isPresented: $creatingNew) {
            CustomReminderEditSheet()
        }
        .sheet(item: $editingCustom) { r in
            CustomReminderEditSheet(reminder: r)
        }
    }

    private var header: some View {
        Text("新建提醒,或在记录指标时开启")
            .font(.tjScaled( 12))
            .foregroundStyle(Tj.Palette.text3)
            .frame(maxWidth: .infinity, alignment: .leading)
    }

    private var createButton: some View {
        Button { creatingNew = true } label: {
            Label(String(appLoc: "新建提醒"), systemImage: "plus")
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(TjPrimaryButton(height: 46, fontSize: 14))
    }

    private func sectionLabel(_ text: String) -> some View {
        Text(text)
            .font(.tjScaled( 12, weight: .semibold))
            .foregroundStyle(Tj.Palette.text3)
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding(.top, 8)
    }

    private var emptyState: some View {
        VStack(spacing: 12) {
            Spacer(minLength: 40)
            TjPlaceholder(label: String(appLoc: "还没有提醒,点上方新建"))
                .frame(width: 240, height: 140)
            Spacer()
        }
        .frame(maxWidth: .infinity)
    }

    // MARK: - 自由提醒

    private func syncCustom(_ r: CustomReminder) async {
        r.updatedAt = .now
        try? ctx.save()
        await ReminderService.sync(r)
    }

    // MARK: - 指标提醒(沿用原逻辑)

    private func toggleEdit(_ id: String) {
        editingId = (editingId == id) ? nil : id
    }

    private func sync(_ r: MetricReminder) async {
        r.updatedAt = .now
        try? ctx.save()
        await ReminderService.sync(r)
    }

    private func delete(_ r: MetricReminder) {
        ReminderService.cancel(metricId: r.metricId)
        ctx.delete(r)
        try? ctx.save()
    }
}

/// 自由提醒行:点空白区进编辑 sheet;行上 Toggle 控开关。
private struct CustomReminderRow: View {
    @Bindable var reminder: CustomReminder
    let onTapEdit: () -> Void
    let onToggle: () -> Void

    var body: some View {
        HStack(spacing: 12) {
            Button(action: onTapEdit) {
                HStack(spacing: 12) {
                    ZStack {
                        Circle()
                            .fill(reminder.enabled ? Tj.Palette.amber.opacity(0.25) : Tj.Palette.sand2)
                        Image(systemName: "bell.fill")
                            .font(.tjScaled( 16))
                            .foregroundStyle(reminder.enabled ? Tj.Palette.ink : Tj.Palette.text3)
                    }
                    .frame(width: 36, height: 36)

                    VStack(alignment: .leading, spacing: 2) {
                        Text(reminder.title)
                            .font(.tjScaled( 15, weight: .semibold))
                            .foregroundStyle(Tj.Palette.text)
                            .lineLimit(1)
                        Text("\(reminder.timeLabel) · \(reminder.frequencyLabel)")
                            .font(.tjScaled( 12))
                            .foregroundStyle(Tj.Palette.text3)
                    }
                    Spacer(minLength: 0)
                }
                .contentShape(Rectangle())
            }
            .buttonStyle(.plain)

            Toggle("", isOn: $reminder.enabled)
                .labelsHidden()
                .tint(Tj.Palette.ink)
                .onChange(of: reminder.enabled) { _, _ in onToggle() }

            // 与指标提醒行的 28×28 展开按钮等宽,保证两类行的 Toggle 纵向对齐。
            Image(systemName: "chevron.right")
                .font(.tjScaled( 12, weight: .semibold))
                .foregroundStyle(Tj.Palette.text3)
                .frame(width: 28, height: 28)
        }
        .padding(14)
        .background(
            RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
                .fill(Tj.Palette.paper)
        )
        .overlay(
            RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
                .strokeBorder(Tj.Palette.lineSoft, lineWidth: 1)
        )
    }
}

private struct ReminderRow: View {
    @Bindable var reminder: MetricReminder
    let isEditing: Bool
    let onTapEdit: () -> Void
    let onChange: () -> Void
    let onDelete: () -> Void

    @State private var pickedTime: Date = .now
    @State private var hydrated = false

    var body: some View {
        VStack(spacing: 12) {
            headerRow
            if isEditing {
                editingPanel
            }
        }
        .padding(14)
        .background(
            RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
                .fill(Tj.Palette.paper)
        )
        .overlay(
            RoundedRectangle(cornerRadius: Tj.Radius.md, style: .continuous)
                .strokeBorder(Tj.Palette.lineSoft, lineWidth: 1)
        )
    }

    private var headerRow: some View {
        HStack(spacing: 12) {
            ZStack {
                Circle()
                    .fill(reminder.enabled ? Tj.Palette.amber.opacity(0.25) : Tj.Palette.sand2)
                Image(systemName: "bell.fill")
                    .font(.tjScaled( 16))
                    .foregroundStyle(reminder.enabled ? Tj.Palette.ink : Tj.Palette.text3)
            }
            .frame(width: 36, height: 36)

            VStack(alignment: .leading, spacing: 2) {
                Text(reminder.displayName)
                    .font(.tjScaled( 15, weight: .semibold))
                    .foregroundStyle(Tj.Palette.text)
                Text("\(reminder.timeLabel) · \(reminder.frequencyLabel)")
                    .font(.tjScaled( 12))
                    .foregroundStyle(Tj.Palette.text3)
            }

            Spacer()

            Toggle("", isOn: $reminder.enabled)
                .labelsHidden()
                .tint(Tj.Palette.ink)
                .onChange(of: reminder.enabled) { _, _ in onChange() }

            Button {
                onTapEdit()
            } label: {
                Image(systemName: isEditing ? "chevron.up" : "chevron.down")
                    .font(.tjScaled( 12, weight: .semibold))
                    .foregroundStyle(Tj.Palette.text3)
                    .frame(width: 28, height: 28)
            }
            .buttonStyle(.plain)
        }
    }

    private var editingPanel: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack {
                Text("时间").font(.tjScaled( 13)).foregroundStyle(Tj.Palette.text2)
                Spacer()
                DatePicker("", selection: $pickedTime, displayedComponents: .hourAndMinute)
                    .datePickerStyle(.compact)
                    .labelsHidden()
                    .onChange(of: pickedTime) { _, new in
                        let cal = Calendar.current
                        reminder.hour = cal.component(.hour, from: new)
                        reminder.minute = cal.component(.minute, from: new)
                        onChange()
                    }
            }
            weekdayRow
            HStack {
                Spacer()
                Button(role: .destructive) {
                    onDelete()
                } label: {
                    Label("删除提醒", systemImage: "trash")
                        .font(.tjScaled( 12, weight: .semibold))
                        .foregroundStyle(Tj.Palette.brick)
                }
                .buttonStyle(.plain)
            }
        }
        .onAppear {
            if !hydrated {
                pickedTime = Calendar.current.date(
                    bySettingHour: reminder.hour, minute: reminder.minute, second: 0, of: .now
                ) ?? .now
                hydrated = true
            }
        }
    }

    private var weekdayRow: some View {
        let names = [
            String(appLoc: "一"), String(appLoc: "二"), String(appLoc: "三"),
            String(appLoc: "四"), String(appLoc: "五"), String(appLoc: "六"),
            String(appLoc: "日"),
        ]
        let weekdayValues = [2, 3, 4, 5, 6, 7, 1]
        return HStack(spacing: 6) {
            ForEach(Array(weekdayValues.enumerated()), id: \.offset) { idx, w in
                Button {
                    var s = Set(reminder.weekdays)
                    if s.contains(w) { s.remove(w) } else { s.insert(w) }
                    reminder.weekdays = s.sorted()
                    onChange()
                } label: {
                    Text(names[idx])
                        .font(.tjScaled( 13,
                                      weight: reminder.weekdays.contains(w) ? .semibold : .regular))
                        .foregroundStyle(reminder.weekdays.contains(w) ? Tj.Palette.paper : Tj.Palette.text)
                        .frame(maxWidth: .infinity, minHeight: 30)
                        .background(
                            RoundedRectangle(cornerRadius: 8, style: .continuous)
                                .fill(reminder.weekdays.contains(w) ? Tj.Palette.ink : Tj.Palette.paper)
                        )
                        .overlay(
                            RoundedRectangle(cornerRadius: 8, style: .continuous)
                                .strokeBorder(Tj.Palette.line,
                                              lineWidth: reminder.weekdays.contains(w) ? 0 : 1)
                        )
                }
                .buttonStyle(.plain)
            }
        }
    }
}

#Preview {
    NavigationStack {
        RemindersListView()
    }
    .modelContainer(for: [MetricReminder.self, CustomReminder.self], inMemory: true)
}
