AnalysisHeaderMessageViewCell.swift 6.35 KB
//
//  AnalysisHeaderMessageViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 18/7/22.
//

import UIKit

class AnalysisHeaderMessageViewCell: UITableViewCell {

    // attributes
    @IBOutlet weak var itemImage: UIImageView!
    @IBOutlet weak var messageLabelView: UIView!
    @IBOutlet weak var messageLabel: UILabel!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var emptyLabel: UILabel!
    @IBOutlet weak var emptyLabelHeight: NSLayoutConstraint!
    @IBOutlet weak var emptyLabelTopSpace: NSLayoutConstraint!
    @IBOutlet weak var emptyLabelBottomSpace: NSLayoutConstraint!
    
    public var loyaltyBadge:swiftApi.LoyaltyBadgeModel = swiftApi().getLoyaltyBadge()
    
    // lifecycle
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // image
//        itemImage.image = UIImage(named: "ic_gift_circle_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        // title
        titleLabel.textColor = UIColor(rgb: 0x212121)
        titleLabel.text = "Αναλυτικά:"
        
        // message
        messageLabel.textColor = UIColor(rgb: 0x212121)
        messageLabelView.layer.borderWidth = 1.0
        messageLabelView.layer.borderColor = UIColor(rgb: 0xE6E6E6).cgColor
        messageLabelView.layer.cornerRadius = 16.5
        messageLabelView.backgroundColor = .white

//        messageLabel.text = "Μέχρι τώρα έχεις κερδίσει " + totalCouponDiscountString + "€ σε προσφορές από " + String(loyaltyBadge._couponCount) + " κουπόνια!"

    }
}

extension AnalysisHeaderMessageViewCell {
    func configureCell(isEmpty: Bool, isMarket: Bool) {
        
        if (isEmpty == true) {
            emptyLabel.isHidden = false
            emptyLabelHeight.constant = 18.5
            emptyLabelTopSpace.constant = 30.0
            emptyLabelBottomSpace.constant = 10.0
        } else {
            emptyLabel.isHidden = true
            emptyLabelHeight.constant = 0.0
            emptyLabelTopSpace.constant = 0.0
            emptyLabelBottomSpace.constant = 0.0
        }

        if (isMarket == true) {
            itemImage.image = UIImage(named: "ic_history_market", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
            
            let oldUnifiedCouponList: Array<swiftApi.CouponItemModel> = swiftApi().getOldUnifiedCouponList()
            let oldUnifiedCouponListLength = oldUnifiedCouponList.count
            var unifiedCouponsDiscount: Float = 0.0
            
            for coupon in oldUnifiedCouponList {
                if let discountFloat = Float(coupon.discount ?? "0.0") {
                    unifiedCouponsDiscount += discountFloat
                }
            }
            
            let totalCouponDiscount = Float(round(100 * unifiedCouponsDiscount) / 100)
            var totalCouponDiscountString = "0"
            totalCouponDiscountString  = String(format: "%.2f", totalCouponDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
            
            let coupNormalText1 = "Μέχρι τώρα έχεις κερδίσει "
            let coupBoldText  = totalCouponDiscountString + "€"
            let coupNormalText2 = " σε προσφορές από "
            let coupBoldText2  = String(oldUnifiedCouponListLength)
            let coupNormalText3 = " κουπόνια!"

            let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 17) ?? UIFont.systemFont(ofSize: 16)]
            let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 17) ?? UIFont.boldSystemFont(ofSize: 16)]
            
            let coupAttributedString = NSMutableAttributedString(string:coupNormalText1, attributes:attrRegular)
            let coupBoldString = NSMutableAttributedString(string: coupBoldText, attributes:attrBold)
            let coupNormalString2 = NSMutableAttributedString(string:coupNormalText2, attributes:attrRegular)
            let coupBoldString2 = NSMutableAttributedString(string: coupBoldText2, attributes:attrBold)
            let coupNormalString3 = NSMutableAttributedString(string:coupNormalText3, attributes:attrRegular)

            coupAttributedString.append(coupBoldString)
            coupAttributedString.append(coupNormalString2)
            coupAttributedString.append(coupBoldString2)
            coupAttributedString.append(coupNormalString3)
            messageLabel.attributedText = coupAttributedString
            
        } else {
            itemImage.image = UIImage(named: "ic_gift_history", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
            
            let totalCouponDiscount = Float(round(100 * loyaltyBadge._value) / 100)
            var totalCouponDiscountString = "0"
            totalCouponDiscountString  = String(format: "%.2f", totalCouponDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
            
            let coupNormalText1 = "Μέχρι τώρα έχεις κερδίσει "
            let coupBoldText  = totalCouponDiscountString + "€"
            let coupNormalText2 = " σε προσφορές από "
            let coupBoldText2  = String(loyaltyBadge._couponCount)
            let coupNormalText3 = " κουπόνια!"

            let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 17) ?? UIFont.systemFont(ofSize: 16)]
            let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Bold", size: 17) ?? UIFont.boldSystemFont(ofSize: 16)]
            
            let coupAttributedString = NSMutableAttributedString(string:coupNormalText1, attributes:attrRegular)
            let coupBoldString = NSMutableAttributedString(string: coupBoldText, attributes:attrBold)
            let coupNormalString2 = NSMutableAttributedString(string:coupNormalText2, attributes:attrRegular)
            let coupBoldString2 = NSMutableAttributedString(string: coupBoldText2, attributes:attrBold)
            let coupNormalString3 = NSMutableAttributedString(string:coupNormalText3, attributes:attrRegular)

            coupAttributedString.append(coupBoldString)
            coupAttributedString.append(coupNormalString2)
            coupAttributedString.append(coupBoldString2)
            coupAttributedString.append(coupNormalString3)
            messageLabel.attributedText = coupAttributedString
        }
        
    }
}