//
//  SeedInterval.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 01/04/2024.
//

import Foundation

struct SeedInterval: Hashable, Comparable {
    let first: Int
    let last: Int
    
    func pointsRange(tournamentLevel: TournamentLevel, teamsCount: Int) -> String {
        tournamentLevel.pointsRange(first: first, last: last, teamsCount: teamsCount)
    }
    
    static func <(lhs: SeedInterval, rhs: SeedInterval) -> Bool {
        return lhs.first < rhs.first
    }
    
    func isFixed() -> Bool {
        first == 1 && last == 2
    }
        
    var count: Int {
        dimension
    }
    
    private var dimension: Int {
        (last - (first - 1))
    }
    
    func chunks() -> [SeedInterval]? {
        if dimension > 3 {
            let split = dimension / 2
            if split%2 == 0 {
                let firstHalf = SeedInterval(first: first, last: first + split - 1)
                let secondHalf = SeedInterval(first: first + split, last: last)
                return [firstHalf, secondHalf]
            } else {
                let firstHalf = SeedInterval(first: first, last: first + split)
                let secondHalf = SeedInterval(first: first + split + 1, last: last)
                return [firstHalf, secondHalf]
            }
        } else {
            return nil
        }
    }
        
    func localizedLabel(_ displayStyle: DisplayStyle = .wide) -> String {
        if dimension < 3 {
            return "\(first)\(first.ordinalFormattedSuffix()) place"
        } else {
            return "Place \(first) à \(last)"
        }
    }
    
    func localizedInterval(_ displayStyle: DisplayStyle = .wide) -> String {
        if dimension < 3 {
            return "#\(first) / #\(last)"
        } else {
            return "#\(first) à #\(last)"
        }
    }
}
