//
//  GroupStageTeamReplacementView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 02/05/2024.
//

import SwiftUI

struct GroupStageTeamReplacementView: View {
    var team: TeamRegistration
    let teamRange: TeamRegistration.TeamRange?
    let teamRangeExtended: TeamRegistration.TeamRange?
    @State private var selectedPlayer: PlayerRegistration?
    @State private var selectedRange: TeamRegistration.TeamRange?
    
    var presentSearchView: Binding<Bool> {
        Binding {
            selectedRange != nil
        } set: { newValue in
            if newValue == false {
                selectedRange = nil
            }
        }
    }


    init(team: TeamRegistration) {
        self.team = team
        self.teamRange = team.replacementRange()
        self.teamRangeExtended = team.replacementRangeExtended()
    }
    
    private func _getWeight() -> Int {
        guard let selectedPlayer else { return 0 }
        return team.weight - selectedPlayer.computedRank
    }
    
    private func _searchableRange(_ teamRange: TeamRegistration.TeamRange) -> String {
        let left = teamRange.left?.weight
        let right = teamRange.right?.weight
        let sides = [left, right].compactMap({ $0 }).map { $0 - _getWeight() }
        return sides.map({ String($0) }).joined(separator: ",")
    }
    
    private func _searchToken(_ teamRange: TeamRegistration.TeamRange) -> SearchToken {
        if teamRange.left == nil { return .rankLessThan }
        if teamRange.right == nil { return .rankMoreThan }
        return .rankBetween
    }
    
    var body: some View {
        List {
            Section {
                Picker(selection: $selectedPlayer) {
                    HStack {
                        Text("Toute l'équipe")
                        Spacer()
                        Text(team.weight.formatted()).bold()
                    }
                    .tag(nil as PlayerRegistration?)
                    ForEach(team.players()) { player in
                        HStack {
                            Text(player.playerLabel())
                            Spacer()
                            Text(player.rankLabel()).bold()
                        }
                        .tag(player as PlayerRegistration?)
                    }
                } label: {
                    Text("Remplacement de l'équipe ou d'un joueur particulier")
                }
                .labelsHidden()
                .pickerStyle(.inline)
            } header: {
                Text("Remplacer")
            } footer: {
                Text("Remplacement de l'équipe ou d'un joueur particulier")
            }
            
            if let teamRange {
                Section {
                    TeamRangeView(teamRange: teamRange, playerWeight: _getWeight())
                    if selectedPlayer != nil {
                        RowButtonView("Chercher dans vos clubs favoris") {
                            selectedRange = teamRange
                        }
                    }
                } header: {
                    Text("Même position en poule")
                } footer: {
                    Text("Intervalle de poids d'équipe pour que le remplacement n'affecte pas les poules")
                }
            }
            if let teamRangeExtended {
                Section {
                    TeamRangeView(teamRange: teamRangeExtended, playerWeight: _getWeight())
                    if selectedPlayer != nil {
                        RowButtonView("Chercher dans vos clubs favoris") {
                            selectedRange = teamRangeExtended
                        }
                    }
                } header: {
                    Text("Même ligne en poule")
                } footer: {
                    Text("Intervalle de poids d'équipe respecte le tirage au sort par chapeau")
                }
            }
        }
        .headerProminence(.increased)
        .navigationTitle("Remplacement")
        .navigationBarTitleDisplayMode(.inline)
        .toolbarBackground(.visible, for: .navigationBar)
        .sheet(isPresented: presentSearchView) {
            if let selectedRange {
                _searchLinkView(selectedRange)
            }
        }
    }
    
    private func _searchLinkView(_ teamRange: TeamRegistration.TeamRange) -> some View {
        NavigationStack {
            let tournament = team.tournamentObject()
            SelectablePlayerListView(searchField: _searchableRange(teamRange), dataSet: .favoriteClubs, filterOption: tournament?.tournamentCategory.playerFilterOption ?? .all, sortOption: .rank, showFemaleInMaleAssimilation: true, tokens: [_searchToken(teamRange)], hidePlayers: tournament?.selectedPlayers().compactMap { $0.licenceId })
        }
    }
    
    struct TeamRangeView: View {
        let teamRange: TeamRegistration.TeamRange
        let playerWeight: Int

        @ViewBuilder
        var body: some View {
            HStack {
                TeamRangeSideView(team: teamRange.left, playerWeight: -playerWeight)
                Spacer()
                Image(systemName: "arrowshape.forward.fill")
                Spacer()
                TeamRangeSideView(team: teamRange.right, playerWeight: -playerWeight)
            }
        }
    }
    
    struct TeamRangeSideView: View {
        let team: TeamRegistration?
        let playerWeight: Int

        @ViewBuilder
        var body: some View {
            if let team {
                Text((team.weight + playerWeight).formatted()).font(.largeTitle)
            } else {
                Text("Aucune limite")
            }
        }
    }
}

//#Preview {
//    GroupStageTeamReplacementView(team: TeamRegistration.mock())
//}
