//
//  PickerView.m
//  traceWrite
//
//  Created by 今井 彰彦 on 2011/12/05
//  Copyright 2011 AIG-Soft. All rights reserved.
//

#import "PickerView.h"
#import "Setup.h"
#import	"click.h"
#import "libs.h"
#import "main.h"

@interface PickerView()
{
	NSString *nextButtonTitle;	// 設定値をしかるべき時点まで保存すること
	id	nextTarget;
	SEL	nextSelector;

	NSUInteger	rows;			// 項目数
}
@end

@implementation PickerView

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

#pragma mark -初期化

-(instancetype)init
{
	self=[super init];
	if (self) {
		nextButtonTitle=nil;
	}
	return self;
}

-(void)viewDidLoad
{
	printSelf();
	[super viewDidLoad];
	_pickerView.delegate  =self;
	_pickerView.dataSource=self;
	
	self.preferredContentSize=self.view.frame.size; // poppverのサイズ合わせ(これが重要)
}

- (void)viewWillAppear:(BOOL)animated
{
	printSelf();
	[super viewWillAppear:animated];
	
	// カーソル位置設定
	[_pickerView selectRow:_selectRow inComponent:0 animated:NO];
	_pickerView.showsSelectionIndicator=YES;
	//
	CALayer *layer=_pickerView.layer;
	layer.cornerRadius  = 3;
	layer.masksToBounds = NO;
	layer.shadowOffset  = CGSizeMake(2, 2);
	layer.shadowRadius  = 2;
	layer.shadowOpacity = 1;
	
	if (nextButtonTitle!=nil) { // DidLoad後でないと設定できない
		[self.btnNext setTitle:NSLocalStr(nextButtonTitle) forState:UIControlStateNormal];
		self.btnNext.hidden=NO;
	}
}

- (void)initPickerView:(id)retTarget retSelector:(SEL)retSelector title:(NSString *)title colmuns:(NSArray *)cols selectRow:(NSUInteger)selectRow_
{
	printSelfs(@"%@",title);
	
	// タイトル設定
	self.title=title;

	// 項目設定
	rows=[cols count];
	nslog(@"rows=%u",(unsigned int)rows);
	self.rowStrings=cols;
	_selectRow=selectRow_;
	// ボタン設定
	self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:NSLocalStr(@"SAVE")
																 style:UIBarButtonItemStyleDone
																target:retTarget
																action:retSelector];
}

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

-(void)setNextButtonTitle:(NSString *)text
{
	printSelfs(@"%@",text);
	nextButtonTitle=text; // しかるべき時まで保存
}

-(void)setNextButton:(id)target_ nextSelector:(SEL)selector_
{
	printSelf();
	nextTarget	=target_;
	nextSelector=selector_;
}

-(IBAction)nextButtonAction:(id)sender
{
	printSelf();
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"        // 警告回避のため
	nslog(@"nextTarget=%@",nextTarget);
	[nextTarget performSelector:nextSelector withObject:sender];
#pragma clang diagnostic pop
}

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

#pragma mark -Picker delegate

// 列数を返す
- (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
{
	printSelf();
	return shouldAutorotateByMain();
}

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

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

#pragma mark -
#pragma mark -Memory management

#ifdef DEBUG
- (void)didReceiveMemoryWarning
{
	nslog(@"メモリ警告発生:PickerView");
	[super didReceiveMemoryWarning];
}
#endif

@end

