//
//  MatchRowView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 25/11/2023.
//

import SwiftUI
import LeStorage

struct MatchRowView: View {
    
    @EnvironmentObject var dataStore: DataStore
    @Environment(\.matchViewStyle) private var matchViewStyle
    @State var match: Match
    var title: String? = nil
    var updatedField: Int? = nil

    @Environment(\.isEditingTournamentSeed) private var isEditingTournamentSeed

    @ViewBuilder
    var body: some View {
        if isEditingTournamentSeed.wrappedValue == true && match.isGroupStage() == false && match.disabled == false {
            MatchSetupView(match: match)
        } else {
//            MatchSummaryView(match: match, matchViewStyle: matchViewStyle)
//                .overlay {
//                    if match.disabled {
//                        Image(systemName: "xmark")
//                            .resizable()
//                            .scaledToFit()
//                            .opacity(0.8)
//                    }
//                }
//                .contextMenu(menuItems: {
//                    Text("index: \(match.index)")
//                    Text("bye state : \(match.byeState)")
//                    Text("disable state : \(match.disabled)")
//                    Button("enable") {
//                        match._toggleMatchDisableState(false)
//                    }
//                    Button("disable") {
//                        match._toggleMatchDisableState(true)
//                    }
//                    Button("bye") {
//                        match.byeState = true
//                    }
//                    Button("not bye") {
//                        match.byeState = false
//                    }
//                    Button("solo toggle") {
//                        match.disabled.toggle()
//                    }
//                    Button("toggle fwrd match") {
//                        match._toggleForwardMatchDisableState(true)
//                    }
//                    Button("toggle loser match") {
//                        match._toggleLoserMatchDisableState(true)
//                    }
//                })

            NavigationLink {
                MatchDetailView(match: match, updatedField: updatedField)
            } label: {
                MatchSummaryView(match: match, title: title, updatedField: updatedField)
                    .contextMenu {
                        Section {
                            ForEach(match.teams().flatMap({ $0.players() })) { player in
                                Button {
                                    player.hasArrived.toggle()
                                    do {
                                        try player.tournamentStore?.playerRegistrations.addOrUpdate(instance: player)
                                    } catch {
                                        Logger.error(error)
                                    }
                                } label: {
                                    Label(player.playerLabel(), systemImage: player.hasArrived ? "checkmark" : "xmark")
                                }
                            }
                        } header: {
                            Text("Présence")
                        }
                        
                        Divider()
                        NavigationLink {
                            EditSharingView(match: match)
                        } label: {
                            Text("Partage sur les réseaux sociaux")
                        }
                    }
            }
            //.modifier(BroadcastViewModifier(isBroadcasted: match.isBroadcasted()))
        }
    }
}


//#Preview {
//    MatchRowView(match: Match.mock(), matchViewStyle: .standardStyle)
//}
