//
//  AppDelegate.m
//  PanoPainter
//
//  Created by Omar Mohamed Ali Mudhir on 07/05/17.
//  Copyright © 2017 Omar Mohamed Ali Mudhir. All rights reserved.
//

#import "AppDelegate.h"
#import "GameViewController.h"
#include "app.h"
#include <WTSonarPenDriver.h>

@import AppCenter;
@import AppCenterAnalytics;
@import AppCenterCrashes;

@interface AppDelegate () {
    GameViewController* view;
    WTSonarPenDriver* sonarpen_driver;
    bool sonarpen_started;
}

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    App::I->open_document([url fileSystemRepresentation]);
    return true;
}

- (void)hockeyapp_crash
{
    [MSCrashes generateTestCrash];
}

- (bool)sonarpen_present
{
    return sonarpen_started && [sonarpen_driver isPenAttached];
}

- (float)sonarpen_pressure
{
    return [sonarpen_driver isPenAttached] ? [sonarpen_driver pressure] : 1.f;
}

- (void)penStatusChanged
{
    switch (sonarpen_driver.state) {
        case WTSonarPenDriverStateInit:
            NSLog(@"Status: Initializing"); break;
        case WTSonarPenDriverStateRunning:
            NSLog(@"Status: Running");
            App::I->message_box("SonarPen", "Your SonarPen is now ready. Have fun!");
            break;
        case WTSonarPenDriverStateCalibrating:
            NSLog(@"Status: Calibrating"); break;
        case WTSonarPenDriverStateNotSonarPen:
            NSLog(@"Status: Not a Sonar Pen");
            App::I->message_box("SonarPen Error", "Couldn't recognize the device attatched to the jack plug as a SonarPen. Please remove it and plug it back in.");
            break;
        case WTSonarPenDriverStateCalibratingMax:
            NSLog(@"Status: Calibrating Max Value"); break;
        case WTSonarPenDriverStateWaitingForPermission:
            NSLog(@"Status: Wait for Permission"); break;
        case WTSonarPenDriverStateNoMicrophonePermission:
            NSLog(@"Status: No Microphone Permission");
            App::I->message_box("SonarPen Error", "SonarPen could not be started because you didn't allow the microphone permission. Please go to Settings -> PanoPainter and enable Microphone.");
            break;
    }
}

- (void)sonarpen_start {
    if (sonarpen_started)
        return;
    sonarpen_driver = [[WTSonarPenDriver alloc] initWithApplication:[UIApplication sharedApplication]];
    [sonarpen_driver start];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(penStatusChanged) name:WTSonarPenDriverStateChangedNotification object:nil];
    sonarpen_started = true;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    sonarpen_started = false;
    [MSAppCenter start:@"c2dccca1-9629-4ecc-a8fd-5d58af2738cf" withServices:@[
      [MSAnalytics class],
      [MSCrashes class]
    ]];

    view = (GameViewController*)self.window.rootViewController;
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    App::I->redraw = true;
    [view reset_touch];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    App::I->redraw = true;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    App::I->redraw = true;
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    App::I->redraw = true;
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    App::I->redraw = true;
}

@end
