//
//  GroupStageScheduleEditorView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 07/04/2024.
//

import SwiftUI
import LeStorage

struct GroupStageScheduleEditorView: View {
    @EnvironmentObject var dataStore: DataStore
    @Bindable var groupStage: GroupStage
    var tournament: Tournament
    @State private var startDate: Date

    init(groupStage: GroupStage, tournament: Tournament) {
        self.groupStage = groupStage
        self.tournament = tournament
        self._startDate = State(wrappedValue: groupStage.startDate ?? tournament.startDate)
    }
    
    var body: some View {
        DatePickingView(title: groupStage.groupStageTitle(), startDate: $startDate, currentDate: $groupStage.startDate, duration: groupStage.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) {
            groupStage.startDate = startDate
            _save()
        }
    }
        
    private func _save() {
        do {
            try dataStore.groupStages.addOrUpdate(instance: groupStage)
        } catch {
            Logger.error(error)
        }
    }
}

//#Preview {
//    GroupStageScheduleEditorView(groupStage: GroupStage.mock(), tournament: Tournament.mock())
//}
