GiftsViewController.swift 7.7 KB
//
//  GiftsViewController.swift
//  WarplySDKFrameworkIOS
//
//  Created by Βασιλης Σκουρας on 5/5/22.
//

import Foundation
import UIKit
import SwiftEventBus

@objc public class GiftsViewController: UIViewController {
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var tableView: UITableView!
    
    public var campaigns:Array<swiftApi.CampaignItemModel> = swiftApi().mergeGFYCCMSLoyaltyCampaigns(campaigns: swiftApi().getCCMSLoyaltyCampaigns())
    public var seasonalList:Array<swiftApi.LoyaltyGiftsForYouPackage> = swiftApi().getSeasonalList()
    
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hidesBottomBarWhenPushed = true

        if (seasonalList.count > 0) {
            for seasonalItem in seasonalList {
                let newCampaign = swiftApi.CampaignItemModel()
                newCampaign._title = seasonalItem._title
                newCampaign._logo_url = seasonalItem._imageUrl
                newCampaign._loyaltyPackageId = seasonalItem._loyaltyPackageId
                campaigns.append(newCampaign)
            }
        }
        
        setBackButton()
        setNavigationTitle("GIFTS for YOU")
        
        tableView.delegate = self
        tableView.dataSource = self

        SwiftEventBus.onMainThread(self, name: "ccms_retrieved") { result in
            print("===== SwiftEventBus ccms_retrieved =====")
            self.campaigns = swiftApi().mergeGFYCCMSLoyaltyCampaigns(campaigns: swiftApi().getCCMSLoyaltyCampaigns())
            self.tableView.reloadData()
        }
        
        tableView.clipsToBounds = true
        tableView.layer.cornerRadius = 30
        tableView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
        tableView.contentInset.top = 50
        
        // Add Top left corner radius
        mainView.clipsToBounds = true
        mainView.layer.cornerRadius = 30
        mainView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
        mainView.backgroundColor = UIColor(red: 0.22, green: 0.32, blue: 0.40, alpha: 1.00)
        
    }
    
}

// MARK: - TableView
extension GiftsViewController: UITableViewDelegate, UITableViewDataSource{
    
    public func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.campaigns.count
    }
    
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 140.0 + 10.0
//        return UITableViewAutomaticDimension
    }
    
    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "InboxTableViewCellId", for: indexPath) as! InboxTableViewCell
        
        cell.configureCell(campaign: campaigns[indexPath.row])
        
        return cell
    }
    
    public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if (campaigns[indexPath.row]._type != nil && campaigns[indexPath.row]._type == "telco") {
            let ccmsCampaigns = swiftApi().getCCMSLoyaltyCampaigns()
            let loyaltyCampaignIdExists = ccmsCampaigns.contains { $0._loyaltyCampaignId == campaigns[indexPath.row].session_uuid }

            if (loyaltyCampaignIdExists == true) {
                for item in ccmsCampaigns {
                    if (item._loyaltyCampaignId == campaigns[indexPath.row].session_uuid) {
                        let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
                        let vc = storyboard.instantiateViewController(withIdentifier: "TelcoViewController") as! SwiftWarplyFramework.TelcoViewController
                        vc.ccms = item
                        vc.campaign = campaigns[indexPath.row]
                        self.navigationController?.pushViewController(vc, animated: true)
                        break;
                    }
                }
            }
            
        } else if (campaigns[indexPath.row]._campaign_type == "coupon") {
            let couponsetUuid = campaigns[indexPath.row]._couponset
            
            if (couponsetUuid != "") {
                let couponsetsList = swiftApi().getCouponSetList()
                
                for item in couponsetsList {
                    if (item.uuid == couponsetUuid) {
                       let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
                       let vc = storyboard.instantiateViewController(withIdentifier: "CouponViewController") as! SwiftWarplyFramework.CouponViewController
                       vc.couponset = item
                       self.navigationController?.pushViewController(vc, animated: true)

                        break;
                    }
                }
            }
                
        } else {
            if (campaigns[indexPath.row].session_uuid != nil && campaigns[indexPath.row].session_uuid != "") {
                let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
                let vc = storyboard.instantiateViewController(withIdentifier: "CampaignViewController") as! SwiftWarplyFramework.CampaignViewController
                var url = ""
                let isCcmsOffer = (campaigns[indexPath.row].ccms_offer != nil) && (campaigns[indexPath.row].ccms_offer != "") && (campaigns[indexPath.row].ccms_offer == "true")
                
                if (isCcmsOffer) {
                    let ccmsCampaigns = swiftApi().getCCMSLoyaltyCampaigns()

                    let loyaltyCampaignIdExists = ccmsCampaigns.contains { $0._loyaltyCampaignId == campaigns[indexPath.row].session_uuid }

                    if (loyaltyCampaignIdExists == true) {
                        var ccmsCampaignFound:swiftApi.LoyaltyContextualOfferModel = swiftApi.LoyaltyContextualOfferModel()
                        for item in ccmsCampaigns {
                            if (item._loyaltyCampaignId == campaigns[indexPath.row].session_uuid) {
                                ccmsCampaignFound = item
                            }
                        }
                        url = swiftApi().constructCcmsUrl(campaigns[indexPath.row], ccmsCampaignFound)

                    } else {
                        url = swiftApi().constructCampaignUrl(campaigns[indexPath.row])

                    }

                    let encoder = JSONEncoder()
                    encoder.outputFormatting = .prettyPrinted
                    let data = try! encoder.encode(campaigns[indexPath.row])

                    print("CCMS_CLICK " + String(data: data, encoding: .utf8)!)

                } else {
                    url = swiftApi().constructCampaignUrl(campaigns[indexPath.row])

                    let encoder = JSONEncoder()
                    encoder.outputFormatting = .prettyPrinted
                    let data = try! encoder.encode(campaigns[indexPath.row])

                    print("GFY_CLICK " + String(data: data, encoding: .utf8)!)
                }
                print("CampaignUrl url onclick: " + url)
                vc.campaignUrl = url
                self.navigationController?.pushViewController(vc, animated: true)

            } else {
                let seasonalEvent = swiftApi.LoyaltyGiftsForYouOfferClickEvent()
                seasonalEvent._title = campaigns[indexPath.row]._title ?? ""
                seasonalEvent._imageUrl = campaigns[indexPath.row]._logo_url ?? ""
                seasonalEvent._loyaltyPackageId = campaigns[indexPath.row]._loyaltyPackageId ?? ""
                SwiftEventBus.post("seasonal", sender: seasonalEvent)
            }
        }
    }
    
}