//
//  RankCalculatorView.swift
//  Padel Tournament
//
//  Created by Razmig Sarkissian on 03/05/2023.
//

import SwiftUI

struct RankCalculatorView: View {
    @State private var rank: Int = 1
    @AppStorage("lastRankCalculatorLevel") private var tournamentLevel: TournamentLevel = .p25
    @AppStorage("lastRankCalculatorCount") private var count: PlayersCountRange = .N8

    var body: some View {
        Form {
            Section {
                HStack {
                    let ordinal = NumberFormatter.ordinal.string(from: NSNumber(value:rank))!
                    Text("\(ordinal) d'un \(tournamentLevel.localizedLabel()) de \(count.localizedLabel()) équipes:")
                    Spacer()
                    Text(tournamentLevel.points(for: rank-1, count: count.rawValue).formatted(.number.sign(strategy: .always())))
                }
            }
            Section {
                Picker(selection: $tournamentLevel) {
                    ForEach(TournamentLevel.allCases) { level in
                        Text(level.localizedLabel()).tag(level)
                    }
                } label: {
                    Label("Niveau", systemImage: "gauge.medium")
                }
                Picker(selection: $count) {
                    ForEach(tournamentLevel.ranges, id: \.self) {
                        Text($0.localizedLabel()).tag($0)
                    }
                } label: {
                    Label("Nombre d'équipes", systemImage: "person.2")
                }
                Picker(selection: $rank) {
                    ForEach((1...count.rawValue), id: \.self) {
                        Text("#\($0)").tag($0)
                    }
                } label: {
                    Label("Votre position", systemImage: "number")
                }
            }
            Section {
                ForEach(tournamentLevel.allPoints(for: count.rawValue).indices, id: \.self) { i in
                    HStack {
                        Text("#"+(i+1).formatted())
                        Spacer()
                        Text(tournamentLevel.allPoints(for: count.rawValue)[i].formatted(.number.sign(strategy: .always())))
                    }
                }
            }
        }
        .navigationTitle("Calculateur de points")
    }
}
