LoyaltyHistoryViewController.swift 20.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
//
//  LoyaltyHistoryViewController.swift
//  SwiftWarplyFramework
//
//  Created by Manos Chorianopoulos on 25/10/23.
//

import Foundation
import UIKit
import SwiftEventBus

@objc public class LoyaltyHistoryViewController: UIViewController {
    @IBOutlet weak var sectionTitleLabel: UILabel!
    @IBOutlet weak var historySectionView: UIView!
    
    @IBOutlet weak var sumBadgeImage: UIImageView!
    @IBOutlet weak var sumBadgeLabel: UILabel!
    @IBOutlet weak var sumBannerLabel: UILabel!
    
    @IBOutlet weak var dfyBannerParentView: UIView!
    @IBOutlet weak var dfyBannerInnerParentView: UIView!
    @IBOutlet weak var dfyBannerInnerView: UIView!
    @IBOutlet weak var dfyBadgeImage: UIImageView!
    @IBOutlet weak var dfyBadgeLabel: UILabel!
    @IBOutlet weak var dfyBannerLabel: UILabel!
    
    @IBOutlet weak var marketBannerParentView: UIView!
    @IBOutlet weak var marketBannerInnerView: UIView!
    @IBOutlet weak var marketBadgeImage: UIImageView!
    @IBOutlet weak var marketBadgeLabel: UILabel!
    @IBOutlet weak var marketBannerLabel: UILabel!
    
    @IBOutlet weak var gfyBannerParentView: UIView!
    @IBOutlet weak var gfyBannerInnerView: UIView!
    @IBOutlet weak var couponBadgeImage: UIImageView!
    @IBOutlet weak var couponBadgeLabel: UILabel!
    @IBOutlet weak var gfyBannerLabel: UILabel!
    
    @IBOutlet weak var boxBannerParentView: UIView!
    @IBOutlet weak var boxBannerInnerParentView: UIView!
    @IBOutlet weak var boxBannerInnerView: UIView!
    @IBOutlet weak var boxBadgeImage: UIImageView!
    @IBOutlet weak var boxBadgeLabel: UILabel!
    @IBOutlet weak var boxBannerLabel: UILabel!
    
    public var coupons:Array<swiftApi.CouponItemModel> = swiftApi().getCouponList()
    public var dfyCoupons:Array<swiftApi.ActiveDFYCouponModel> = swiftApi().getActiveDFYCoupons()
    public var unifiedCoupons:Array<swiftApi.UnifiedCouponModel> = []
    public var loyaltyBadge:swiftApi.LoyaltyBadgeModel = swiftApi().getLoyaltyBadge()
    public var boxCoupons:Array<swiftApi.ActiveBoxCouponModel> = swiftApi().getActiveBoxCoupons()
    
    var totalCouponValue = swiftApi().getDealsCouponsSum()
    var totalCouponDiscount = Float(round(100 * swiftApi().getLoyaltyBadge()._value) / 100)
    var unifiedCouponsDiscount:Float = 0.0
    var boxCouponValue = swiftApi().getBoxCouponsSum()
    
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        self.hidesBottomBarWhenPushed = true
        
        SwiftEventBus.onBackgroundThread(self, name: "coupons_fetched") { result in

            DispatchQueue.main.async {
                self.coupons = swiftApi().getCouponList()
                self.totalCouponDiscount = Float(round(100 * swiftApi().getLoyaltyBadge()._value) / 100)
                self.loyaltyBadge = swiftApi().getLoyaltyBadge()
//                self.tableView.reloadData()
                self.updateUI()
            }
        }
        
        SwiftEventBus.onBackgroundThread(self, name: "unified_coupons_fetched") { result in

            DispatchQueue.main.async {
                self.unifiedCoupons = swiftApi().getUnifiedCouponList()
                
                self.matchOldSMCoupons()
                self.updateMarketBadge()
//                self.tableView.reloadData()
                self.updateUI()
            }
        }
        
        SwiftEventBus.onBackgroundThread(self, name: "navigation_back") { result in

            DispatchQueue.main.async {
                self.navigationController?.popViewController(animated: true)
            }
        }
        
//        setBackButton()
        setBackButton("ic_close_3")
        setNavigationTitle("Ιστορικό")
        
        // === images === //
        
        sumBadgeImage.image = UIImage(named: "history_heart", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        dfyBadgeImage.image = UIImage(named: "history_banner_dfy", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        marketBadgeImage.image = UIImage(named: "history_banner_sm", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        couponBadgeImage.image = UIImage(named: "history_banner_gfy", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        boxBadgeImage.image = UIImage(named: "history_banner_box", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
        
        sectionTitleLabel.text = "Εξαργυρωμένα κουπόνια"
        
        historySectionView.backgroundColor = .white
        historySectionView.layer.cornerRadius = 16.0
        // historySectionView.layer.borderWidth = 1
        // historySectionView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        // Add shadow
        historySectionView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        historySectionView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        historySectionView.layer.shadowOpacity = 1.0
        historySectionView.layer.shadowRadius = 2.0
        
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        swiftApi().logTrackersEvent("screen", "LoyaltyHistoryScreen")

        // addNavShadow()
        self.navigationController?.hideHairline()
        
        self.coupons = swiftApi().getCouponList()
        self.unifiedCoupons = swiftApi().getUnifiedCouponList()
        self.dfyCoupons = swiftApi().getActiveDFYCoupons()
        self.boxCoupons = swiftApi().getActiveBoxCoupons()
        
        self.matchOldSMCoupons()
        self.updateMarketBadge()
//        self.tableView.reloadData()
        self.updateUI()
    }
    
//    public override func viewWillDisappear(_ animated: Bool) {
//        super.viewWillDisappear(animated)
//
//        self.navigationController?.navigationBar.layer.shadowOpacity = 0.0
//        
//    }
    
    // MARK: - Functions
    func updateMarketBadge() {
        unifiedCouponsDiscount = 0.0
//        for smCouponSet in swiftApi().getCouponSetsDealsList() {
//            for oldCoupon in swiftApi().getAllOldCouponList() {
//                if (smCouponSet.uuid != "" && oldCoupon.couponset_uuid != "" && smCouponSet.uuid == oldCoupon.couponset_uuid) {
//
//                    oldCoupon.setCouponSetData(smCouponSet);
//
//                     if let discountFloat = Float(oldCoupon.discount ?? "0.0") {
//                         unifiedCouponsDiscount += discountFloat
//                     }
//                    break;
//                }
//            }
//        }
        
        for coupon in swiftApi().getOldUnifiedCouponList() {
            if let discountFloat = Float(coupon.discount ?? "0.0") {
                unifiedCouponsDiscount += discountFloat
            }
        }
        
        // TODO: UNCOMMENT if emptyView is needed again
//        if (totalCouponValue == 0.0 && loyaltyBadge._couponCount == 0 && dfyCoupons.count == 0 && coupons.count == 0 && unifiedCoupons.count == 0 && unifiedCouponsDiscount == 0.0) {
//            emptyView.isHidden = false
//            emptyViewHeight.constant = emptyView.intrinsicContentSize.height
//        } else {
//            emptyView.isHidden = true
//            emptyViewHeight.constant = 0
//        }
    }
    
    func matchOldSMCoupons() {
        var oldUnifiedCouponsArray:Array<swiftApi.CouponItemModel> = []
        
        // TODO: Check - Old implementation
//        for smCouponSet in swiftApi().getCouponSetsDealsList() {
//            for oldCoupon in swiftApi().getAllOldCouponList() {
//                if (smCouponSet.uuid != "" && oldCoupon.couponset_uuid != "" && smCouponSet.uuid == oldCoupon.couponset_uuid) {
//                    
//                    oldCoupon.setCouponSetData(smCouponSet);
//                    oldUnifiedCouponsArray.append(oldCoupon);
//                    
//                    break;
//                }
//            }
//        }
        
        // TODO: DELETE - TEST
//        oldUnifiedCouponsArray = swiftApi().getAllOldCouponList().filter({ return $0.couponset_data?.couponset_type == "supermarket" })
        oldUnifiedCouponsArray = swiftApi().getSMOldCouponList()
        
        oldUnifiedCouponsArray.sort(by: {
            let date1 = $0.redeemed_date
            let date2 = $1.redeemed_date
            
            if ((date1 != nil) && (date2 != nil)) {
                return date1!.compare(date2!) == .orderedDescending
            } else {
                return false
            }
            
        })
        swiftApi().setOldUnifiedCouponList(oldUnifiedCouponsArray)
    }
    
    func updateUI() {
        let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-Regular", size: 14) ?? UIFont.systemFont(ofSize: 13), NSAttributedString.Key.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)]
        let attrBold = [NSAttributedString.Key.font : UIFont(name: "PeridotPE-SBold", size: 14) ?? UIFont.boldSystemFont(ofSize: 13), NSAttributedString.Key.foregroundColor: UIColor(red: 0.13, green: 0.13, blue: 0.13, alpha: 1.00)]
        
        // === SUM banner === //
        
        let sumRewards = totalCouponValue + totalCouponDiscount + unifiedCouponsDiscount + boxCouponValue
        
//        sumRewards = Float(round(100 * sumRewards) / 100)
        var sumRewardsString = "0"
        sumRewardsString = String(format: "%.2f", sumRewards).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        sumBadgeLabel.text = sumRewardsString + "€"
        
        sumBannerLabel.text = "Δες αναλυτικά το συνολικό όφελός σου έως τώρα από κουπόνια"
        
        // === DFY banner === //
        
        dfyBannerParentView.backgroundColor = .clear
        dfyBannerInnerParentView.backgroundColor = .white
//        dfyBannerInnerView.backgroundColor = UIColor(red: 0.46, green: 0.75, blue: 0.45, alpha: 0.05)
        dfyBannerInnerView.backgroundColor = .white
        dfyBannerInnerParentView.layer.cornerRadius = 16.0
        dfyBannerInnerView.layer.cornerRadius = 16.0
        // dfyBannerInnerView.layer.borderWidth = 1
        // dfyBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        dfyBannerParentView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        dfyBannerParentView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        dfyBannerParentView.layer.shadowOpacity = 1.0
        dfyBannerParentView.layer.shadowRadius = 2.0
        
        let totalCouponValueFixed = Float(round(100 * totalCouponValue) / 100)
        var totalCouponValueString = "0"
        totalCouponValueString = String(format: "%.2f", totalCouponValueFixed).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        dfyBadgeLabel.text = totalCouponValueString + "€"
        
        let dfyNormalText1 = "Μέχρι τώρα έχεις όφελος "
        let dfyBoldText  = totalCouponValueString + "€"
        let dfyNormalText2 = " από τα DEALS FOR YOU"

        let dfyAttributedString = NSMutableAttributedString(string:dfyNormalText1, attributes:attrRegular)
        let dfyBoldString = NSMutableAttributedString(string: dfyBoldText, attributes:attrBold)
        let dfyNormalString = NSMutableAttributedString(string:dfyNormalText2, attributes:attrRegular)

        dfyAttributedString.append(dfyBoldString)
        dfyAttributedString.append(dfyNormalString)
        dfyBannerLabel.attributedText = dfyAttributedString
        
        // === MARKET banner === //
        
        marketBannerParentView.backgroundColor = .clear
//        marketBannerInnerView.backgroundColor = UIColor(red: 1.00, green: 0.99, blue: 0.97, alpha: 1.00)
        marketBannerInnerView.backgroundColor = .white
        marketBannerInnerView.layer.cornerRadius = 16.0
        // marketBannerInnerView.layer.borderWidth = 1
        // marketBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        marketBannerParentView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        marketBannerParentView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        marketBannerParentView.layer.shadowOpacity = 1.0
        marketBannerParentView.layer.shadowRadius = 2.0
        
        let marketDiscount = Float(round(100 * unifiedCouponsDiscount) / 100)
        var marketDiscountString = "0"
        marketDiscountString  = String(format: "%.2f", marketDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
    
        self.marketBadgeLabel.text = marketDiscountString + "€"
        
        let marketNormalText1 = "Μέχρι τώρα έχεις όφελος "
        let marketBoldText  = marketDiscountString + "€"
        let marketNormalText2 = " από τα SUPERMARKET DEALS!"

        let marketAttributedString = NSMutableAttributedString(string:marketNormalText1, attributes:attrRegular)
        let marketBoldString = NSMutableAttributedString(string: marketBoldText, attributes:attrBold)
        let marketNormalString = NSMutableAttributedString(string:marketNormalText2, attributes:attrRegular)

        marketAttributedString.append(marketBoldString)
        marketAttributedString.append(marketNormalString)
        marketBannerLabel.attributedText = marketAttributedString
        
        // === GFY banner === //
        
        gfyBannerParentView.backgroundColor = .clear
//        gfyBannerInnerView.backgroundColor = UIColor(red: 0.96, green: 0.97, blue: 0.98, alpha: 1.00)
        gfyBannerInnerView.backgroundColor = .white
        gfyBannerInnerView.layer.cornerRadius = 16.0
        // gfyBannerInnerView.layer.borderWidth = 1
        // gfyBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        gfyBannerParentView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        gfyBannerParentView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        gfyBannerParentView.layer.shadowOpacity = 1.0
        gfyBannerParentView.layer.shadowRadius = 2.0
        
        var totalCouponDiscountString = "0"
        totalCouponDiscountString  = String(format: "%.2f", totalCouponDiscount).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        couponBadgeLabel.text = totalCouponDiscountString + "€"
        
        let gfyNormalText1 = "Μέχρι τώρα έχεις όφελος "
        let gfyBoldText  = totalCouponDiscountString + "€"
        let gfyNormalText2 = " από τα FREE COUPONS"
//        let gfyNormalText2 = " από τα GIFTS FOR YOU"

        let gfyAttributedString = NSMutableAttributedString(string:gfyNormalText1, attributes:attrRegular)
        let gfyBoldString = NSMutableAttributedString(string: gfyBoldText, attributes:attrBold)
        let gfyNormalString = NSMutableAttributedString(string:gfyNormalText2, attributes:attrRegular)

        gfyAttributedString.append(gfyBoldString)
        gfyAttributedString.append(gfyNormalString)
        gfyBannerLabel.attributedText = gfyAttributedString
        
        // === BOX banner === //
        
        boxBannerParentView.backgroundColor = .clear
        boxBannerInnerParentView.backgroundColor = .white
//        boxBannerInnerView.backgroundColor = UIColor(red: 0.46, green: 0.75, blue: 0.45, alpha: 0.05)
        boxBannerInnerView.backgroundColor = .white
        boxBannerInnerParentView.layer.cornerRadius = 16.0
        boxBannerInnerView.layer.cornerRadius = 16.0
        // boxBannerInnerView.layer.borderWidth = 1
        // boxBannerInnerView.layer.borderColor = UIColor(red: 0.90, green: 0.90, blue: 0.90, alpha: 1.00).cgColor
        
        boxBannerParentView.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.2).cgColor
        boxBannerParentView.layer.shadowOffset = CGSize(width: 0.0, height: 1.0)
        boxBannerParentView.layer.shadowOpacity = 1.0
        boxBannerParentView.layer.shadowRadius = 2.0
        
        let boxCouponValueFixed = Float(round(100 * boxCouponValue) / 100)
        var boxCouponValueString = "0"
        boxCouponValueString = String(format: "%.2f", boxCouponValueFixed).replacingOccurrences(of: ".", with: ",", options: .literal, range: nil)
        boxBadgeLabel.text = boxCouponValueString + "€"
        
        let boxNormalText1 = "Μέχρι τώρα έχεις όφελος "
        let boxBoldText  = boxCouponValueString + "€"
        let boxNormalText2 = " από το BOX"

        let boxAttributedString = NSMutableAttributedString(string:boxNormalText1, attributes:attrRegular)
        let boxBoldString = NSMutableAttributedString(string: boxBoldText, attributes:attrBold)
        let boxNormalString = NSMutableAttributedString(string:boxNormalText2, attributes:attrRegular)

        boxAttributedString.append(boxBoldString)
        boxAttributedString.append(boxNormalString)
        boxBannerLabel.attributedText = boxAttributedString
        
    }
    
    // MARK: - Actions
    @IBAction func dfyBannerButtonAction(_ sender: Any) {
//        var totalCouponValue = swiftApi().getDealsCouponsSum()
//        if (totalCouponValue > 0.0) {
            print("DFY coupon banner pressed!")
           // analysis_pressed event
           let dealsAnalysis = swiftApi.WarplyDealsAnalysisEventModel()
           dealsAnalysis._isPressed = true
           SwiftEventBus.post("analysis_pressed", sender: dealsAnalysis)

//            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
//            firebaseEvent._eventName = "did_tap_gifts_for_you_badge"
//            firebaseEvent.setParameter = ("screen", "Loyalty History")
//            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_deals_for_you_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty History")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "DealsBanner"))
//        }
    }
    
    @IBAction func gfyBannerButtonAction(_ sender: Any) {
//        if (loyaltyBadge._couponCount > 0) {
//            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
//            firebaseEvent._eventName = "did_tap_deals_for_you_badge"
//            firebaseEvent.setParameter = ("screen", "Loyalty History")
//            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_gifts_for_you_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty History")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "LoyaltyBanner"))

            let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
            let vc = storyboard.instantiateViewController(withIdentifier: "LoyaltyAnalysisViewController") as! SwiftWarplyFramework.LoyaltyAnalysisViewController
            self.navigationController?.pushViewController(vc, animated: true)
//        }
    }
    
    @IBAction func marketBannerButtonAction(_ sender: Any) {
//        if (self.unifiedCouponsDiscount > 0.0) {
            let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
            firebaseEvent._eventName = "did_tap_market_badge"
            firebaseEvent.setParameter = ("screen", "Loyalty History")
            SwiftEventBus.post("firebase", sender: firebaseEvent)
            
            swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "MarketBanner"))

            let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
            let vc = storyboard.instantiateViewController(withIdentifier: "MarketLoyaltyAnalysisViewController") as! SwiftWarplyFramework.MarketLoyaltyAnalysisViewController
            self.navigationController?.pushViewController(vc, animated: true)
//        }
    }
    
    @IBAction func boxBannerButtonAction(_ sender: Any) {
        print("BOX coupon banner pressed!")
        
        // box_analysis_pressed event
        let dealsAnalysis = swiftApi.WarplyBoxAnalysisEventModel()
        dealsAnalysis._isPressed = true
        SwiftEventBus.post("box_analysis_pressed", sender: dealsAnalysis)
        //        SwiftEventBus.post("box_analysis_pressed")
        
        let firebaseEvent = swiftApi.LoyaltySDKFirebaseEventModel()
        firebaseEvent._eventName = "did_tap_box_badge"
        firebaseEvent.setParameter = ("screen", "Loyalty History")
        SwiftEventBus.post("firebase", sender: firebaseEvent)
        
        swiftApi().logTrackersEvent("click", ("LoyaltyHistoryScreen:" + "BoxBanner"))
    }
}