//
//  PromoteView.swift
//  TwoRooks
//
//  Created by Sam McBroom on 10/9/21.
//

import SwiftUI

struct PromoteView: View {
	@Environment(\.theme) private var theme
	@Environment(\.boardLength) private var boardLength
	@EnvironmentObject private var game: Game
	let promoteGroups: [Piece.Group] = [.knight, .bishop, .rook, .queen]
	let isLight: Bool
	
	private func promote(to group: Piece.Group) {
		let square = game.pawnSquare!
		let piece = Piece(isLight: game.board.lightTurn, group: group)
		game.board.promote(at: square, to: piece)
		game.pawnSquare = nil
	}
	
	var body: some View {
		Spacer()
		
		ForEach(promoteGroups) { group in
			Button {
				promote(to: group)
			} label: {
				PieceView(group: group, isLight: isLight)
					.frame(width: boardLength / 8, height: boardLength / 8)
			}
			
			Spacer()
		}
		.font(.system(size: 50))
		.disabled(game.pawnSquare == nil)
	}
}
