UnifiedCouponsTableViewCell.swift 2.93 KB
//
//  UnifiedCouponsTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 3/4/23.
//

import Foundation
import UIKit

@objc public class UnifiedCouponsTableViewCell: UITableViewCell {
    @IBOutlet weak var couponBgImage: UIImageView!
    @IBOutlet weak var couponImage: UIImageView!
    @IBOutlet weak var borderView: UIView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var discriptionLabel: UILabel!
    @IBOutlet weak var expirationLabel: UILabel!
    
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        // Add shadow
        self.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 1.0
        
        couponBgImage.image = UIImage(named: "coupon_bg_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        couponImage.image = UIImage(named: "coupon_market_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)

        
        borderView.addDashedBorderVertical(color: UIColor(red: 0.62, green: 0.62, blue: 0.61, alpha: 1.00), width: 1.0, height: 110.0)
    }

    public override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        //set the values for top,left,bottom,right margins
        let margins = UIEdgeInsets(top: 0, left: 0, bottom: 8, right: 0)
        contentView.frame = contentView.frame.inset(by: margins)
    }
    
    
    func configureCell(coupon: swiftApi.UnifiedCouponModel) {
        let activeCoupons = coupon._coupons.filter { $0.status == 1 }
        let activeCouponsCount = activeCoupons.count
        // let activeCouponsDiscount = activeCoupons.reduce(0) { $0 + (Float($1.discount ?? "0") ?? 0) }
        
//        var activeCouponsCount:Int = 0
//        for coupon in coupon._coupons {
//            if (coupon.status == 1) {
//                activeCouponsCount += 1
//            }
//        }
        
        nameLabel.text = "COSMOTE SUPERMARKET DEALS"
        
        discriptionLabel.text =
        "έχεις " + String(activeCouponsCount) +
        (
            activeCouponsCount == 1 ? " ενεργό κουπόνι" : " ενεργά κουπόνια"
        )
        // + " αξίας " + String(activeCouponsDiscount) + "€"
        
//        if let earliestExpiration = activeCoupons.min(by: { ($0.expiration ?? "") < ($1.expiration ?? "") }) {
        if let latestExpiration = activeCoupons.max(by: { ($0.expiration ?? "") < ($1.expiration ?? "") }) {
            expirationLabel.text = "Ισχύει έως " + (latestExpiration.expiration ?? "")
        } else {
            expirationLabel.text = ""
        }
    }

}