// // P2PModelCard.swift // Wallet // // Created by Igor on 16.12.2020. // Copyright © 2020 AM. All rights reserved. // import UIKit extension Cards { struct OldModel: Codable { let uuid: String let number: String let country: String let bank: String let type: Cards.Model.Kind } struct Model: Codable { let uuid: String let number: String let countryId: Int let country: String let bankId: Int let bank: String let type: Kind } } extension Cards.Model { enum Kind: String, Codable { case VISA case MASTERCARD case AMEX case DISCOVER case DANKORT case MAESTRO case DINERS case LASER case JCB case UNIONPAY case UNKNOWN } } extension Cards.Model { static func formmated(number: String) -> String { let number = cleaned(number: number) var index = 0 var string = "" var collection = [String]() number.forEach({ string += "\($0)" index += 1 if index == 4 { index = 0 collection.append(string) string = "" } }) if string.count > 0 { collection.append(string) } return collection.joined(separator: " ") } static func cleaned(number: String) -> String { number.replacingOccurrences(of: " ", with: "") } var secured: String { ["****", Self.cleaned(number: number).suffix(4)].joined(separator: " ") } var image: UIImage { let image = Asset.cardUnknown.image.tinted(with: Asset.deepWater.color)! return type == .UNKNOWN ? image : (UIImage(named: "card-\(type.rawValue.lowercased())") ?? image) } } extension CommonMenuAction { static func cards( _ cards: [Cards.Model] = Cards.shared.collection, selected: Cards.Model? = nil, empty: Common.Model.Menu? = Cards.Model.empty(), title: String? = L10n.Common.Card.Card.select, in viewController: UIViewController? = nil, _ completion: ((Common.Model.Menu) -> Void)? = nil ) -> CommonMenuAction { if let viewController = viewController, cards.isEmpty { return .action(icon: Asset.commonArrowRight.image, empty: Cards.Model.empty()) { _ in viewController.navigationController?.pushViewController(StoryboardScene.Cards.initial.instantiate(), animated: true) } } else { return .select( icon: Asset.commonMenuHorizontal.image, kind: .cards, title: title, empty: empty, collection: cards.map({ $0.toMenu() }), selected: selected?.toMenu(), completion: completion ) } } } extension Cards.Model: CommonMenuMappable { static func empty() -> Common.Model.Menu { .menu( image: Asset.commonCard.image.tinted(with: Asset.brick.color), title: L10n.Common.Card.Card.Empty.text, details: L10n.Common.Card.Card.Empty.title ) } func toMenu() -> Common.Model.Menu { .menu( uuid: uuid, image: image, title: bank, text: country, details: secured, badge: Cards.shared.default?.uuid == self.uuid ? (L10n.Cards.Card.primary, Asset.forest.color) : nil ) } }