//
//  CounterView.m
//  ZeroRec
//
//  Created by AIG-Soft on 11/03/26.
//  Copyright 2011 AIG-Soft. All rights reserved.
//

#import "CounterView.h"
#import "Setup.h"
#import "click.h"
#import "libs.h"
#import "AppDelegate.h"

@interface CounterView()
{
	AppDelegate *app;
	Setup *setup;
}
@end

@implementation CounterView

-(void)setCounters
{
	printSelf();
	// nib部の設定
	NSUInteger apics,cpics;
	apics=[setup getPictures:PICTURE_ALARM];
	cpics=[setup getPictures:PICTURE_CONSTANT];
	labelPICTURES_TOTAL.text		=NSLocalizedString(@"PICTURES_TOTAL"	,@"総録画枚数");
	labelNumPICTURES_TOTAL.text		=[NSString stringWithFormat:@"%8d\n"	,(int)(apics+cpics)];
	//
	labelPICTURES_ALARM.text		=NSLocalizedString(@"PICTURES_ALARM"	,@"アラーム録画");
	labelNumPICTURES_ALARM.text		=[NSString stringWithFormat:@"%8d\n"	,(int)(apics)];
	//
	labelPICTURES_CONSTANT.text		=NSLocalizedString(@"PICTURES_CONSTANT"	,@"常時録画");
	labelNumPICTURES_CONSTANT.text	=[NSString stringWithFormat:@"%8d\n",(int)cpics];
	//
	labelPICTURES_LATEST.text		=NSLocalizedString(@"PICTURES_LATEST"	,@"直近録画");
	labelNumPICTURES_LATEST.text	=[NSString stringWithFormat:@"%8d\n",(int)[setup getPictures:PICTURE_LATEST]];
}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
// アクションシートのボタンが押された
{
	printSelf();
	if (buttonIndex!=actionSheet.cancelButtonIndex) {
		// 録画枚数リセット
		nslog(@"録画枚数リセット");
		for (NSUInteger i=PICTURE_LATEST;i<MAX_PICTURES;i++) {
			[setup setPictures:0 valno:i];
		}
		// 録画枚数リセットログ
		setLog(LOG_NAME_CNT0);
	}
	clickSound();
	[self setCounters]; // 表示内容更新
	[self.view setNeedsDisplay]; // 再描画要請(枚数表示)
}

-(IBAction)pushResetButton:(id)sender
{
	printSelf();
	// UIActionSheetは毎回alloc&initしないと二回目以降化けてしまう
	asheet = [[UIActionSheet alloc]
			  initWithTitle:NSLocalStr(@"RESET_OK")
					delegate:self
		   cancelButtonTitle:NSLocalStr(@"NO")		// ボタン番号=cancelButtonIndex
	  destructiveButtonTitle:nil
		   otherButtonTitles:NSLocalStr(@"YES"),	// ボタン番号=firstOtherButtonIndex 
	 nil
	];
	setActionSheetResize(asheet); // リサイズ指定
	// 表示
	clickSound();
	[asheet showInView:self.view];
}

-(void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	// 画面方向に応じた画面設定をする
	[self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0.0];
}

-(void)viewDidLoad
{
	printSelf();
	[super viewDidLoad];
	[self setCounters]; // nib部の設定
	//
	NSString *title=NSLocalizedString(@"RESET",@"カウンターリセット");
	[_btnReset			setTitle:title forState:UIControlStateNormal];
	[_btnResetLandscape	setTitle:title forState:UIControlStateNormal];
}

- (void)initCounterView:(NSString *)title
{
	app=getAppDelegate();
	setup=app.setup;
	self.title=title; // titleプロパティへの代入
	UIBarButtonItem *tempBarItem=	[[UIBarButtonItem alloc]
										   initWithImage:[UIImage imageNamed:@"back"]
										   style:UIBarButtonItemStyleDone
										   target:self
										   action:@selector(closeCounterView:)];
	self.navigationItem.leftBarButtonItem=tempBarItem;
	asheet=nil; // 初回
}

-(void)closeCounterView:(id)sender
{
	printSelf();
	// これは設定処理ではないので保存なし
	
	clickSound(); // クリック音
	[self.navigationController popViewControllerAnimated:YES];
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
// 回転検出
// UIViewControllerの標準メソッドのオーバーライド
// 呼び出されるのは回転「前」のインスタンス
{
	printSelfs(@"Orientation=%d",(int)interfaceOrientation);
	switch (interfaceOrientation) {
		default: // 警告回避のため
		case UIInterfaceOrientationPortrait:
		case UIInterfaceOrientationPortraitUpsideDown:
			nslog(@"縦:CounterView");
			// 縦のを表示
			_btnReset.hidden=NO;
			// 横のを消す
			_btnResetLandscape.hidden=YES;
			break;
		case UIInterfaceOrientationLandscapeLeft:
		case UIInterfaceOrientationLandscapeRight:
			nslog(@"横:CounterView");
			// 縦のを消す
			_btnReset.hidden=YES;
			// 横のを表示
			_btnResetLandscape.hidden=NO;
			break;
	}
	nslog(@"回転終了");
}

//----------------------------------------------
// 画面を回転して良いかどうか
//----------------------------------------------
// iOS6.0

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

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

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

#ifdef DEBUG
- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

#endif

@end
