//
//  MatchScheduleEditorView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 10/04/2024.
//

import SwiftUI

struct MatchScheduleEditorView: View {
    @Bindable var match: Match
    var tournament: Tournament
    @State private var startDate: Date
    
    init(match: Match, tournament: Tournament) {
        self.match = match
        self.tournament = tournament
        self._startDate = State(wrappedValue: match.startDate ?? tournament.startDate)
    }
    
    var title: String {
        if let round = match.roundObject {
            return round.roundTitle() + " " + match.matchTitle()
        } else {
            return match.matchTitle()
        }
    }
    
    var body: some View {
        DatePickingView(title: title, startDate: $startDate, currentDate: .constant(nil), duration: match.matchFormat.getEstimatedDuration(tournament.additionalEstimationDuration)) {
            await _updateSchedule()
        }
    }
    
    private func _updateSchedule() async {
        let scheduler: MatchScheduler? = tournament.matchScheduler()
        scheduler?.updateBracketSchedule(tournament: tournament, fromRoundId: match.round, fromMatchId: match.id, startDate: startDate)
    }
}

//#Preview {
//    MatchScheduleEditorView(match: Match.mock(), tournament: Tournament.mock())
//}
