WalletBannersTableViewCell.swift 9.92 KB
//
//  WalletBannersTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 24/4/23.
//

import UIKit

@objc public class WalletBannersTableViewCell: UITableViewCell {

    @IBOutlet weak var sumBannerArrowImage: UIImageView!
    @IBOutlet weak var sumBannerInnerView: UIView!
    @IBOutlet weak var sumBadgeImage: UIImageView!
    @IBOutlet weak var sumBadgeLabel: UILabel!
    @IBOutlet weak var sumBannerLabel: UILabel!
    
    @IBOutlet weak var dfyBannerView: UIView!
    @IBOutlet weak var dfyBannerHeight: NSLayoutConstraint!
    @IBOutlet weak var dfyBannerTopSpace: NSLayoutConstraint!
    @IBOutlet weak var dfyBannerInnerView: UIView!
    @IBOutlet weak var dfyBadgeImage: UIImageView!
    @IBOutlet weak var dfyBadgeLabel: UILabel!
    @IBOutlet weak var dfyBannerLabel: UILabel!
    
    @IBOutlet weak var gfyBannerView: UIView!
    @IBOutlet weak var gfyBannerHeight: NSLayoutConstraint!
    @IBOutlet weak var gfyBannerTopSpace: NSLayoutConstraint!
    @IBOutlet weak var gfyBannerInnerView: UIView!
    @IBOutlet weak var couponBadgeImage: UIImageView!
    @IBOutlet weak var couponBadgeLabel: UILabel!
    @IBOutlet weak var gfyBannerLabel: UILabel!
    
    @IBOutlet weak var marketBannerView: UIView!
    @IBOutlet weak var marketBannerHeight: NSLayoutConstraint!
    @IBOutlet weak var marketBannerTopSpace: NSLayoutConstraint!
    @IBOutlet weak var marketBannerInnerView: UIView!
    @IBOutlet weak var marketBadgeImage: UIImageView!
    @IBOutlet weak var marketBadgeLabel: UILabel!
    @IBOutlet weak var marketBannerLabel: UILabel!
    
    public var loyaltyBadge:swiftApi.LoyaltyBadgeModel = swiftApi().getLoyaltyBadge()
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        sumBadgeImage.image = UIImage(named: "wallet_summary_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        dfyBadgeImage.image = UIImage(named: "wallet_dfy_3", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        couponBadgeImage.image = UIImage(named: "wallet_coupons_4", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        marketBadgeImage.image = UIImage(named: "wallet_market_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        sumBannerInnerView.layer.cornerRadius = 16.5
        sumBannerInnerView.layer.borderWidth = 1
        sumBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        dfyBannerInnerView.layer.cornerRadius = 16.5
        dfyBannerInnerView.layer.borderWidth = 1
        dfyBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        gfyBannerInnerView.layer.cornerRadius = 16.5
        gfyBannerInnerView.layer.borderWidth = 1
        gfyBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        marketBannerInnerView.layer.cornerRadius = 16.5
        marketBannerInnerView.layer.borderWidth = 1
        marketBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        dfyBannerView.isHidden = true
        dfyBannerHeight.constant = 0.0
        dfyBannerTopSpace.constant = 0.0
        
        gfyBannerView.isHidden = true
        gfyBannerHeight.constant = 0.0
        gfyBannerTopSpace.constant = 0.0
        
        marketBannerView.isHidden = true
        marketBannerHeight.constant = 0.0
        marketBannerTopSpace.constant = 0.0
        
    }

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

        // Configure the view for the selected state
    }
    
    func configureCell(totalCouponValue: Float, totalCouponDiscount: Float, unifiedCouponsDiscount: Float, forYouExpanded: Bool) {
        
        if (forYouExpanded == false) {
            sumBannerArrowImage.image = UIImage(named: "ic_down_dark_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
            
            dfyBannerView.isHidden = true
            dfyBannerHeight.constant = 0.0
            dfyBannerTopSpace.constant = 0.0
            
            gfyBannerView.isHidden = true
            gfyBannerHeight.constant = 0.0
            gfyBannerTopSpace.constant = 0.0
            
            marketBannerView.isHidden = true
            marketBannerHeight.constant = 0.0
            marketBannerTopSpace.constant = 0.0
            
        } else {
            sumBannerArrowImage.image = UIImage(named: "ic_up_dark_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
            
            dfyBannerView.isHidden = false
            dfyBannerHeight.constant = 80.0
            dfyBannerTopSpace.constant = 8.0
            
            gfyBannerView.isHidden = false
            gfyBannerHeight.constant = 80.0
            gfyBannerTopSpace.constant = 8.0
            
            marketBannerView.isHidden = false
            marketBannerHeight.constant = 80.0
            marketBannerTopSpace.constant = 8.0
        }
        
        if (totalCouponValue == 0.0 && loyaltyBadge._couponCount == 0 && unifiedCouponsDiscount == 0.0) {
            sumBannerArrowImage.isHidden = true
        } else {
            sumBannerArrowImage.isHidden = false
        }
        
        // ===
        
        let sumRewards = totalCouponValue + totalCouponDiscount + unifiedCouponsDiscount
        
//        if (sumRewards == 0.0) {
//            sumBannerArrowImage.isHidden = true
//        } else {
//            sumBannerArrowImage.isHidden = false
//        }
        
//        sumRewards = Float(round(100 * sumRewards) / 100)
        var sumRewardsString = "0"
        sumRewardsString = String(format: "%.2f", sumRewards).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        sumBadgeLabel.text = sumRewardsString + "€"
        
        let sumNormalText1 = "Μέχρι τώρα έχεις κερδίσει "
        let sumBoldText  = sumRewardsString + "€"
        let sumNormalText2 = " στο For You!"

        let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 14) ?? UIFont.systemFont(ofSize: 13), NSAttributedString.Key.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)]
        let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 14) ?? UIFont.boldSystemFont(ofSize: 13), NSAttributedString.Key.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)]
        
        let sumAttributedString = NSMutableAttributedString(string:sumNormalText1, attributes:attrRegular)
        let sumBoldString = NSMutableAttributedString(string: sumBoldText, attributes:attrBold)
        let sumNormalString = NSMutableAttributedString(string:sumNormalText2, attributes:attrRegular)

        sumAttributedString.append(sumBoldString)
        sumAttributedString.append(sumNormalString)
        sumBannerLabel.attributedText = sumAttributedString
        
        // ===
        
        let totalCouponValueFixed = Float(round(100 * totalCouponValue) / 100)
        var totalCouponValueString = "0"
        totalCouponValueString = String(format: "%.2f", totalCouponValueFixed).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        dfyBadgeLabel.text = totalCouponValueString + "€"
        
        let dfyNormalText1 = "Έχεις κερδίσει "
        let dfyBoldText  = totalCouponValueString + "€"
        let dfyNormalText2 = " με το DEALS for YOU!"

        let dfyAttributedString = NSMutableAttributedString(string:dfyNormalText1, attributes:attrRegular)
        let dfyBoldString = NSMutableAttributedString(string: dfyBoldText, attributes:attrBold)
        let dfyNormalString = NSMutableAttributedString(string:dfyNormalText2, attributes:attrRegular)

        dfyAttributedString.append(dfyBoldString)
        dfyAttributedString.append(dfyNormalString)
        dfyBannerLabel.attributedText = dfyAttributedString
        
        // ===
        
        var totalCouponDiscountString = "0"
        totalCouponDiscountString  = String(format: "%.2f", totalCouponDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        couponBadgeLabel.text = totalCouponDiscountString + "€"
        
        let gfyNormalText1 = "Έχεις κερδίσει "
        let gfyBoldText  = totalCouponDiscountString + "€"
        let gfyNormalText2 = " με το GIFTS for YOU!"

        let gfyAttributedString = NSMutableAttributedString(string:gfyNormalText1, attributes:attrRegular)
        let gfyBoldString = NSMutableAttributedString(string: gfyBoldText, attributes:attrBold)
        let gfyNormalString = NSMutableAttributedString(string:gfyNormalText2, attributes:attrRegular)

        gfyAttributedString.append(gfyBoldString)
        gfyAttributedString.append(gfyNormalString)
        gfyBannerLabel.attributedText = gfyAttributedString
        
        // ===
        
        let marketDiscount = Float(round(100 * unifiedCouponsDiscount) / 100)
        var marketDiscountString = "0"
        marketDiscountString  = String(format: "%.2f", marketDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
    
        self.marketBadgeLabel.text = marketDiscountString + "€"
        
        let marketNormalText1 = "Έχεις κερδίσει "
        let marketBoldText  = marketDiscountString + "€"
        let marketNormalText2 = " με τα SuperMarket Deals!"

        let marketAttributedString = NSMutableAttributedString(string:marketNormalText1, attributes:attrRegular)
        let marketBoldString = NSMutableAttributedString(string: marketBoldText, attributes:attrBold)
        let marketNormalString = NSMutableAttributedString(string:marketNormalText2, attributes:attrRegular)

        marketAttributedString.append(marketBoldString)
        marketAttributedString.append(marketNormalString)
        marketBannerLabel.attributedText = marketAttributedString
        
    }

}