MyRewardsOffersScrollTableViewCell.swift 6.72 KB
//
//  MyRewardsOffersScrollTableViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 23/5/25.
//

import UIKit

protocol MyRewardsOffersScrollTableViewCellDelegate: AnyObject {
    func didSelectOffer(_ offer: OfferModel)
}

@objc public class MyRewardsOffersScrollTableViewCell: UITableViewCell {
    @IBOutlet weak var categoryLabel: UILabel!
    @IBOutlet weak var allButtonView: UIView!
    @IBOutlet weak var allButtonLabel: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
    
    weak var delegate: MyRewardsOffersScrollTableViewCellDelegate?
    var data: SectionModel?

    public override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        allButtonView.layer.borderWidth = 1.0
        allButtonView.layer.borderColor = UIColor(rgb: 0x000F1E).cgColor
        allButtonView.layer.cornerRadius = 4.0
        
        allButtonLabel.text = "Όλα"
        allButtonLabel.font = UIFont(name: "PingLCG-Regular", size: 16)
        allButtonLabel.textColor = UIColor(rgb: 0x00111B)
        allButtonLabel.frame.size.width = allButtonLabel.intrinsicContentSize.width
        allButtonLabel.frame.size.height = allButtonLabel.intrinsicContentSize.height
        
        // Register XIBs for collection view cells
        collectionView.register(UINib(nibName: "MyRewardsOfferCollectionViewCell", bundle: Bundle(for: MyEmptyClass.self)), forCellWithReuseIdentifier: "MyRewardsOfferCollectionViewCell")

        // Fix background colors
        collectionView.backgroundColor = UIColor.clear
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = UIColor.clear
        
        // Remove content insets and gaps
        collectionView.contentInset = UIEdgeInsets.zero
        collectionView.scrollIndicatorInsets = UIEdgeInsets.zero

        if #available(iOS 11.0, *) {
            collectionView.contentInsetAdjustmentBehavior = .never
        }

        // Configure collection view layout
        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            layout.scrollDirection = .horizontal
            layout.minimumLineSpacing = 7
            layout.minimumInteritemSpacing = 0
            layout.sectionInset = UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
        }
        
        // Enable paging for smooth banner scrolling
        // collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        
        // Set delegates
        collectionView.delegate = self
        collectionView.dataSource = self

    }

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

        // Configure the view for the selected state
    }
    
    func configureCell(data: SectionModel?) {
        self.data = data

        // Update collection view height based on section
        if data?.title == "Αγαπημένα" {
            collectionViewHeightConstraint.constant = 350 // Match cell height
        } else {
            collectionViewHeightConstraint.constant = 232 // Default height
        }

        // Configure layout based on section type
        if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            if data?.title == "Αγαπημένα" {
                layout.minimumLineSpacing = 24
            } else {
                layout.minimumLineSpacing = 7
            }
        }

        let catBoldText = (data?.title ?? "") + " "
        let catRegText = "(" + String(data?.count ?? 0) + ")"
        
        let attrBold = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Bold", size: 18) ?? UIFont.boldSystemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
        let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
        
        let catAttributedString = NSMutableAttributedString(string:catBoldText, attributes:attrBold)
        let catRegString = NSMutableAttributedString(string: catRegText, attributes:attrRegular)

        catAttributedString.append(catRegString)
        categoryLabel.attributedText = catAttributedString
        
        self.collectionView.reloadData();
        
    }
}

extension MyRewardsOffersScrollTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data?.offers.count ?? 0
    }

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyRewardsOfferCollectionViewCell", for: indexPath) as! MyRewardsOfferCollectionViewCell
//        cell.configureCell(offer: self.data?.offers[indexPath.row])
        if let offer = self.data?.offers[indexPath.row] {
            cell.configureCell(data: offer)
        }
        return cell;
    }
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if let offer = self.data?.offers[indexPath.row] {
            delegate?.didSelectOffer(offer)
        }
    }

    // MARK: - UICollectionViewDelegateFlowLayout
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if self.data?.title == "Αγαπημένα" {
            return CGSize(width: 275, height: 350)
        } else {
            return CGSize(width: 257, height: 232)
        }
    }

    // Distance Between Item Cells
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        if self.data?.title == "Αγαπημένα" {
            return 24
        } else {
            return 7
        }
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    // Cell Margin
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
    }
}