//=============================================================================================================================
//
// Copyright (c) 2015-2022 VisionStar Information Technology (Shanghai) Co., Ltd. All Rights Reserved.
// EasyAR is the registered trademark or trademark of VisionStar Information Technology (Shanghai) Co., Ltd in China
// and other countries for the augmented reality technology developed by VisionStar Information Technology (Shanghai) Co., Ltd.
//
//=============================================================================================================================

import GLKit
import EasyARSwift

//
// Steps to create the key for this sample:
//  1. login www.easyar.com
//  2. create app with
//      Name: HelloAR
//      Bundle ID: cn.easyar.samples.helloar
//  3. find the created item in the list and show key
//  4. set key string bellow
//
let key = "===PLEASE ENTER YOUR KEY HERE==="

internal class ViewController: GLKViewController {
    override func viewDidLoad() {
        if !Engine.initialize(key) {
            print("Initialization Failed.")
            ViewController.displayToastWithMessage(Engine.errorMessage())
            return
        }
        if (!CameraDevice.isAvailable()) {
            ViewController.displayToastWithMessage("CameraDevice not available.")
            return
        }
        if (!ImageTracker.isAvailable()) {
            ViewController.displayToastWithMessage("ImageTracker not available.")
            return
        }
        let v = view as! OpenGLView
        v.initialize()
        v.setOrientation(interfaceOrientation)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let v = view as! OpenGLView
        v.start()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let v = view as! OpenGLView
        v.stop()
    }
    
    override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
        super.willRotate(to: toInterfaceOrientation, duration: duration)
        let v = view as! OpenGLView
        v.setOrientation(toInterfaceOrientation)
    }

    static func displayToastWithMessage(_ toastMessage: String) -> Void {
        OperationQueue.main.addOperation {
            let keyWindow = UIApplication.shared.keyWindow!
            let toastView = UILabel()
            toastView.text = toastMessage
            toastView.textAlignment = NSTextAlignment.center
            toastView.textColor = UIColor.white
            toastView.lineBreakMode = NSLineBreakMode.byWordWrapping
            toastView.numberOfLines = 0
            toastView.frame = CGRect(x: 0, y: 0, width: keyWindow.frame.size.width / 2, height: 200)
            toastView.layer.cornerRadius = 10
            toastView.layer.masksToBounds = true
            toastView.center = keyWindow.center

            keyWindow.addSubview(toastView)
            UIView.animate(withDuration: 0.5, delay: 3, options: UIView.AnimationOptions.curveLinear, animations: { toastView.alpha = 0 }) { (finished) in toastView.removeFromSuperview() }
        }
    }
}
