//
//  SetupView.swift
//  MagicCounter
//
//  Created by Atridad Lahiji on 2025-12-06.
//

import SwiftUI

/**
 * Screen for configuring a new game.
 *
 * Allows setting player count, starting life, and game options.
 */
struct SetupView: View {
    @Environment(\.dismiss) var dismiss
    @EnvironmentObject var gameManager: GameManager
    
    @State private var playerCount: Double = 4
    @State private var startingLife: Double = 40
    @State private var trackPoison = true
    @State private var trackCommander = true
    @State private var matchName = ""
    @State private var playerNames: [String] = []
    
    var body: some View {
        NavigationStack {
            Form {
                Section("Game Settings") {
                    SettingSlider(
                        title: "Starting Life",
                        value: $startingLife,
                        range: 10...40,
                        step: 5
                    )
                    
                    SettingSlider(
                        title: "Players",
                        value: $playerCount,
                        range: 2...8,
                        step: 1
                    )
                    .onChange(of: playerCount) { newValue in
                        updatePlayerNames(count: Int(newValue))
                    }
                }
                
                Section("Options") {
                    Toggle("Track Poison", isOn: $trackPoison)
                    Toggle("Track Commander Damage", isOn: $trackCommander)
                    TextField("Match Name (Optional)", text: $matchName)
                }
                
                Section("Player Names") {
                    ForEach(0..<playerNames.count, id: \.self) { index in
                        TextField("Player \(index + 1)", text: $playerNames[index])
                    }
                }
            }
            .navigationTitle("New Game")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") {
                        dismiss()
                    }
                }
                ToolbarItem(placement: .confirmationAction) {
                    Button("Start") {
                        startGame()
                    }
                    .bold()
                }
            }
            .onAppear {
                updatePlayerNames(count: Int(playerCount))
            }
        }
    }
    
    private func updatePlayerNames(count: Int) {
        while playerNames.count < count {
            playerNames.append("Player \(playerNames.count + 1)")
        }
        while playerNames.count > count {
            playerNames.removeLast()
        }
    }
    
    private func startGame() {
        gameManager.startNewGame(
            players: playerNames,
            startingLife: Int(startingLife),
            trackPoison: trackPoison,
            trackCommander: trackCommander,
            matchName: matchName
        )
        dismiss()
    }
}
