//
//  BunshoSelect.m
//  traceWrite
//
//  Created by 今井 彰彦 on 2012/02/19
//  Copyright 2012 AIG-Soft. All rights reserved.
//

#import "BunshoSelect.h"
#import "Setup.h"
#import	"click.h"
#import "libs.h"
#import "main.h"
#import "Bunsho.h"
#import "BunshoList.h"
#import "AppDelegate.h"

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

@end

@implementation BunshoSelect

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

-(void)viewDidLoad
{
	printSelf();
	[super viewDidLoad];
	
	app=getAppDelegate();
	setup=app.setup;
	
	// デリゲートとソース設定
	self.pickerView.delegate  =self;
	self.pickerView.dataSource=self;
}

- (void)initPickerView:(id)retTarget retSelector:(SEL)retSelector title:(NSString *)title colmuns:(NSArray *)cols selectRow:(NSUInteger)selectRow_
{
	[super initPickerView:retTarget retSelector:retSelector title:title colmuns:cols selectRow:selectRow_];
	_nextTitle=nil;
}

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

-(void)setNextButton:(NSString *)title
{
	printSelf();
	[super setNextButtonTitle:title];
	[super setNextButton:self nextSelector:@selector(openFontSelect:)];
}

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

// _fontSelectを閉じる
-(void)closeFontSelect:(id)sender
{
	printSelf();
	
	// 選択値を読み出す(pickerを開放してしまうので、その前に)
	setup.fontName	=[_fontSelect no2FontName:_fontSelect.nameNo];
	// plistにも設定：名前とサイズ
	writeBunshoFont(setup.bunFileName,setup.fontName,setup.fontSize);
	
	// 画面を閉じる
	clickSound(); // クリック音
	[self.navigationController popViewControllerAnimated:YES];
}

// フォント選択
-(void)openFontSelect:(id)sender
{
	printSelf();
	//
	NSArray *pathes=[Bunsho bunList];
	NSString *path=pathes[self.selectRow];
	setup.bunFileName=[path lastPathComponent];
	nslog(@"bunFileName=%@",setup.bunFileName);
	// 文書名からフォント名を取得
	NSString *fontName;
#ifdef DEBUG
	CGFloat fontSize=
#endif
	readBunshoFont(setup.bunFileName,&fontName);
	nslog(@"fontName=%@",fontName);
	nslog(@"fontSize=%f",fontSize);
	//
	self.fontSelect = [[FontSelect alloc]initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]]; // _fontSelectはPickerViewを使って開く
	[_fontSelect initFontSelect:self selector:@selector(closeFontSelect:) name:fontName title:NSLocalStr(@"FONT")];
	[_fontSelect setNextButton:NSLocalStr(@"FONTSIZE")];

	// サンプル文字の設定;文章の先頭1文字
	Bunsho *bunsho=[[Bunsho alloc]init];
	[bunsho load:path];
	
	nslog(@"実質的な先頭1文字を得る");
///	nslog(@"bunsho.text=%@",bunsho.text); ///
	NSString *c=@"A"; // 警告回避のため
	unichar cc;
	for (int i=0;i<[bunsho.text length];i++) {
		c=[bunsho.text substringWithRange:NSMakeRange(i, 1)];
		nslog(@"c=%@<",c);
		cc=[c characterAtIndex:0];
		nslog(@"cc=$%02x",cc);
		if (cc>' ') {
			break;
		}
		// 空白文字は飛ばす
	}
	[_fontSelect setSample:c];
	
	// 子画面呼び出し
	clickSound(); // クリック音
	[self.navigationController pushViewController:_fontSelect animated:YES];
}

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

#pragma mark -
#pragma mark -Memory management

#ifdef DEBUG
- (void)didReceiveMemoryWarning
{
	printSelf();
	[super didReceiveMemoryWarning];
}
#endif


@end

