WalletActiveCouponsScrollTableViewCell.swift 8.72 KB
//
//  WalletActiveCouponsScrollTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 8/5/24.
//

import UIKit
import SwiftEventBus

@objc public class WalletActiveCouponsScrollTableViewCell: UITableViewCell {
    @IBOutlet weak var sectionTitleLabel: UILabel!
    @IBOutlet weak var historyButtonImage: UIImageView!
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var dotLeft: UIView!
    @IBOutlet weak var dotLeftWidth: NSLayoutConstraint!
    @IBOutlet weak var dotLeftHeight: NSLayoutConstraint!
    @IBOutlet weak var dotRight: UIView!
    @IBOutlet weak var dotRightWidth: NSLayoutConstraint!
    @IBOutlet weak var dotRightHeight: NSLayoutConstraint!
    
    var parent: UIViewController?
    public var activeCouponBadges:Array<swiftApi.WalletActiveCouponBadgeModel> = []
    
    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        // Section Header
        sectionTitleLabel.text = "Κουπόνια"
        historyButtonImage.image = UIImage(named: "wallet_history_blue", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        historyButtonImage.image = historyButtonImage.image?.withRenderingMode(.alwaysTemplate)
        historyButtonImage.tintColor = UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)
        
        collectionView.contentInset.left = 17
        collectionView.contentInset.right = 17
        
        dotLeftWidth.constant = 12
        dotLeftHeight.constant = 12
        dotLeft.layer.cornerRadius = 6
        
        dotRightWidth.constant = 8
        dotRightHeight.constant = 8
        dotRight.layer.cornerRadius = 4
        
    }

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

        // Configure the view for the selected state
    }
    
    func configureCell(dfyCount: Int, smCount: Int, gfyCount: Int, boxCount: Int) {
        sortBadges(dfyCount, smCount, gfyCount, boxCount)
    }
    
    func sortBadges(_ dfyCount: Int, _ smCount: Int, _ gfyCount: Int, _ boxCount: Int) {
//        deals weight 3
//        sm weight 2
//        free weight 1
//        box weight 0
        
        let DFYBadge = swiftApi.WalletActiveCouponBadgeModel()
        DFYBadge._id = "dfy"
        DFYBadge._count = dfyCount
        DFYBadge._weight = 3
        
        let SMBadge = swiftApi.WalletActiveCouponBadgeModel()
        SMBadge._id = "sm"
        SMBadge._count = smCount
        SMBadge._weight = 2
        
        let GFYBadge = swiftApi.WalletActiveCouponBadgeModel()
        GFYBadge._id = "gfy"
        GFYBadge._count = gfyCount
        GFYBadge._weight = 1
        
        let BOXBadge = swiftApi.WalletActiveCouponBadgeModel()
        BOXBadge._id = "box"
        BOXBadge._count = boxCount
        BOXBadge._weight = 0
        
        let tempActiveCouponBadges:Array<swiftApi.WalletActiveCouponBadgeModel> = [DFYBadge, SMBadge, GFYBadge, BOXBadge]
//        var tempActiveCouponBadges:Array<swiftApi.WalletActiveCouponBadgeModel> = []
//        tempActiveCouponBadges.append(DFYBadge)
//        tempActiveCouponBadges.append(SMBadge)
//        tempActiveCouponBadges.append(GFYBadge)
//        tempActiveCouponBadges.append(BOXBadge)
        
        let sortedActiveCouponBadges = tempActiveCouponBadges.sorted {
            ($0._weight) > ($1._weight)
        }
        .sorted {
            ($0._count) > ($1._count)
        }
        
        activeCouponBadges = sortedActiveCouponBadges
        
        collectionView.reloadData();
        
    }
    
}

extension WalletActiveCouponsScrollTableViewCell: UIScrollViewDelegate {
    
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let contentOffsetX = scrollView.contentOffset.x
        if contentOffsetX >= (scrollView.contentSize.width - scrollView.bounds.width) - 20 {
            
            dotLeftWidth.constant = 8
            dotLeftHeight.constant = 8
            dotLeft.layer.cornerRadius = 4
            
            dotRightWidth.constant = 12
            dotRightHeight.constant = 12
            dotRight.layer.cornerRadius = 6
            
        } else {
            dotLeftWidth.constant = 12
            dotLeftHeight.constant = 12
            dotLeft.layer.cornerRadius = 6
            
            dotRightWidth.constant = 8
            dotRightHeight.constant = 8
            dotRight.layer.cornerRadius = 4
        }
        
    }
}

extension WalletActiveCouponsScrollTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate {
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return activeCouponBadges.count
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let badgeId = activeCouponBadges[indexPath.row]._id;
        let badgeCount = activeCouponBadges[indexPath.row]._count;
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WalletActiveCouponCollectionViewCellId", for: indexPath) as! WalletActiveCouponCollectionViewCell
        cell.configureCell(badgeId: badgeId, badgeCount: badgeCount)
        return cell;
    }
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let badgeId = activeCouponBadges[indexPath.row]._id
        
        if (badgeId == "dfy") {
            print("Active DFY coupon banner Tapped!")
            
            swiftApi().logTrackersEvent("click", ("LoyaltyWalletScreen:" + "ActiveDealsBanner"))
            
            let couponDetails = swiftApi.ActiveDFYCouponEventModel()
            couponDetails._isPressed = true
            SwiftEventBus.post("dfy_coupon_details", sender: couponDetails)
            
        } else if (badgeId == "sm") {
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_market_active_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty Wallet")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyWalletScreen:" + "ActiveMarketBanner"))
            
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
            let vc = storyboard.instantiateViewController(withIdentifier: "UnifiedCouponsViewController") as! SwiftWarplyFramework.UnifiedCouponsViewController
            parent?.navigationController?.pushViewController(vc, animated: true)
            
        } else if (badgeId == "gfy") {
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_gifts_for_you_active_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty Wallet")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyWalletScreen:" + "ActiveLoyaltyBanner"))
            
            let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: SwiftWarplyFramework.CouponsViewController.self))
            if let vc = storyboard.instantiateViewController(withIdentifier: "CouponsViewController") as? SwiftWarplyFramework.CouponsViewController{
                parent?.navigationController?.pushViewController(vc,animated: true)
            }
            
        } else if (badgeId == "box") {
            print("Active BOX coupon banner Tapped!")
            
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_box_active_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty Wallet")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyWalletScreen:" + "ActiveBoxBanner"))
            
//            SwiftEventBus.post("box_coupon_details")
            
            let couponDetails = swiftApi.ActiveBoxCouponEventModel()
            couponDetails._isPressed = true
            SwiftEventBus.post("box_coupon_details", sender: couponDetails)
            
        }
    }
    
    // Distance Between Item Cells
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5
    }
    
    // Cell Margin
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0)
    }
    
}