// // Club.swift // PadelClub // // Created by Laurent Morvillier on 02/02/2024. // import Foundation import SwiftUI import LeStorage @Observable final public class Club: BaseClub { public func clubTitle(_ displayStyle: DisplayStyle = .wide) -> String { switch displayStyle { case .wide, .title: return name case .short: return acronym } } public func shareURL() -> URL? { return URL(string: URLs.main.url.appending(path: "?club=\(id)").absoluteString.removingPercentEncoding!) } public var customizedCourts: [Court] { DataStore.shared.courts.filter { $0.club == self.id }.sorted(by: \.index) } public override func deleteDependencies(store: Store, actionOption: ActionOption) { store.deleteDependencies(type: Court.self, actionOption: actionOption) { ($0 as? Court)?.club == self.id } } public override func deleteUnusedSharedDependencies(store: Store) { store.deleteUnusedSharedDependencies(type: Court.self, shouldBeSynchronized: false) { $0.club == self.id } } } extension Club { public var isValid: Bool { name.isEmpty == false && name.count > 3 } public func automaticShortName() -> String { name.acronym() } public enum AcronymMode: String, CaseIterable { case automatic = "Automatique" case custom = "Personalisée" } public func shortNameMode() -> AcronymMode { (acronym.isEmpty || acronym == automaticShortName()) ? .automatic : .custom } public func hasTenupId() -> Bool { code != nil } public func federalLink() -> URL? { guard let code else { return nil } return URL(string: "https://tenup.fft.fr/club/\(code)") } public func courtName(atIndex courtIndex: Int) -> String { courtNameIfAvailable(atIndex: courtIndex) ?? Court.courtIndexedTitle(atIndex: courtIndex) } public func courtNameIfAvailable(atIndex courtIndex: Int) -> String? { customizedCourts.first(where: { $0.index == courtIndex })?.name } public func update(fromClub club: Club) { self.acronym = club.acronym self.name = club.name self.phone = club.phone self.code = club.code self.address = club.address self.city = club.city self.zipCode = club.zipCode self.latitude = club.latitude self.longitude = club.longitude } public func hasBeenCreated(by creatorId: String?) -> Bool { return creatorId == creator || creator == nil || self.relatedUser == creatorId } public func isFavorite() -> Bool { return DataStore.shared.user.clubs.contains(where: { $0 == self.id }) } public static func findOrCreate(name: String, code: String?, city: String? = nil, zipCode: String? = nil) -> Club { /* identify a club : code, name, ?? */ let club: Club? = DataStore.shared.clubs.first(where: { (code == nil && $0.name == name && $0.city == city && $0.zipCode == zipCode) || code != nil && $0.code == code }) if let club { return club } else { let club = Club(creator: StoreCenter.main.userId, name: name, acronym: name.acronym(), code: code, city: city, zipCode: zipCode) club.relatedUser = StoreCenter.main.userId return club } } }