MarketAnalysisItemViewCell.swift 5.9 KB
//
//  MarketAnalysisItemViewCell.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 14/1/25.
//

import UIKit

class MarketAnalysisItemViewCell: UITableViewCell {

    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var itemImage: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    @IBOutlet weak var productTitleLabel: UILabel!
    
    var postImageURL: String? {
        didSet {
            if let url = postImageURL {
                self.itemImage.image = UIImage() // UIImage(named: "loading")

                UIImage.loadImageUsingCacheWithUrlString(url) { image in
                    // set the image only when we are still displaying the content for the image we finished downloading
                    if url == self.postImageURL {
                        self.itemImage.image = image
                    }
                }
            }
            else {
                self.itemImage.image = nil
            }
        }
    }
    
    // lifecycle
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // date
        dateLabel.textColor = UIColor(rgb: 0x212121)
        dateLabel.font = UIFont(name: "PeridotPE-Bold", size: 12.0)
        
        // title
        titleLabel.textColor = UIColor(rgb: 0x212121)
        titleLabel.font = UIFont(name: "PeridotPE-SBold", size: 17.0)
        
        // price
        priceLabel.textColor = UIColor(rgb: 0x212121)
        priceLabel.font = UIFont(name: "PeridotPE-Bold", size: 17.0)
        
        // subtitle
//        subtitleLabel.textColor = UIColor(rgb: 0x9D9D9C)
        subtitleLabel.textColor = UIColor(rgb: 0x212121)
        subtitleLabel.font = UIFont(name: "PeridotPE-SBold", size: 14.0)
        
        // productTitleLabel
        productTitleLabel.textColor = UIColor(rgb: 0x212121)
        productTitleLabel.font = UIFont(name: "PeridotPE-Regular", size: 14.0)
    }
}

extension MarketAnalysisItemViewCell {
    func configureCell(item: swiftApi.CouponItemModel) {
        
//        COUPONSET: desc, img_preview, name, terms, merchant_uuid
//        COUPON: coupon, expiration, discount, status
//        MERCHANT: _img_preview,_admin_name
                
        let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
        let couponSetData: swiftApi.CouponSetItemModel? = item.couponset_data
        
        titleLabel.text = ""
        
        // for merchant in merchantList {
        //     // if (merchant._uuid == couponSetData?.merchant_uuid) {
        //     if (merchant._uuid == item.merchant_uuid) {
        //         // itemImage.load(link: merchant._img_preview, placeholder: UIImage(), cache: URLCache())
        //         self.postImageURL = merchant._img_preview
        //         titleLabel.text = merchant._admin_name
        //         break;
        //     }
        // }

        let merchantDetails: swiftApi.MerchantModel? = item.merchant_details
        self.postImageURL = merchantDetails?._img_preview
        titleLabel.text = merchantDetails?._admin_name
        
        dateLabel.text = item.redeemed ?? "" // expiration
//         itemImage.load(link: couponSetData?.img_preview ?? "", placeholder: UIImage(), cache: URLCache())
//         titleLabel.text = couponSetData?.name ?? ""
        
        let priceFloat = Float(round(100 * (Float(couponSetData?.final_price ?? 0.0) )) / 100)
        var priceString = "0"
        priceString  = String(format: "%.2f", priceFloat).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        priceLabel.text = priceString + "€"
         
//        subtitleLabel.text = "Έκπτωτικό κουπόνι"
        subtitleLabel.text = item.redeemed_merchant_details?._name ?? ""
//        subtitleLabel.text = couponSetData?.short_description ?? ""
        // OR
//        let htmlText = couponSetData?.inner_text ?? ""
//        subtitleLabel.text = htmlText.htmlToString
        
        let htmlText = couponSetData?.inner_text ?? ""
        productTitleLabel.text = htmlText.htmlToString
        
     }
    
    func configureCell(item: swiftApi.CouponItemModel, isMarket: Bool) {
        
//        COUPONSET: desc, img_preview, name, terms, merchant_uuid
//        COUPON: coupon, expiration, discount, status
//        MERCHANT: _img_preview,_admin_name
                
        let merchantList:Array<swiftApi.MerchantModel> = swiftApi().getMerchantList()
//        let couponSetData: swiftApi.CouponSetItemModel? = item.couponset_data

        titleLabel.text = ""
        
        // for merchant in merchantList {
        //     if (merchant._uuid == item.merchant_uuid) {
        //         // itemImage.load(link: merchant._img_preview, placeholder: UIImage(), cache: URLCache())
        //         self.postImageURL = merchant._img_preview
        //         titleLabel.text = merchant._admin_name
        //         break;
        //     }
        // }

        let merchantDetails: swiftApi.MerchantModel? = item.merchant_details
        self.postImageURL = merchantDetails?._img_preview
        titleLabel.text = merchantDetails?._admin_name

//        titleLabel.text = item.name
//        self.postImageURL = item.image
        
        dateLabel.text = item.redeemed ?? ""
        
        let priceFloat = Float(round(100 * (Float(item.discount ?? "0.0") ?? 0.0 )) / 100)
        var priceString = "0"
        priceString  = String(format: "%.2f", priceFloat).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        priceLabel.text = priceString + "€"
         
//        subtitleLabel.text = "Έκπτωτικό κουπόνι"
        subtitleLabel.text = item.redeemed_merchant_details?._name ?? ""
//        subtitleLabel.text = couponSetData?.short_description ?? ""
        // OR
//        let htmlText = couponSetData?.inner_text ?? ""
//        subtitleLabel.text = htmlText.htmlToString
        
     }
}