//
//  MenuWarningView.swift
//  PadelClub
//
//  Created by razmig on 28/04/2024.
//

import SwiftUI

struct MenuWarningView: View {
    let teams: [TeamRegistration]
    var date: Date?
    var message: String?
    var umpireMail: String?
    var subject: String?
    
    @Binding var contactType: ContactType?

    private func _getUmpireMail() -> [String]? {
        if let umpireMail {
            return [umpireMail]
        }
        return nil
    }
    
    // TODO: Guard
    @ViewBuilder
    private func _actionView(players: [PlayerRegistration], privateMode: Bool = false) -> some View {
        if players.count == 1, let player = players.first, let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "tel:\(number)") {
            Link(destination: url) {
                Label("Appeler", systemImage: "phone")
                Text(number)
            }
        } else {
            Menu {
                ForEach(players) { player in
                    if let number = player.phoneNumber?.replacingOccurrences(of: " ", with: ""), let url = URL(string: "tel:\(number)") {
                        Link(destination: url) {
                            Label(player.playerLabel(.short), systemImage: "phone")
                            Text(number)
                        }
                    }
                }
            } label: {
                Text("Appeler un joueur")
            }
        }

        Button("Message") {
            contactType = .message(date: date, recipients: players.compactMap({ $0.phoneNumber }), body: message, tournamentBuild: nil)
        }
        Button("Mail") {
            contactType = .mail(date: date, recipients: privateMode ? _getUmpireMail() : players.compactMap({ $0.email }), bccRecipients: privateMode ? players.compactMap({ $0.email }) : nil, body: message, subject: subject, tournamentBuild: nil)
        }
    }
    
    var body: some View {
        
        Menu {
            if let team = teams.first, teams.count == 1 {
                _teamActionView(team)
            } else {
                Menu("Tout le monde") {
                    let players = teams.flatMap({ $0.players() })
                    _actionView(players: players, privateMode: true)
                }
                Divider()
                ForEach(teams) { team in
                    _teamView(team)
                }
            }
        } label: {
            Text("Prévenir")
                .underline()
        }
    }
    
    func _playerView(_ player: PlayerRegistration) -> some View {
        Menu {
            let players = [player]
            _actionView(players: players)
        } label: {
            Text(player.playerLabel())
        }
    }

    func _teamView(_ team: TeamRegistration) -> some View {
        Menu {
            _teamActionView(team)
        } label: {
            Text(team.teamLabel(.short, twoLines: true))
        }
    }
    
    @ViewBuilder
    func _teamActionView(_ team: TeamRegistration) -> some View {
        Menu("Toute l'équipe") {
            let players = team.players()
            _actionView(players: players)
        }
        Divider()
        ForEach(team.players()) { player in
            _playerView(player)
        }
    }
}

//#Preview {
//    MenuWarningView(teams: [], contactType: .constant(nil))
//}
