//
//  RemoteViewController.m
//  ZeroRec/Komadori
//
//  Created by AIG-Soft on 11/05/10.
//  Copyright 2011 AIG-Soft. All rights reserved.
//

#import "RemoteViewController.h"
#import	"Bluetooth.h"
#import	"click.h"
#import "libs.h"
#import "main.h"
#import "AppDelegate.h"

//#define	TEST

@interface RemoteViewController()
<BluetoothDelegate>
@end


@implementation RemoteViewController

#pragma mark BluetoothDelegate

// 応答受信関数
- (void)bluetoothDidRecieve:(NSData *)data peer:(MCPeerID *)peerID
{
	printSelf();
	unichar c=[bluetooth readReceiveBuff];
	nslog(@"command=%c($%04x)",c,c);
	switch (c) {
#ifdef TEST
		case BT_COMMAND_SHUTTER:
			Alert1(@"BT_COMMAND_NSHOTBT_COMMAND_SHUTTER",@"BT_COMMAND_SHUTTER",nil);
			[bluetooth send:BT_COMMAND_RESULT];

			break;
		case BT_COMMAND_NSHOT:
			Alert1(@"BT_COMMAND_NSHOT",@"BT_COMMAND_NSHOT",nil);
			[bluetooth send:BT_COMMAND_RESULT];
			break;
#endif
			
		case BT_COMMAND_ERROR:
			nslog(@"エラー受信");
			Alert1(@"RECORDER_ERROR",@"RECORDER_ERROR_MESSAGE",nil);
			break;
			
		case BT_COMMAND_RECEND:
			nslog(@"RecEnd受信");
#ifdef TEST
			Alert1(@"受信",@"BT_COMMAND_RECEND",nil);
#else
			btnNShot.enabled=
			btnShutter.enabled=NO; // シャッター系ボタンを無効にする
#endif
			// 閉じる処理
#ifndef TEST
			clickSound(); // クリック音
			[self.navigationController popViewControllerAnimated:YES];
#endif
			break;
		
		case BT_COMMAND_RECSTART:
			nslog(@"RecStart受信");
#ifdef TEST
			Alert1(@"受信",@"BT_COMMAND_RECSTART",nil);
#endif
			btnNShot.enabled=
			btnShutter.enabled=YES; // シャッター系ボタンを有効にする
			break;
			
		case BT_COMMAND_RESULT:
			// 正常受信されたようなのでクリック音を出す
#ifdef TEST
			Alert1(@"受信",@"BT_COMMAND_RESULT",nil);
#endif
			clickSound();
			break;

		case BT_COMMAND_DISCONNECT:
#ifdef TEST
			Alert1(@"受信",@"BT_COMMAND_DISCONNECT",nil);
#endif
			[self stop:nil];
		{
			AppDelegate *app=getAppDelegate();
			[app.rootViewController disconnectBluetooth:NO];
		}
			break;
			
	}
}

// 来ないけど実装必須のため
-(void)bluetoothSelectShow:(MCBrowserViewController *)mvc
{
	[self presentViewController:mvc animated:YES completion:nil];
}

-(void)bluetoothCancelConnect:(Bluetooth *)bt
{
	printSelf();
	bt.fremote=BT_REMOTE_CANCEL;
	// 相手をキャンセルさせたいけど、接続できてないから無理
//	[self stop:self]; // recendさせる
}

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

-(void)stop:(id)sender
{
	printSelf();
	if (sender!=nil) {
		[bluetooth send:BT_COMMAND_RECEND];
	}
	btnNShot.enabled=
	btnShutter.enabled=NO; // シャッター系ボタンを無効にする
	// 閉じる処理
	clickSound(); // クリック音
	[self.navigationController popViewControllerAnimated:YES];
}

// ボタン押下時の処理
-(IBAction)buttonAction:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	// コマンド送信
	switch (btn.tag) {
		case BTN_REMOTE_NSHOT:
			nslog(@"BTN_REMOTE_NSHOT");
			[bluetooth send:BT_COMMAND_NSHOT];
			break;
		case BTN_REMOTE_SHUTTER:
			nslog(@"BTN_REMOTE_SHUTTER");
			[bluetooth send:BT_COMMAND_SHUTTER];
			break;
		case BTN_REMOTE_RECEND:
			[self stop:sender];
			break;
	}
}

-(void)open:(Bluetooth *)bluetooth_
{
	printSelf();
	self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:NSLocalStr(@"STOP")
																		  style:UIBarButtonItemStyleDone
																		 target:self
																		 action:@selector(stop:)];
	bluetooth	=bluetooth_;
	// 受信関数登録
	bluetooth.delegate=self;
}

- (void)viewDidLoad
{
	printSelf();
    [super viewDidLoad];
	self.title=NSLocalStr(@"BT_REMOTE_TITLE");
//#ifndef TEST
	// 最初はシャッターボタンは無効
	btnNShot.enabled=
	btnShutter.enabled=NO;
//#endif
#ifndef USE_NSHOT
	btnNShot.hidden=YES;	// n-Shot無効時はボタンを隠す
#endif
}

-(void)viewDidAppear:(BOOL)animated
{
	printSelf();
	[super viewDidAppear:animated];
	if (bluetooth.fremote==BT_REMOTE_CANCEL) {
		// 接続先選択でキャンセルされた時はすぐ消してしまう
		[self.navigationController popViewControllerAnimated:YES];
	}

}

// 回転検出
// UIViewControllerの標準メソッドのオーバーライド
// 呼び出されるのは回転「前」のインスタンス
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
	printSelfs(@"Orientation=%d",(int)interfaceOrientation);
	// 画面x中央寄せ
	CGPoint center;
	CGFloat x=self.view.center.x;
	center=btnRecEnd.center;  center.x=x; btnRecEnd.center =center;
	center=btnShutter.center; center.x=x; btnShutter.center=center;
	center=btnNShot.center;	  center.x=x; btnNShot.center  =center;
#if 0
	switch (interfaceOrientation) {
		case UIInterfaceOrientationPortrait:
		case UIInterfaceOrientationPortraitUpsideDown:
			nslog(@"縦");
			btnNShot.center.x=
			btnRecEnd.center.x=
			btnShutter.centerx=x;
			break;
			
		case UIInterfaceOrientationLandscapeLeft:
		case UIInterfaceOrientationLandscapeRight:
			nslog(@"横");
			break;
	}
#endif
}
//----------------------------------------------
// 画面を回転して良いかどうか
//----------------------------------------------
// iOS6.0

-(BOOL)shouldAutorotate
{
	return shouldAutorotateByMain();
}

- (NSUInteger)supportedInterfaceOrientations
{
	return (supportedInterfaceOrientationsByMain());
}

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
