//
//  ReportAddStatusView.swift
//  Tusker
//
//  Created by Shadowfacts on 1/14/23.
//  Copyright © 2023 Shadowfacts. All rights reserved.
//

import SwiftUI
import Pachyderm

struct ReportAddStatusView: View {
    @ObservedObject var report: EditedReport
    let mastodonController: MastodonController
    
    @Environment(\.dismiss) private var dismiss
    @State private var statuses: [StatusMO]?
    @State private var error: Error?
    
    var body: some View {
        statusesListIfLoaded
            .navigationTitle("Add Posts")
    }
    
    @ViewBuilder
    private var statusesListIfLoaded: some View {
        if let statuses {
            List {
                ForEach(statuses, id: \.id) { status in
                    Button {
                        report.statusIDs.append(status.id)
                        dismiss()
                    } label: {
                        ReportStatusView(status: status, mastodonController: mastodonController)
                    }
                }
                .appGroupedListRowBackground()
            }
            .modifier(ScrollBackgroundModifier())
        } else {
            ZStack {
                // because the background needs to fill the entire screen
                Color.appGroupedBackground
                    .edgesIgnoringSafeArea(.all)
                
                ProgressView()
                    .progressViewStyle(.circular)
                    .alertWithData("Error Loading Posts", data: $error, actions: { _ in
                        Button("OK") {}
                    }, message: { error in
                        Text(error.localizedDescription)
                    })
                    .task { @MainActor in
                        do {
                            let req = Account.getStatuses(report.accountID, range: .count(40), excludeReplies: false, excludeReblogs: true)
                            let (statuses, _) = try await mastodonController.run(req)
                            await mastodonController.persistentContainer.addAll(statuses: statuses)
                            self.statuses = statuses.compactMap { mastodonController.persistentContainer.status(for: $0.id) }
                        } catch {
                            self.error = error
                        }
                    }
            }
        }
    }
}

private struct ScrollBackgroundModifier: ViewModifier {
    @Environment(\.colorScheme) private var colorScheme
    
    func body(content: Content) -> some View {
        if #available(iOS 16.0, *) {
            content
                .scrollContentBackground(.hidden)
                .background {
                    // otherwise the pureBlackDarkMode isn't propagated, for some reason?
                    // even though it is for ReportSelectRulesView??
                    let traits: UITraitCollection = {
                        var t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light)
                        if #available(iOS 17.0, *) {
                            t = t.modifyingTraits({ mutableTraits in
                                mutableTraits.pureBlackDarkMode = true
                            })
                        } else {
                            t.obsoletePureBlackDarkMode = true
                        }
                        return t
                    }()
                    Color(uiColor: .appGroupedBackground.resolvedColor(with: traits))
                        .edgesIgnoringSafeArea(.all)
                }
        } else {
            content
        }
    }
}
