//
//  PickerView.m
//  ZeroRec/Komadori
//
//  Created by AIG-Soft on 11/02/24.
//  Copyright 2011 AIG-Soft. All rights reserved.
//

#import "AppDelegate.h"
#import "PickerView.h"
#import	"click.h"

@interface PickerView()
<UIPickerViewDelegate,UIPickerViewDataSource>
{
	AppDelegate *app;
	Setup *setup;

	NSArray			*rowStrings;	// 項目文字列
	NSUInteger		rows;			// 項目数
	
	// 一時保存
	NSString 		*title;
	NSTimeInterval *tbl;
	NSUInteger		cnt;
	BOOL			fminute;
	NSUInteger		no;
	NSUInteger		fchange;
}
@end

@implementation PickerView

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

-(void)viewDidLoad
{
	printSelf();
	[super viewDidLoad];

	// iOS7で、viewの下にあるUIScrollViewを内包するUI要素(UITableView/UIScrollView/UIWebViewなど)のスクロール開始位置が勝手にずらされるのを禁止する
	self.automaticallyAdjustsScrollViewInsets=NO;
	
	app=getAppDelegate();
	setup=app.setup;

	_pickerView.delegate  =self;
	_pickerView.dataSource=self;
//	_pickerView.backgroundColor=[UIColor clearColor];

	// 影
	CALayer *layer=_pickerView.layer;
	layer.cornerRadius=5;
	layer.masksToBounds = NO;
	layer.shadowOffset = CGSizeMake(1, 2);
	layer.shadowRadius = 5;
	layer.shadowOpacity = 0.5;

	// タイトル設定
	self.title=title;
	
	// 項目設定
	rowStrings=[setup setTimeIntervalMessages:tbl count:cnt fmin:fminute fchange:fchange];
	rows=cnt;
	
	// カーソル位置計算
	_selectRow=searchTbl(tbl,[setup getTmValues:no],cnt);
	nslog(@"[no=%d/cnt=%d]=%f;line=%d",(int)no,(int)cnt,[setup getTmValues:no],(int)_selectRow);
	
	// ボタン設定
	self.navigationItem.leftBarButtonItem=	[[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"SAVE",@"保存")
																  style:UIBarButtonItemStyleDone
																 target:self
																 action:@selector(closePickerView:)];
}

-(void)closePickerView:(id)sender
{
	if ([self.delegate respondsToSelector:@selector(AtPickerViewClose:)]) {
		[self.delegate performSelector:@selector(AtPickerViewClose:) withObject:self];
	}
	// pickerを閉じる
	clickSound(); // クリック音
	[self.navigationController popViewControllerAnimated:YES];
}


- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	// 画面方向に応じた画面設定をする
	[self willAnimateRotationToInterfaceOrientation:self.interfaceOrientation duration:0.0];
	// カーソル位置設定;ここでしないとうまくいかない
	[_pickerView selectRow:_selectRow inComponent:0 animated:NO];
}

// NSTimeInterval設定用
- (void)initPickerView:(NSString *)title_
				 param:(NSTimeInterval *)tbl_
				 count:(NSUInteger)cnt_
				  fmin:(BOOL)fminute_
				 valno:(NSUInteger)no_
			   fchange:(NSUInteger)fchange_
{
	printSelfs(@"%@",title);
	title	=title_;
	tbl		=tbl_;
	cnt		=cnt_;
	fminute	=fminute_;
	no		=no_;
	fchange	=fchange_;
}

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

// 列数を返す
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
	// すべての項目が列＝1
	return 1;
}

// 指定行の項目数を返す
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
	return rows;
}

// 指定行項目の文字列を返す
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
	return rowStrings[row];
}

// 選択時に呼び出される
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
	nslog(@"選択:(%d,%d)",(int)row,(int)component); // componentは0のはず
	_selectRow=row;
#if 0
	// ここにこれを入れると、選択直後に表示が変わる
	[pickerView reloadAllComponents]; // 項目の全読み込み直し
#endif
}

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

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

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

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

#pragma mark -
#pragma mark Memory management

#ifdef DEBUG
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
	nslog(@"メモリ警告発生:PickerView");
	[super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}
#endif


@end

