CampaignViewController.swift 5.68 KB
//
//  CampaignViewController.swift
//  SwiftWarplyFramework
//
//  Created by Βασιλης Σκουρας on 11/5/22.
//

import Foundation
import UIKit
import WebKit
import SwiftEventBus

@objc public class CampaignViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHandler {
    @IBOutlet weak var webview: WKWebView!
    
    public var campaignUrl: String = ""
    
    public override func viewDidLoad() {
        super.viewDidLoad()

        var campaignUrl = campaignUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        print("Webview url: " + (campaignUrl ?? ""))
        
        self.hidesBottomBarWhenPushed = true

        setBackButton()
        
        webview.navigationDelegate = self
        if let url = URL(string: campaignUrl ?? "") {
            webview.load(URLRequest(url: url))
            webview.allowsBackForwardNavigationGestures = true
        }

        webview.configuration.userContentController.add(self, name: "Cosmote")
    }

    public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
        if let event = message.body as? String {
            let eventArray = event.split(separator: ":")
            
            if (eventArray[0] == "event") {
                if (eventArray[1] == "closeArtwork") {

                    swiftApi().getProfileAsync(getProfileCallback)

                    func getProfileCallback (_ profileData: swiftApi.ProfileModel?) -> Void {
                        if (profileData != nil) {
                            DispatchQueue.main.async {
                                swiftApi().getCampaignsAsyncNew(language: "en", filters: [String : Any](), getCampaignsCallback)

                                func getCampaignsCallback (_ campaignsData: Array<swiftApi.CampaignItemModel>?) -> Void {
                                    if (campaignsData != nil) {
                                        DispatchQueue.main.async {
                                        }
                                    } else {
                                    }
                                }
                                
                            }
                        } else {
                        }
                    }

                    self.navigationController?.popViewController(animated: true)
                    
                } else if (eventArray[1] == "addUserTag") {
                    let questionnaireEvent = swiftApi.QuestionnaireEventModel()
                    questionnaireEvent._name = String(eventArray[1])
                    if (eventArray.count > 2) {
                        questionnaireEvent._parameter = String(eventArray[2])
                        swiftApi().setUserTag(String(eventArray[2]))
                    } else {
                        questionnaireEvent._parameter = ""
                        swiftApi().setUserTag("")
                    }
                    SwiftEventBus.post("questionnaire", sender: questionnaireEvent)
                    
                } else if (eventArray[1] == "couponRetrieved") {
                    let couponEvent = swiftApi.CouponEventModel()
                   SwiftEventBus.post("coupon_retrieved", sender: couponEvent)

                    swiftApi().getCouponsAsync(getCouponsCallback)
                    
                    func getCouponsCallback (_ couponsData: Array<swiftApi.CouponItemModel>?) -> Void {
                        if (couponsData != nil) {
                            
                            DispatchQueue.main.async {
                                print("========= getCouponsRequest SUCCESSSS CampaignVC =========")
                                
                                print("========= getCouponsRequest count CampaignVC =========")
                                print(swiftApi().getCouponList().count)
                            }
                        } else {
                            print("========= getCouponsRequest ERROR CampaignVC =========")
                        }
                    }
                    
                } else if (eventArray[1] == "couponShared") {
                    
                }
                // else if (eventArray[2] == "serviceDisabled") {
                //     let pacingService = swiftApi.WarplyPacingEventModel()
                //     pacingService._isEnabled = false
                //    SwiftEventBus.post("pacing", sender: pacingService)
                    
                // } else if (eventArray[2] == "serviceEnabled") {
                //     let pacingService = swiftApi.WarplyPacingEventModel()
                //     pacingService._isEnabled = true
                //    SwiftEventBus.post("pacing", sender: pacingService)
                    
                // } 
                else if (eventArray[1] == "steps" && eventArray[2] == "shortcutEnabled") {
                    let pacingEvent = swiftApi.WarplyPacingEventModel()
                    pacingEvent._isVisible = true
                   SwiftEventBus.post("pacing", sender: pacingEvent)
                    
                } else if (eventArray[1] == "steps" && eventArray[2] == "shortcutDisabled") {
                    let pacingEvent = swiftApi.WarplyPacingEventModel()
                    pacingEvent._isVisible = false
                   SwiftEventBus.post("pacing", sender: pacingEvent)
                    
                } else if (eventArray[1] == "request" || eventArray[1] == "response") {
                    print("**************** WARPLY Webview Log START *****************")
                    print(event)
                    print("**************** WARPLY Webview Log END *****************")
                }
            }
        }
    }

}