PopupMerchantsViewController.swift 5.52 KB
//
//  PopupSMMerchantsViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 17/10/24.
//

import UIKit

class PopupMerchantsViewController: UIViewController {
    @IBOutlet weak var popupView: UIView!
    @IBOutlet weak var headerLabel: UILabel!
    @IBOutlet weak var closeButton: UIButton!
    @IBOutlet weak var tableView: DynamicSizeTableView!
    
    public var coupon: swiftApi.CouponItemModel?
    public var headerText: String? = "Βρες το προϊον στα supermarket"
    public var buttonTitle: String? = "Βρες το"
    public var buttonIcon: String?
    public var footerText: String? = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
    
    let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tableView.delegate = self
        tableView.dataSource = self
        
        tableView.contentInset.top = 40
        
        popupView.clipsToBounds = true
        popupView.layer.cornerRadius = 16
        popupView.layer.maskedCorners = [ .layerMinXMinYCorner, .layerMaxXMinYCorner] // Top left, right corner radius
        
        closeButton.setImage(UIImage(named: "ic_close_3.png", in: MyEmptyClass.resourceBundle(), compatibleWith: nil), for: .normal)
        closeButton.imageView?.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
        
//        self.tableView.reloadData()
        self.tableView.invalidateIntrinsicContentSize()
        self.headerLabel.text = self.headerText
        headerLabel.font = UIFont(name: "BTCosmo-Bold", size: 18)
        headerLabel.textColor = UIColor(rgb: 0x000000)
    }
    
    // MARK: - UIButton Actions
    @IBAction func closeButtonAction(_ sender: Any) {
        self.dismiss(animated: true, completion: {})
    }
    
}

// MARK: - TableView
extension PopupMerchantsViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if (section == 0) {
            return self.merchantList.count
        } else if (section == 1) {
            return 1
        } else {
            return 0
        }
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (indexPath.section == 0) {
            return 56.0 + 32.0
    //        return UITableViewAutomaticDimension
        } else if (indexPath.section == 1) {
            return 50.0
        } else {
            return 0.0
        }
//        return UITableViewAutomaticDimension
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if (indexPath.section == 0) {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MerchantTableViewCellId", for: indexPath) as! MerchantTableViewCell
            cell.configureCell(merchant: merchantList[indexPath.row], buttonTitle: self.buttonTitle ?? "Βρες το", iconImage: buttonIcon)
            
            // Set the delegate to self
            cell.delegate = self
            
            return cell
            
        } else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "PopupMerchantsFooterTableViewCellId", for: indexPath) as! PopupMerchantsFooterTableViewCell
            cell.configureCell(title: self.footerText ?? "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία.")
            
            return cell
        }
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        self.dismiss(animated: true, completion: {})
        
            // TODO: DELETE LOGS
            print(" didSelectRowAt Button tapped in row \(indexPath.row)")
        
        // TODO: Open merchant url
        
        // Logs
//        let couponSetData: swiftApi.CouponSetItemModel? = coupons[indexPath.row].couponset_data
//        print("Coupon clicked: " + (coupons[indexPath.row].coupon ?? ""))
//        print("Coupon Name clicked: " + (couponSetData?.name ?? ""))
//        print("Coupon Description clicked: " + (couponSetData?.short_description ?? ""))
//        print("Coupon Expiration clicked: " + (coupons[indexPath.row].expiration ?? ""))

    }
    
    public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }

    public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.0
    }
    
    public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return CGFloat.leastNormalMagnitude
        return 0.0
    }

    public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}

// MARK: - MerchantTableViewCellDelegate
extension PopupMerchantsViewController: MerchantTableViewCellDelegate {
    
    func didTapButton(in cell: MerchantTableViewCell) {
        // Find the index path of the cell where the button was tapped
        if let indexPath = tableView.indexPath(for: cell) {
            // TODO: DELETE LOGS
            print("Button tapped in row \(indexPath.row)")
            print("Button tapped cell: \(cell)")
            // Handle the button tap action here
        }
    }
    
}