// // TournamentStats.swift // TournamentStats // // Created by Laurent Morvillier on 10/06/2019. // Copyright © 2019 Stax River. All rights reserved. // import Foundation import UIKit class TournamentStats : NSObject, HTMLRepresentable, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { var entries: Double var buyin: Double var prizepool: Double var firstPrize: Double init(entries: Double, buyin: Double, prizepool: Double, firstPrize: Double) { self.entries = entries self.buyin = buyin self.prizepool = prizepool self.firstPrize = firstPrize super.init() } func columnDescriptors() -> [ColumnDescriptor] { return [] } func cellValues() -> [String] { return [] } func htmlHeaders() -> String { return "" } func html() -> String { var strings: [(String, String)] = [] strings.append(("Entries", NumberFormatter().string(from: NSNumber(value: self.entries))!)) strings.append(("Buy-in", Formatter.currency.string(from: NSNumber(value: self.buyin))!)) strings.append(("Prizepool", Formatter.currency.string(from: NSNumber(value: self.prizepool))!)) strings.append(("Earnings", Formatter.currency.string(from: NSNumber(value: self.firstPrize))!)) var all = "" strings.forEach { (tuple) in let table = [tuple.0, tuple.1] let joined = table.joined(separator: "") all.append("") all.append(joined) all.append("\n") } return all } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } fileprivate let FONTSIZE: CGFloat = 18.0 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell() cell.backgroundColor = UIColor.clear var cells: (String, String) switch indexPath.row { case 0: cells = ("Entries", NumberFormatter().string(from: NSNumber(value: self.entries))!) case 1: cells = ("Buy-in", Formatter.currency.string(from: NSNumber(value: self.buyin))!) case 2: cells = ("Prizepool", Formatter.currency.string(from: NSNumber(value: self.prizepool))!) default: cells = ("Earnings", Formatter.currency.string(from: NSNumber(value: self.firstPrize))!) } var leftAnchor = cell.contentView.leftAnchor for i in 0..<2 { let labelContainer: UIView = UIView() cell.contentView.addSubview(labelContainer) labelContainer.translatesAutoresizingMaskIntoConstraints = false labelContainer.topAnchor.constraint(equalTo: cell.contentView.topAnchor).isActive = true labelContainer.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor).isActive = true let leftPadding: CGFloat = (i == 0) ? 0.0 : 0.0 //16.0 labelContainer.leftAnchor.constraint(equalTo: leftAnchor, constant: leftPadding).isActive = true leftAnchor = labelContainer.rightAnchor labelContainer.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor, multiplier: 0.5).isActive = true let label: UILabel = UILabel(frame: CGRect.zero) label.textColor = UIColor.white label.backgroundColor = UIColor.clear label.text = (i == 0) ? cells.0 : cells.1 label.font = (i == 0) ? UIFont.boldSystemFont(ofSize: FONTSIZE) : UIFont.systemFont(ofSize: FONTSIZE) label.textAlignment = (i == 1) ? .right : .left labelContainer.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false label.topAnchor.constraint(equalTo: labelContainer.topAnchor).isActive = true label.bottomAnchor.constraint(equalTo: labelContainer.bottomAnchor).isActive = true label.leftAnchor.constraint(equalTo: labelContainer.leftAnchor, constant: 8.0).isActive = true label.rightAnchor.constraint(equalTo: labelContainer.rightAnchor, constant: -8.0).isActive = true } return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 36.0 } func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { return 0.1 } // MARK: - UICollectionViewDataSource func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 4 } 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 = "Entries" value = NumberFormatter().string(from: NSNumber(value: self.entries))! case 1: name = "Buy-in".uppercased() value = Formatter.currency.string(from: NSNumber(value: self.buyin))! case 2: name = "Prizepool".uppercased() value = Formatter.currency.string(from: NSNumber(value: self.prizepool))! case 3: name = "First prize".uppercased() value = Formatter.currency.string(from: NSNumber(value: self.firstPrize))! 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: 380.0, height: 90.0) } }