//
//  TaskList.swift
//  Subtasks
//

import Adwaita

struct TaskList: View {

    @Binding var tasks: [Task]
    @State private var showAddDialog = false

    var view: Body {
        ScrollView {
            list
        }
        .topToolbar {
            HeaderBar.start {
                Button(icon: .default(icon: .listAdd)) {
                    showAddDialog = true
                }
            }
        }
        .dialog(visible: $showAddDialog, id: "add") {
            dialog
        }
    }

    var list: AnyView {
        List(tasks, selection: nil) { task in
            taskRow(task: task)
        }
        .boxedList()
        .valign(.start)
        .padding(20)
        .frame(maxWidth: 500)
    }

    var dialog: View {
        Text("Hello, world!")
            .padding()
            .topToolbar {
                HeaderBar.empty()
            }
    }

    func taskRow(task: Task) -> AnyView {
        ActionRow()
            .title(task.label)
            .prefix {
                CheckButton()
                    .active($tasks[id: task.id, default: .init(label: "")].done)
                    .selectionMode()
                    .valign(.center)
            }
            .suffix {
                ButtonContent()
                    .iconName(Icon.default(icon: .goNext).string)
            }
    }

}
