//
//  GDPRSettingsViewController.swift
//  sdk-demo-ios
//
//  Created by Smaato Inc on 25.06.18.
//  Copyright © 2018 Smaato Inc. All rights reserved.
//

import UIKit

class GDPRSettingsViewController: UIViewController {

    @IBOutlet private var removeConsentInformationButton: UIButton!
    @IBOutlet weak var infoLabel: UILabel!
    private var gdprHelper: GDPRHelper?
    
    private var tcfVersion = 1 {
        didSet {
            gdprHelper = tcfVersion == 1 ? GDPRHelperV1(withParentViewController: self) : GDPRHelperV2(withParentViewController: self)
            gdprHelper?.delegate = self
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = OtherContent.GDPR.identifier
        
        self.tcfVersion = 1

        updateInfo()
    }

    //MARK: Actions

    @IBAction private func onShowConsentDialog(_: Any?) {
        gdprHelper?.showConsentDialog()
    }
    
    @IBAction func onDeleteConsentInformation(_ sender: Any) {
        gdprHelper?.deleteConsentInformation()
    }
    
    @IBAction func didChangeTCFVersion(_ sender: Any) {
        guard let segmentControl = sender as? UISegmentedControl else { return }
        self.tcfVersion = segmentControl.selectedSegmentIndex + 1
        updateInfo()
    }
    
    private func updateInfo() {
        infoLabel.text = gdprHelper?.infoString ?? "Undefined"
    }
}

extension GDPRSettingsViewController: GDPRHelperDelegate {

    func needsUpdateGDPRInformation() {
        updateInfo()
        self.dismiss(animated: true, completion: nil)
    }

}
