// // PlayerNotableFinishes.swift // TournamentStats // // Created by Laurent Morvillier on 28/08/2019. // Copyright © 2019 Stax River. All rights reserved. // import Foundation import UIKit struct PlayerNotableFinishes { var player: Player var wins: [Result] = [] var runnerUps: [Result] = [] var finalTables: [Result] = [] } class PlayerNotableFinishesDataSource : NSObject, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { let notableFinishes: [PlayerNotableFinishes] let mostWins: [Player] let mostWinsCount: Int let mostRunnerUps: [Player] let mostRunnerUpsCount: Int let mostFinalTables: [Player] let mostFinalTablesCount: Int init(notableFinishes: [PlayerNotableFinishes]) { self.notableFinishes = notableFinishes /////// var nWins = notableFinishes nWins.sort { (p1, p2) -> Bool in return p1.wins.count > p2.wins.count } let winsTopCount = nWins.first?.wins.count ?? 0 self.mostWins = nWins.filter { $0.wins.count == winsTopCount}.map { $0.player } /////// var nRunnerups = notableFinishes nRunnerups.sort { (p1, p2) -> Bool in return p1.runnerUps.count > p2.runnerUps.count } let runnerUpsTopCount = nRunnerups.first?.runnerUps.count ?? 0 self.mostRunnerUps = nRunnerups.filter { $0.runnerUps.count == runnerUpsTopCount}.map { $0.player } /////// var nFinalTables = notableFinishes nFinalTables.sort { (p1, p2) -> Bool in return p1.finalTables.count > p2.finalTables.count } let finalTablesTopCount = nFinalTables.first?.finalTables.count ?? 0 self.mostFinalTables = nFinalTables.filter { $0.finalTables.count == finalTablesTopCount}.map { $0.player } self.mostWinsCount = winsTopCount self.mostRunnerUpsCount = runnerUpsTopCount self.mostFinalTablesCount = finalTablesTopCount } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 3 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Stat", for: indexPath) as! StatCollectionViewCell let name: String let value: String switch indexPath.row { case 0: name = "Most Wins" value = self.mostWins.map { $0.formattedName }.joined(separator: ", ") + " (\(self.mostWinsCount))" case 1: name = "Most Runner-up" value = self.mostRunnerUps.map { $0.formattedName }.joined(separator: ", ") + " (\(self.mostRunnerUpsCount))" case 2: name = "Most final tables" value = self.mostFinalTables.map { $0.formattedName }.joined(separator: ", ") + " (\(self.mostFinalTablesCount))" default: name = "" value = "" } cell.nameLabel.text = name.uppercased() cell.valueLabel.text = value cell.nameLabel.textColor = UIColor.white cell.valueLabel.textColor = UIColor.white cell.nameLabel.font = Fonts.regular cell.valueLabel.font = Fonts.bigNumbers return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 800.0, height: 90.0) } }