//
//  EventLinksView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 26/05/2024.
//

import SwiftUI

struct EventLinksView: View {
    let event: Event
    @State private var pageLink: PageLink = .teams
    
    func eventLinksPasteData() -> String {
        var link = [String]()
        link.append(event.eventTitle())
        
        event.tournaments.forEach({ tournament in
            if let url = tournament.shareURL(pageLink) {
                var tournamentLink = [String]()
                tournamentLink.append(tournament.tournamentTitle())
                tournamentLink.append(url.absoluteString)
                link.append(tournamentLink.joined(separator: "\n"))
            }
        })
        
        return link.joined(separator: "\n\n")
    }
    
    
    var body: some View {
        List {
            Section {
                let links : [PageLink] = [.teams, .summons, .groupStages, .matches, .rankings]
                Picker(selection: $pageLink) {
                    ForEach(links) { pageLink in
                        Text(pageLink.localizedLabel()).tag(pageLink)
                    }
                } label: {
                    Text("Choisir une page du tournoi en particulier")
                }
                .pickerStyle(.menu)
            }

            let eventLinksPasteData = eventLinksPasteData()
            Section {
                Text(eventLinksPasteData)
                    .italic()
                    .multilineTextAlignment(.leading)
                
                
                ShareLink("Partagez ce lien", item: eventLinksPasteData)
            }
        }
    }
}

//#Preview {
//    EventLinksView(event: Event.mock())
//}
