//
//  SetLabelView.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 02/04/2024.
//

import SwiftUI

struct SetLabelView: View {
    @Binding var initialValueLeft: Int?
    @Binding var initialValueRight: Int?
    @State private var valueLeft: Int = 0
    @State private var valueRight: Int = 0
    var shouldDisplaySteppers: Bool = false
    var isTieBreak: Bool = false

    var body: some View {
        HStack(spacing: 0) {
            if shouldDisplaySteppers {
                Stepper(value: $valueLeft, in: 0...Int.max) {
                    
                } onEditingChanged: { didChange in
                    initialValueLeft = valueLeft
                }
                .fixedSize()
                .scaleEffect(0.7)
            }
            Spacer()
            Text("\(valueLeft) / \(valueRight)")
                .font(isTieBreak ? .headline : .largeTitle).monospacedDigit()
                .scaledToFit()
                .minimumScaleFactor(0.5)
                .lineLimit(1)
            Spacer()
            if shouldDisplaySteppers {
                Stepper(value: $valueRight, in: 0...Int.max) {
                } onEditingChanged: { didChange in
                    initialValueRight = valueRight
                }
                .fixedSize()
                .scaleEffect(0.7)
            }
        }
        .onChange(of: initialValueLeft) {
            valueLeft = initialValueLeft ?? 0
        }
        .onChange(of: initialValueRight) {
            valueRight = initialValueRight ?? 0
        }
        .onAppear {
            valueLeft = initialValueLeft ?? 0
            valueRight = initialValueRight ?? 0
        }
    }
}

