import SwiftUI

private struct FixSheet: View {

    @Binding
    var isPresented: Bool

    @Binding
    var message: String

    @Binding
    var infoItems: [String]

    let action: () -> Void

    init(isPresented: Binding<Bool>, message: Binding<String>, infoItems: Binding<[String]>, action: @escaping () -> Void) {
        self._isPresented = isPresented
        self._message = message
        self._infoItems = infoItems
        self.action = action
    }

    var body: some View {
        VStack {
            Text("Fix issue")
                .font(.headline)
            Text(message)
                .font(.body)
            List {
                ForEach(infoItems, id: \.self) { item in
                    Text(item)
                }
            }
            HStack {
                Button("Fix", action: {
                    isPresented = false
                    action()
                })
                Button("Cancel", action: { isPresented = false })
            }
        }
        .frame(minHeight: 200)
        .padding()
    }
}

struct PageSettingsContentView: View {

    @EnvironmentObject
    private var content: Content

    @StateObject
    var checker: PageIssueChecker = .init()

    var body: some View {
        VStack(alignment: .leading) {
            HStack {
                Button("Check pages", action: { checker.check(pages: content.pages) })
                    .disabled(checker.isCheckingPages)
                if checker.isCheckingPages {
                    ProgressView()
                        .progressViewStyle(.circular)
                        .frame(height: 20)
                }
            }
            Text("\(checker.issues.count) Issues")
                .font(.headline)
            List(checker.issues.sorted()) { issue in
                HStack {
                    PageIssueView(issue: issue)
                        .id(issue.id)
                }
                .environmentObject(checker)
            }
        }
        .padding()
    }
}
