//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class SpeakerErrorNavigationController: UINavigationController {
    // This exists to allow stringless instantiation from storyboard - do not remove
}

// MARK: -

class SpeakerErrorViewController: ModalViewController {
    private weak var timer: Timer?
}

// MARK: - Overridden Methods
extension SpeakerErrorViewController {
    override func viewDidLoad() {
        hasCloseButton = false
        title = NSLocalizedString("NEW SPEAKERS", comment: "Speaker error screen title")
        
        super.viewDidLoad()
        
        timer = Timer.scheduledTimer(timeInterval: 10,
                                                   target: self,
                                                   selector: #selector(timedOut(_:)),
                                                   userInfo: nil,
                                                   repeats: false)
    }
}

// MARK: - UI Actions
extension SpeakerErrorViewController {
    @IBAction private func notNowButtonTapped(_ sender: AnyObject?) {
        timer?.invalidate()
        timer = nil
        appSpinnerPresenter()?.transitionToRunning()
        if let presenter = presentingViewController {
            presenter.dismiss(animated: true, completion: nil)
        }
    }
}

// MARK: - Private Methods
extension SpeakerErrorViewController {
    @objc private func timedOut(_ sender: Timer) {
        timer = nil
        if let presenter = presentingViewController {
            presenter.dismiss(animated: true, completion: {
                appSpinnerPresenter()?.searchForSpeakers(failureIsError: false)
            })
        }
    }
}
