//  X-BASIC'
//
//  Created by AIG-Soft on 2012/8/13
//  Copyright 2012-2015 AIG-Soft. All rights reserved.
//

// 未定義時はdeviceOrientation;iOS6では必須
//#define UIORIENTATION	1	// 定義時はUIOrientation

#import <UIKit/UIKit.h>
#import	"main.h"
#import "AppDelegate.h"
#import "NSString+substringWithRange2.h"

#import "Music8ch.h"
//#import <AVFoundation/AVFoundation.h>
//#import "FileControl2.h"

// グローバル(的)変数
BOOL	fnonActive=NO;	// nonActiveになったかどうか
BOOL	ffirst;			// 初回起動かどうか
BOOL	fRotaionLock;	// YES=回転禁止

// プログラム名
NSString *programVersion,*programName;

int main(int argc, char *argv[])
{
#if 1
	// 32bit	64bit
	NSLog(@"%zdbit",sizeof(long)*8);
#endif

#if 0
//	nslog2(@"file=%@",getResourceOrSavedataPath(@"futta-dream.wav"));
	NSData *data=[NSData dataWithContentsOfFile:getResourceOrSavedataPath(@"futta-dream.wav")];
	nslog2(@"data=%p",data);
	NSError *error;
	AVAudioPlayer *audioPlay=[[AVAudioPlayer alloc]initWithData:data error:&error];
	nslog2(@"audioPlay=%@",audioPlay);
	nslog2(@"error=%@",error);
	[audioPlay play];
	time_t ts,t;
	time(&ts);
	t=ts;
	while (t-ts<=2) {
		time(&t);
	}
	audioPlay=nil; // これで消音するかどうか
#endif
	
#if 0
	Music8ch *music8ch=[[Music8ch alloc]init];
	music8ch.tempo=180;
	[music8ch track:0 mmlSource:
		@"@'track1 Super Mario Bros. Theme Song'"
		@"O3D8  D8  R8 D8  R8 D8  D4  >G4 R4"
	 ];
	[music8ch track:1 mmlSource:
		@"@'track2 Super Mario Bros. Theme Song'"
		@"O4F+8 F+8 R8 F+8 R8 F+8 F+4 G4 R4"
	 ];
	[music8ch track:2 mmlSource:
		@"@'track3 Super Mario Bros. Theme Song'"
		@"O5E8  E8  R8 E8  R8 C8  E4  G4 R4"
	 ];
	[music8ch assign:0 trk:0];
	[music8ch assign:1 trk:1];
	[music8ch assign:2 trk:2];
	[music8ch play:MUSIC_bCH0|MUSIC_bCH1|MUSIC_bCH2];
#endif
	
#if 1
	Music8ch *music8ch=[[Music8ch alloc]init];
	music8ch.tempo=66;
//	[music8ch track:1 mmlSource:@"Q7 @1 V10   O4 T66 L16 {FE FE FE FE}8 &ED32E32"];
#if 1
	[music8ch track:0 mmlSource:@"Q7 @1  V9   O4 T66 L16 B-4&B-B-AGF8G8DE-F8 : <R8F8&FE-DC>B-8 <D8> AB-<C8 >F<FD>B-<E-8G8&GE-C>A&A8<F8 : D4E4F8>B-4A8"];
//	[music8ch track:3 mmlSource:@"Q8 @2  V6   O5 T66 L16 B-4&B-B-AGF8G8DE-F8 : <R8F8&FE-DC>B-8 <D8> AB-<C8 >F<FD>B-<E-8G8&GE-C>A&A8<F8 : D4E4F8>B-4A8"];
	[music8ch track:1 mmlSource:@"Q7 @1 V10   O4 T66 L16 R1 : >B-FB-<DC8E-8&E-8DE {FE FE FE FE}8 &ED32E32 F4G8B-8E-8G8<C8C8& : C>FB-<D>G8B-8A8DE-FGE-F"];
//	[music8ch track:4 mmlSource:@"Q8 @2  V7   O5 T66 L16 R1 : >B-FB-<DC8E-8&E-8DE {FE FE FE FE}8 &ED32E32 F4G8B-8E-8G8<C8C8& : C>FB-<D>G8B-8A8DE-FGE-F"];
	[music8ch track:2 mmlSource:@"Q7 @1  V4   O3 T66 L8  B-<DC>B-AGFE- : D4F4G4<C4 D4&L16DD>B-G<C8E-8&E-C>AF : B-4&B-B-AGF8G8DE-F8"];
//	[music8ch track:5 mmlSource:@"Q8 @2  V2   O4 T66 L8  B-<DC>B-AGFE- : D4F4G4<C4 D4&L16DD>B-G<C8E-8&E-C>AF : B-4&B-B-AGF8G8DE-F8"];
#endif
	for (int i=0;i<=5;i++) {
		[music8ch assign:i trk:i];
	}
	[music8ch play:MUSIC_bCH0|MUSIC_bCH1|MUSIC_bCH2|MUSIC_bCH3|MUSIC_bCH4|MUSIC_bCH5];
#endif
	
#if 0
	NewAudioPlay *nav=[[NewAudioPlay alloc]init];
//	[nav setFile:@"futta-dream.wav"];
	[nav setFile:@"15_FORGAN1_A1.wav"];
	nav.volume=0.25;
//	[nav setReverb:50 preset:AVAudioUnitReverbPresetLargeHall2];
//	[nav scheduleFile];
//	[nav play];
//	while (nav.playing);
	nslog2(@"play1");
	[nav readFileToBuffer];
	[nav setFormatWav:44100];
	[nav connectAndStart];
	[nav scheduleBuffer:AVAudioPlayerNodeBufferInterruptsAtLoop];
	[nav play];
	while (nav.playing);
	nslog2(@"play2");
	[nav setFormatWav:88200]; // フォーマットだけ書き換えても音程は変わらない
	[nav scheduleBuffer:AVAudioPlayerNodeBufferInterruptsAtLoop]; // これがないと音が出ない
	nslog2(@"playing=%d",nav.playing);
	[nav play];
	nslog2(@"playing=%d",nav.playing);
	while (nav.playing);
	nslog2(@"playing=%d",nav.playing);
#endif
	
#if 0
#define MAXSTR	(8+1)
	NSString *np=@"だったとする[music8ch comment:ch];";
	nslog2(@"元文字列=%@",np);
	char *p=nsStringTocString(np);
	if (strlen(p)>=MAXSTR) {
		// コメント長が最大文字列長より大きい時
		int j=1;
		for (int i=1;i<=[np length];i++) {
			NSString *subStr=[np substringToIndex2:i];
			p=nsStringTocString(subStr);
			if (strlen(p)>=MAXSTR) {
				subStr=[np substringToIndex2:j]; // 1つ前が入りきる限界
				p=nsStringTocString(subStr);
				break;
			}
			// まだ最大文字列長に収まる
			j=i; // その位置を記録
		}
	}
	nslog2(@"切り出し文字列=%@(%tu)",@(p),strlen(p));
#endif
	
#if 0	// 構造体サイズ確認
	nslog2(@"VARIABLE1=%lu",sizeof(VARIABLE1));
	nslog2(@"VAR_CTRL=%lu",sizeof(VAR_CTRL));
	nslog2(@"VAR_CTRL_EXEC=%lu",sizeof(VAR_CTRL_EXEC));
	nslog2(@"DIM1=%lu",sizeof(DIM1));
	nslog2(@"DIM1_EXEC=%lu",sizeof(DIM1_EXEC));
#endif
	
#if 0
	// subStringテスト
	NSString *str=@"あW＊123";
	NSLog(@"str=%@",str);
	NSLog(@"subStringToIndex:0=%@",[str substringToIndex:0]);
	NSLog(@"subStringToIndex:1=%@",[str substringToIndex:1]);
	NSLog(@"subStringToIndex:2=%@",[str substringToIndex:2]);
	NSLog(@"substringFromIndex:0=%@",[str substringFromIndex:0]);
	NSLog(@"substringFromIndex:1=%@",[str substringFromIndex:1]);
	NSLog(@"substringFromIndex:2=%@",[str substringFromIndex:2]);

	str=@"🏿🌷a";
	NSLog(@"str=%@",str);
	NSLog(@"substringWithRange(0,1)=%@",[str substringWithRange:NSMakeRange(0,1)]); // これは表示が出ない
	NSLog(@"substringWithRange(0,2)=%@",[str substringWithRange:NSMakeRange(0,2)]); // 表示される
	NSLog(@"substringWithRange(2,1)=%@",[str substringWithRange:NSMakeRange(2,1)]); // これは表示が出ない
	NSLog(@"substringWithRange(2,2)=%@",[str substringWithRange:NSMakeRange(2,2)]); // 表示される
	NSLog(@"substringWithRange(3,1)=%@",[str substringWithRange:NSMakeRange(4,1)]); // 表示される
	NSLog(@"---");
	NSLog(@"subStringToIndex2:0=%@",[str substringToIndex2:0]);
	NSLog(@"subStringToIndex2:1=%@",[str substringToIndex2:1]);
	NSLog(@"subStringToIndex2:2=%@",[str substringToIndex2:2]);
	NSLog(@"---");
	NSLog(@"substringFromIndex2:0=%@",[str substringFromIndex2:0]);
	NSLog(@"substringFromIndex2:1=%@",[str substringFromIndex2:1]);
	NSLog(@"substringFromIndex2:2=%@",[str substringFromIndex2:2]);
	NSLog(@"---");
	NSLog(@"substringWithRange2(0,1)=%@",[str substringWithRange2:NSMakeRange(0,1)]);
	NSLog(@"substringWithRange2(0,2)=%@",[str substringWithRange2:NSMakeRange(0,2)]);
	NSLog(@"substringWithRange2(1,1)=%@",[str substringWithRange2:NSMakeRange(1,1)]);
	NSLog(@"substringWithRange2(2,1)=%@",[str substringWithRange2:NSMakeRange(2,1)]);
#endif
	
#if 0													// 32bit	64bit
	NSLog(@"sizeof(long)   =%lu",sizeof(long));				// 4		8
	NSLog(@"sizeof(int)    =%lu",sizeof(int));				// 4		4
	NSLog(@"sizeof(ULong)  =%ld",sizeof(ULong));			// 4		4
	NSLog(@"sizeof(CGFloat)=%ld",sizeof(CGFloat));			// 4		8
	NSLog(@"sizeof(double) =%ld",sizeof(double));			// 8		8
	NSLog(@"sizeof(float)  =%ld",sizeof(float));			// 4		4
	NSLog(@"sizeof(char *) =%ld",sizeof(char *));			// 4		8
	NSLog(@"sizeof(DOUBLE_LONG)=%lu",sizeof(DOUBLE_LONG));	// 8		8
	NSLog(@"sizeof(double)/sizeof(ULong)=%lu",MAX_DL);		// 2		2
	NSLog(@"sizeof(double)/sizeof(void*)=%lu",MAX_DP);		// 2		1
	
	DOUBLE_LONG dl;
	dl.l[LL]=0;
	dl.l[HL]=1;
	NSLog(@"LL=%d/HL=%d/LW=%d/HW=%d",LL,HL,LW,HW);
	
	NSLog(@"dl. ll=%lld/$%llx"	,dl.ll	,dl.ll);
	NSLog(@"dl.sll=%lld/$%llx"	,dl.sll	,dl.sll);
	NSLog(@"sizeof(dl.l)   =%ld",sizeof(dl.l));
	NSLog(@"sizeof(dl.l[0])=%ld",sizeof(dl.l[0]));
	NSLog(@"sizeof(dl.l[1])=%ld",sizeof(dl.l[1]));
#endif

#if 0
	nslog(@"platform=%@",getDevicePlatform());
	getDeviceType();
	CGRect r = [[UIScreen mainScreen] bounds];
	nslog(@"scrn=%@",NSStringFromCGRect(r));
#endif
	
#if 0	// 全フォント名を表示する
	NSArray *namesArray=[NSArray arrayWithArray:[UIFont familyNames]];
	NSEnumerator *emu=[namesArray objectEnumerator];
	NSString *name;
	while (name=[emu nextObject]) {
		nslog2(@"Font familyNames=%@",name);
		NSArray *namesArray2=[NSArray arrayWithArray:[UIFont fontNamesForFamilyName:name]];
		NSEnumerator *emu2=[namesArray2 objectEnumerator];
		NSString *name2;
		while (name2=[emu2 nextObject]) {
			nslog2(@"+-fontNames=%@",name2);
		}
	}
#endif
	
    @autoreleasepool {
		// plistからプログラム名とバージョンを得ておく
		programName=[[NSBundle mainBundle]infoDictionary][(NSString *)kCFBundleNameKey]; // これは常に日本語名なので注意
		nslog(@"programName=%@",programName);
		programVersion=[[NSBundle mainBundle]infoDictionary][(NSString *)kCFBundleVersionKey];
		nslog(@"programVersion=%@",programVersion);
		//
		// defaultデータベースに書きこむ；これにより、iOS設定でこれらの値が読めるようになる
		// Root.plistに書くkey名(Identifier)はdefault内のキー名またはRoot.strings内のキー名である
		// ただし、現在読んでいるのはバージョンのみ
		NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
		[defaults setObject:programName	   forKey:keyProgramName];
		[defaults setObject:programVersion forKey:keyProgramVersion];
		//
		return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

// 画面回転の許可条件(iOS6)
// okInterfaceOrientation()に合わせる
UIInterfaceOrientationMask supportedInterfaceOrientationsByMain(void)
{
//	nslog(@"%s",__FUNCTION__);
//	understandOrientation(interfaceOrientation); // 画面方向を記録
#if 0
	typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) {
		UIInterfaceOrientationMaskPortrait,
		UIInterfaceOrientationMaskLandscapeLeft,
		UIInterfaceOrientationMaskLandscapeRight,
		UIInterfaceOrientationMaskPortraitUpsideDown,
		UIInterfaceOrientationMaskLandscape,
		UIInterfaceOrientationMaskAll,
		UIInterfaceOrientationMaskAllButUpsideDown,
	};
#endif
	// これが↓なら、shouldAutorotateByMainは呼び出されないようである
	return UIInterfaceOrientationMaskAll;
}

// iOS6
BOOL shouldAutorotateByMain(void)
{
//	nslog(@"%s:fRotaionLock=%d",__FUNCTION__,fRotaionLock);
	if (!fRotaionLock) {
		return YES;
	}
	return NO;
}

//--------------------------------------------------------------------------------------
