//******************************************************************************
//
// MIDITrail / MIDITrailAppDelegate
//
// MIDITrail アプリケーションデリゲート
//
// Copyright (C) 2012-2019 WADA Masashi. All Rights Reserved.
//
//******************************************************************************

#import "MIDITrailAppDelegate.h"


@implementation MIDITrailAppDelegate

//******************************************************************************
// アプリケーション起動処理終了
//******************************************************************************
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id>*)launchOptions
{
	int result = 0;
	
	NSLog(@"MIDITrailAppDelegate::didFinishLaunching");
	
	//アプリケーション生成
	m_pApp = [[MIDITrailApp alloc] init];
	
	//アプリケーション初期化
	result = [m_pApp initialize:launchOptions];
	if (result != 0) goto EXIT;
	
	//アプリケーション実行
	result = [m_pApp run];
	if (result != 0) goto EXIT;
	
EXIT:;
	if (result != 0) YN_SHOW_ERR();
	return YES;
}

//******************************************************************************
// 非アクティブ状態遷移
//******************************************************************************
- (void)applicationWillResignActive:(UIApplication*)application
{
	// Xcode自動生成コメント
	// 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.
	
	NSLog(@"MIDITrailAppDelegate::applicationWillResignActive");
	
	//非アクティブ状態遷移
	//  ホーム画面に戻るときも呼び出されるため電話着信に限らない
	//  このあとにバックグラウンド状態遷移 applicationDidEnterBackground が呼び出される
	[m_pApp onApplicationWillResignActive];
	
	return;
}

//******************************************************************************
// バックグラウンド状態遷移
//******************************************************************************
- (void)applicationDidEnterBackground:(UIApplication*)application
{
	// Xcode自動生成コメント
	// 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.
	
	NSLog(@"MIDITrailAppDelegate::applicationDidEnterBackground");
	
	//バックグラウンド状態遷移
	[m_pApp onApplicationDidEnterBackground];
	
	return;
}

//******************************************************************************
// フォアグラウンド状態遷移
//******************************************************************************
- (void)applicationWillEnterForeground:(UIApplication*)application
{
	// Xcode自動生成コメント
	// 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.

	NSLog(@"MIDITrailAppDelegate::applicationWillEnterForeground");
	
	//フォアグラウンド状態遷移
	//  バックグラウンドからフォアグラウンドに戻るときに呼び出される
	//  この後にアクティブ状態遷移 applicationDidBecomeActive が呼び出される
	
	return;
}

//******************************************************************************
// アクティブ状態遷移
//******************************************************************************
- (void)applicationDidBecomeActive:(UIApplication*)application
{
	// Xcode自動生成コメント
	// 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.

	NSLog(@"MIDITrailAppDelegate::applicationDidBecomeActive");
	
	//アクティブ状態遷移
	[m_pApp onApplicationDidBecomeActive];
	
	return;
}

//******************************************************************************
// アプリケーション終了
//******************************************************************************
- (void)applicationWillTerminate:(UIApplication*)application
{
	// Xcode自動生成コメント
	// Called when the application is about to terminate. Save data if 
	// appropriate. See also applicationDidEnterBackground:.
	
	int result = 0;
	
	NSLog(@"MIDITrailAppDelegate::applicationWillTerminate");
	
	//アプリケーション停止
	result = [m_pApp terminate];
	if (result != 0) goto EXIT;
	
	[m_pApp release];
	m_pApp = nil;
	
EXIT:;
	if (result != 0) YN_SHOW_ERR();
	return;
}

//******************************************************************************
// 破棄
//******************************************************************************
- (void)dealloc
{
	NSLog(@"MIDITrailAppDelegate::dealloc");
	
	[super dealloc];
	
	return;
}

//******************************************************************************
// 指定URLオープン：iOS9以降
//******************************************************************************
- (BOOL)application:(UIApplication*)application
			openURL:(NSURL*)url
			options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
	int result = 0;
	
	//指定URLオープンが呼び出されるタイミング
	//  (a) メールの添付ファイルをMIDITrailで開く
	//  (b) ファイルAppでMIDITrailのDocumentsフォルダ配下のファイルを開く
	//  (c) ファイルAppで他アプリのDocumentsフォルダ配下のファイルをMIDITrailで開く
	//  (d) ファイルAppでDownladフォルダ配下のファイルををMIDITrailで開く
	//  (e) SafariでダウンロードしたファイルをMIDITrailで開く
	
	result = [m_pApp openURL:url];
	if (result != 0) goto EXIT;
	
EXIT:;
	if (result != 0) YN_SHOW_ERR();
	return YES;
}

@end

