PopupMerchantsViewController.swift 8.03 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: DynamicSizeMerchantsTableView!
    
    public var availableShopsList:Array<swiftApi.ShopAvailabilityItemModel> = []
//    public var coupon: swiftApi.CouponItemModel?
    public var headerText: String? = "Βρες το προϊον στα supermarket"
    public var buttonTitle: String? = "Βρες το"
    public var buttonIcon: String?
    public var footerText: String? = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
    public var showEshops: Bool? = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if (showEshops == true) {
            setupEshopsData()
        }
        
        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)
        
//        setupFooterView()
    }
    
    // MARK: - Functions
    func setupEshopsData() {
        let tempShop1: swiftApi.ShopAvailabilityItemModel = swiftApi.ShopAvailabilityItemModel()
        tempShop1._merchant_uuid = "ab"
        tempShop1._product_url = "https://www.ab.gr"
        availableShopsList.append(tempShop1)
        
        let tempShop2: swiftApi.ShopAvailabilityItemModel = swiftApi.ShopAvailabilityItemModel()
        tempShop2._merchant_uuid = "masoutis"
        tempShop2._product_url = "https://www.masoutis.gr"
        availableShopsList.append(tempShop2)
    }
    
//    private func setupFooterView() {
//        // Create a footer view container
//        let footerView = UIView()
//        footerView.backgroundColor = .clear
//        
//        // Create a label for the footer
//        let footerLabel = UILabel()
//        footerLabel.text = "Η διαθεσιμότητα ενδέχεται να διαφέρει ανάλογα με την τοποθεσία."
//        footerLabel.textAlignment = .center
//        footerLabel.font = UIFont(name: "PeridotPE-Regular", size: 13)
//        footerLabel.textColor = UIColor(rgb: 0x000000)
//        footerLabel.numberOfLines = 0  // Allow for multiline text
//        
//        // Add label to the footer view
//        footerView.addSubview(footerLabel)
//        footerLabel.translatesAutoresizingMaskIntoConstraints = false
//        
//        // Set constraints for the label inside the footer view
//        NSLayoutConstraint.activate([
//            footerLabel.leadingAnchor.constraint(equalTo: footerView.leadingAnchor, constant: 16),
//            footerLabel.trailingAnchor.constraint(equalTo: footerView.trailingAnchor, constant: -16),
//            footerLabel.topAnchor.constraint(equalTo: footerView.topAnchor, constant: 10),
//            footerLabel.bottomAnchor.constraint(equalTo: footerView.bottomAnchor, constant: -10)
//        ])
//        
//        // Set the footer view's frame size to fit the content
//        footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
//
//        // Assign the footer view to the table view's footer
//        tableView.tableFooterView = footerView
//    }
    
    // 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.availableShopsList.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(shop: availableShopsList[indexPath.row], buttonTitle: self.buttonTitle ?? "Βρες το", iconImage: self.buttonIcon, showEshops: self.showEshops)
            
            // 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) {
        // Do nothing - tap handled with protocol
//        print(" didSelectRowAt Button tapped in row \(indexPath.row)")
        
    }
    
    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) {
            let urlString = availableShopsList[indexPath.row]._product_url
            if let url = URL(string: urlString){
                if UIApplication.shared.canOpenURL(url) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
        
    }
    
}

// Class for dynamic height table view
@objc public class DynamicSizeMerchantsTableView: UITableView
{
    override public func layoutSubviews() {
        super.layoutSubviews()
        if bounds.size != intrinsicContentSize {
            invalidateIntrinsicContentSize()
        }
    }

    // override public var intrinsicContentSize: CGSize {
    //     return contentSize
    // }

    override public var intrinsicContentSize: CGSize {
        return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height + contentInset.top + contentInset.bottom + 30)
    }
}