ActiveCodeTableViewCell.swift 8.55 KB
//
//  ActiveCodeTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 3/4/23.
//

import UIKit

@objc public class ActiveCodeTableViewCell: UITableViewCell {
    @IBOutlet weak var activeCodeView: UIView!
    @IBOutlet weak var activeCodeViewHeight: NSLayoutConstraint!
    @IBOutlet weak var activeCodesCountLabel: UILabel!
    @IBOutlet weak var activeCodeLabel: UILabel!
    @IBOutlet weak var activeCodeExpirationLabel: UILabel!
    @IBOutlet weak var activeCodeImage: UIImageView!
    @IBOutlet weak var activeCodeScrollView: UIScrollView!
    @IBOutlet weak var activeCodeScrollViewHeight: NSLayoutConstraint!
    @IBOutlet weak var activeCodeContentView: UIView!
    @IBOutlet weak var activeCodeContentViewHeight: NSLayoutConstraint!
    
    public var dfyCoupons:Array<swiftApi.ActiveDFYCouponModel> = swiftApi().getActiveDFYCoupons()

    public override func awakeFromNib() {
        super.awakeFromNib()
        
        // TODO: DELETE ===>
    //    let coupon = swiftApi.ActiveDFYCouponModel()
    //    coupon._value = "12"
    // //    coupon._date = "2022-12-05 01:55:01"
    //    coupon._date = "2022-10-26 23:59:01"
    //    coupon._code = "123456789"

    //    let coupon2 = swiftApi.ActiveDFYCouponModel()
    //    coupon2._value = "23"
    //    coupon2._date = "2022-11-05 01:55"
    //    coupon2._code = "234567891"

    //    let coupon3 = swiftApi.ActiveDFYCouponModel()
    //    coupon3._value = "34"
    //    coupon3._date = "2022-07-01 01:55"
    //    coupon3._code = "345678912"

    //    let couponsArray: Array<swiftApi.ActiveDFYCouponModel> = [coupon, coupon2, coupon3, coupon3, coupon3]

    //     swiftApi().setActiveDFYCoupons(dfyCoupons: couponsArray)

    //     dfyCoupons = swiftApi().getActiveDFYCoupons()
        // TODO: DELETE <===
        
        // 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

        activeCodeView.layer.cornerRadius = 16.5
        // activeCodeView.layer.borderWidth = 1
        // activeCodeView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        activeCodeImage.image = UIImage(named: "active_code_logo_2", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        if (dfyCoupons.count > 0) {
            if (dfyCoupons.count == 1) {
                let dateFormatter = DateFormatter()
                dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
                // dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
                
                // sort dfyCoupons by date
                dfyCoupons.sort(by: {
                    let date1 = dateFormatter.date(from: $0._date)
                    let date2 = dateFormatter.date(from: $1._date)
                    
                    if ((date1 != nil) && (date2 != nil)) {
                        return date1!.compare(date2!) == .orderedAscending
                    } else {
                        return false
                    }
                    
                })
                
                // Get days from now of the most recet coupon
                var daysFromNow = ""
                let calendar = Calendar.current

                // Replace the hour (time) of both dates with 00:00
                let date1 = calendar.startOfDay(for: Date())
                if let date2 = dateFormatter.date(from: dfyCoupons[0]._date) {
                    let components = calendar.dateComponents([.day], from: date1, to: date2)
                    daysFromNow = (components.day) != nil ? String((components.day ?? 0) + 1) : ""
                }
                
                activeCodesCountLabel.text = "Ενεργός κωδικός:"
                // activeCodeLabel.text = dfyCoupons[0]._code
                let newLabel = CopyableLabel()
                newLabel.text = String(dfyCoupons[0]._code)
                newLabel.font = UIFont(name: "PeridotPE-Bold", size: 18)
                newLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
                newLabel.frame.size.width = newLabel.intrinsicContentSize.width
                newLabel.frame.size.height = newLabel.intrinsicContentSize.height // tagHeight
                activeCodeContentView.addSubview(newLabel)
                // set the btn frame origin
                newLabel.frame.origin.x = 0
                newLabel.frame.origin.y = 0
                let scrollHeight = newLabel.intrinsicContentSize.height
                activeCodeContentViewHeight.constant = scrollHeight
                activeCodeScrollViewHeight.constant = scrollHeight

                activeCodeExpirationLabel.isHidden = false
                if (daysFromNow == "1") {
                    activeCodeExpirationLabel.text = "Λήγει σε " + daysFromNow + " ημέρα"
                } else {
                    activeCodeExpirationLabel.text = "Λήγει σε " + daysFromNow + " ημέρες"
                }
                
            } else {
                var tagHeight:CGFloat = 30
                let tagPadding: CGFloat = 0
                let tagSpacingX: CGFloat = 0
                let tagSpacingY: CGFloat = 2
                
                let containerWidth = activeCodeContentView.frame.size.width
                
                var currentOriginX: CGFloat = 0
                var currentOriginY: CGFloat = 0

                // var couponCodesString = ""
                for (index, item) in dfyCoupons.enumerated() {
                    let newLabel = CopyableLabel()
                    newLabel.font = UIFont(name: "PeridotPE-Bold", size: 18)
                    newLabel.textColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)

                    if (index == (dfyCoupons.endIndex - 1)) {
                        // couponCodesString += String(item._code)
                        newLabel.text = String(item._code)
                    } else {
                        // couponCodesString += String(item._code) + ", "
                        newLabel.text = String(item._code) + ", "
                    }
                    
                    newLabel.frame.size.width = newLabel.intrinsicContentSize.width + tagPadding
                    newLabel.frame.size.height = newLabel.intrinsicContentSize.height // tagHeight
                    tagHeight = newLabel.intrinsicContentSize.height
                    activeCodeContentView.addSubview(newLabel)
                    
                    // if current X + label width will be greater than container view width
                    //  "move to next row"
                    if currentOriginX + newLabel.frame.width > containerWidth {
                        currentOriginX = 0
                        currentOriginY += tagHeight + tagSpacingY
                    }
                    
                    // set the btn frame origin
                    newLabel.frame.origin.x = currentOriginX
                    newLabel.frame.origin.y = currentOriginY
                    
                    // increment current X by btn width + spacing
                    currentOriginX += newLabel.frame.width + tagSpacingX
                }
                
                activeCodesCountLabel.text = String(dfyCoupons.count) + " Ενεργοί κωδικοί:"
                // activeCodeLabel.text = couponCodesString
                activeCodeExpirationLabel.isHidden = true

                // update container view height
                activeCodeContentViewHeight.constant = currentOriginY + tagHeight
                if ((currentOriginY + tagHeight) <= (2 * tagHeight + tagSpacingY)) {
                    activeCodeScrollViewHeight.constant = currentOriginY + tagHeight
                } else {
                    activeCodeScrollViewHeight.constant = 2 * tagHeight + tagSpacingY
                }
            }
        } else {
            activeCodeLabel.text = "-"
            activeCodeExpirationLabel.text = ""

            activeCodeView.isHidden = true
            
            activeCodeViewHeight.constant = 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: 2, left: 0, bottom: 4, right: 0)
        contentView.frame = contentView.frame.inset(by: margins)
    }

}