//
//  ImportedPlayer+Extensions.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 24/04/2024.
//

import Foundation

extension ImportedPlayer: PlayerHolder {
    func getAssimilatedAsMaleRank() -> Int? {
        guard male == false else { return nil }
        return getRank()?.femaleInMaleAssimilation
    }
    
    var computedAge: Int? { nil }
    
    var tournamentPlayed: Int? {
        Int(tournamentCount)
    }
    
    func getPoints() -> Double? {
        self.points
    }
    
    func getFirstName() -> String {
        self.firstName ?? ""
    }
    
    func getLastName() -> String {
        self.lastName ?? ""
    }
    
    func formattedLicense() -> String {
        if let license { return license.computedLicense }
        return "aucune licence"
    }
    
    func getRank() -> Int? {
        Int(rank)
    }
    
    func isUnranked() -> Bool {
        false
    }
    
    func formattedRank() -> String {
        rank.formatted()
    }

    func isMalePlayer() -> Bool {
        male
    }
    
    func hitForSearch(_ searchText: String) -> Int {
        var trimmedSearchText = searchText.lowercased().trimmingCharacters(in: .whitespaces).folding(options: .diacriticInsensitive, locale: .current)
        trimmedSearchText = trimmedSearchText.replaceCharactersFromSet(characterSet: .punctuationCharacters, replacementString: " ")
        trimmedSearchText = trimmedSearchText.replaceCharactersFromSet(characterSet: .symbols, replacementString: " ")
        
        if trimmedSearchText.isEmpty { return 0 }
        let tokens = trimmedSearchText.components(separatedBy: .whitespacesAndNewlines).filter { $0.isEmpty == false }
        if let license, trimmedSearchText.contains(license) {
            return 100
        }
        
        let label = canonicalFullName!
        if tokens.count > 1 {
            var wordFound = 0
            if trimmedSearchText.lowercased().components(separatedBy: .whitespacesAndNewlines).count > 1 {
                let searchFields: Set = Set([firstName!.canonicalVersion.components(separatedBy: .whitespacesAndNewlines), lastName!.canonicalVersion.components(separatedBy: .whitespacesAndNewlines)].flatMap { $0 })
                let tokens: Set = Set(trimmedSearchText.components(separatedBy: .whitespacesAndNewlines))
                wordFound = searchFields.intersection(tokens).count
            }

            if wordFound == 2 {
                if let first = tokens.pairs().first(where: { a,b in
                    label.contains(a) && label.contains(b)
                }) {
                    return 2 + first.0.count + first.1.count
                }
            } else {
                return wordFound * 10
            }
        } else if let first = tokens.first {
            if label.contains(first) {
                return 1
            }
        }
        return 0
    }
}

fileprivate extension Int {
    var femaleInMaleAssimilation: Int {
        self + TournamentCategory.femaleInMaleAssimilationAddition(self)
    }
}
