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

import Foundation

struct SetDescriptor: Identifiable, Equatable {
    let id: UUID = UUID()
    var valueTeamOne: Int?
    var valueTeamTwo: Int?
    var tieBreakValueTeamOne: Int?
    var tieBreakValueTeamTwo: Int?
    var setFormat: SetFormat
    var showSetInputView: Bool = true
    var showTieBreakInputView: Bool = false

    var isTeamOneSet: Bool {
        return valueTeamOne != nil || tieBreakValueTeamOne != nil
    }
    
    var hasEnded: Bool {
        if let valueTeamTwo, let valueTeamOne {
            return setFormat.hasEnded(teamOne: valueTeamOne, teamTwo: valueTeamTwo)
        } else {
            return false
        }
    }
    
    var winner: TeamPosition? {
        if let valueTeamTwo, let valueTeamOne, valueTeamOne != valueTeamTwo {
            return setFormat.winner(teamOne: valueTeamOne, teamTwo: valueTeamTwo)
        } else {
            return nil
        }
    }
    
    var shouldTieBreak: Bool {
        setFormat.shouldTiebreak(scoreTeamOne: valueTeamOne ?? 0, scoreTeamTwo: valueTeamTwo ?? 0)
    }
    
    func getValue(teamPosition: TeamPosition) -> String? {
        switch teamPosition {
        case .one:
            if let valueTeamOne {
                if let tieBreakValueTeamOne {
                    return "\(valueTeamOne)-\(tieBreakValueTeamOne)"
                } else {
                    return "\(valueTeamOne)"
                }
            }
        case .two:
            if let valueTeamTwo {
                if let tieBreakValueTeamTwo {
                    return "\(valueTeamTwo)-\(tieBreakValueTeamTwo)"
                } else {
                    return "\(valueTeamTwo)"
                }
            }
        }
        
        return nil
    }
}
