//
//  GDPRHelper.swift
//  sdk-demo-ios
//
//  Created by Created by Smaato Inc on 04/12/2019.
//  Copyright © 2019 Smaato Inc. All rights reserved.
//

import UIKit
import ConsentViewController

protocol GDPRHelperDelegate : class {
    func needsUpdateGDPRInformation()
}

class GDPRHelper: NSObject {
    private let consentURL = "https://demofiles.smaato.com/cmp/index.html"
    
    var parentVC : UIViewController!
    
    lazy var consentViewController: GDPRConsentViewController = { return GDPRConsentViewController(accountId: 1081, propertyId: 4992, propertyName:try! GDPRPropertyName("demo.smaato.ios"), PMId: "5e2eff2c0a86b80e86d52233", campaignEnv: .Public, consentDelegate: self)}()
    
    private lazy var gdprAPI: CMPConsentToolAPI = {
        let api = CMPConsentToolAPI()
        api.subjectToGDPR = .unknown
        api.cmpPresent = true
        return api
    }()
    weak var delegate : GDPRHelperDelegate?
    
    func shouldShowConsentDialogMessage() -> Bool {
        let subject = gdprAPI.subjectToGDPR;
        if ((subject == .yes || subject == .unknown) && (gdprAPI.consentString ?? "").isEmpty) {
            return true;
        }
        
        return false;
    }
    
    func gdprInfo() -> String {
        var str = String()

        str += "SubjectToGDPR: \(gdprAPI.subjectToGDPR.rawValue)\n\n"
        str += "ConsentString: \(String(describing: gdprAPI.consentString))\n\n"
        str += "VendorConsentBitString: \(String(describing: gdprAPI.parsedVendorConsents))\n\n"
        str += "PurposeConsentBitString: \(String(describing: gdprAPI.parsedPurposeConsents))\n\n"
        str += "CmpPresent: \(gdprAPI.cmpPresent)\n\n"

        let vendorId: Int32 = 82
        let vendorConsent = gdprAPI.isVendorConsentGiven(for: vendorId)
        let consentString = vendorConsent ? "YES" : "NO"
        str += "Consent for vendor ID: \(vendorId) = \(consentString)\n\n"

        for purposeId: Int32 in 1...5 {
            let purposeConsent = gdprAPI.isPurposeConsentGiven(for: purposeId)
            let purposeString = purposeConsent ? "YES" : "NO"
            str += "Consent for purpose ID: \(purposeId) = \(purposeString)\n"
        }

        return str
    }
    
    func showConsentDialog(onViewController parentVC: UIViewController) {
        self.parentVC = parentVC
        consentViewController.loadPrivacyManager()
    }
    
    func showConsentMessage(onViewController parentVC: UIViewController) {
        self.parentVC = parentVC
        consentViewController.loadMessage()
    }
    
    func deleteConsentInformation() {
        consentViewController.clearAllData()
        delegate?.needsUpdateGDPRInformation()
    }
}

extension GDPRHelper: GDPRConsentDelegate {
    func consentUIWillShow() {
        consentViewController.modalPresentationStyle = .fullScreen
        self.parentVC.present(consentViewController, animated: true, completion: nil)
    }
    
    func consentUIDidDisappear() {
        delegate?.needsUpdateGDPRInformation()
    }
    
    func onConsentReady(gdprUUID: GDPRUUID, userConsent: GDPRUserConsent) {
        delegate?.needsUpdateGDPRInformation()
    }
    
    func onErrorOccurred(error: GDPRConsentViewControllerError) {
        print("Error: \(error.debugDescription)")
    }    
    
}
