//=============================================================================================================================
//
// 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 "ViewController.h"

#import "OpenGLView.h"

#import <easyar/camera.oc.h>
#import <easyar/engine.oc.h>
#import <easyar/imagetracker.oc.h>

@implementation ViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    if (![easyar_Engine initialize:key]) {
        NSLog(@"Initialization Failed.");
        [ViewController displayToastWithMessage:[easyar_Engine errorMessage]];
        return;
    }
    if (![easyar_CameraDevice isAvailable]) {
        [ViewController displayToastWithMessage:@"CameraDevice not available."];
        return;
    }
    if (![easyar_ImageTracker isAvailable]) {
        [ViewController displayToastWithMessage:@"ImageTracker not available."];
        return;
    }
    OpenGLView * view = (OpenGLView *)(self.view);
    [view initialize];
    [view setOrientation:self.interfaceOrientation];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    OpenGLView * view = (OpenGLView *)(self.view);
    [view start];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    OpenGLView * view = (OpenGLView *)(self.view);
    [view stop];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    OpenGLView * view = (OpenGLView *)(self.view);
    [view setOrientation:toInterfaceOrientation];
}

+ (void)displayToastWithMessage:(NSString *)toastMessage
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        UIWindow * keyWindow = [[UIApplication sharedApplication] keyWindow];
        UILabel * toastView = [[UILabel alloc] init];
        toastView.text = toastMessage;
        toastView.textAlignment = NSTextAlignmentCenter;
        toastView.textColor = [UIColor whiteColor];
        toastView.lineBreakMode = NSLineBreakByWordWrapping;
        toastView.numberOfLines = 0;
        toastView.frame = CGRectMake(0.0, 0.0, keyWindow.frame.size.width/2.0, 200.0);
        toastView.layer.cornerRadius = 10;
        toastView.layer.masksToBounds = YES;
        toastView.center = keyWindow.center;
        
        [keyWindow addSubview:toastView];
        
        [UIView animateWithDuration: 0.5f
                              delay: 3.0f
                            options: UIViewAnimationOptionCurveLinear
                         animations: ^{
                             toastView.alpha = 0.0f;
                         }
                         completion: ^(BOOL finished) {
                             [toastView removeFromSuperview];
                         }
         ];
    }];
}

@end
