#import "StyleSettingsController.h"
#import "AppDelegate.h"
#import "OnOffTableCell.h"
#import "SliderTableCell.h"

@interface FontTableCell : UITableViewCell {
	UILabel *label;
	UISegmentedControl *control;
	NSString* notification;
}

@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UISegmentedControl *control;
@property (nonatomic, retain) NSString *notification;

@end

@implementation FontTunesTableCell

@synthesize label;
@synthesize control;
@synthesize notification;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
	if (self) {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    CGRect frame = CGRectMake(18.0, 44.0, 264.0, 32.0);
    control = [[UISegmentedControl alloc] initWithFrame:frame];
    [control insertSegmentWithTitle:@"Index" atIndex:0 animated:NO];
    [control insertSegmentWithTitle:@"Title" atIndex:1 animated:NO];
    [control insertSegmentWithTitle:@"Key" atIndex:2 animated:NO];
    [control insertSegmentWithTitle:@"Meter" atIndex:3 animated:NO];
    control.selectedSegmentIndex = appDelegate.tuneSortOrder;
    [control addTarget:self action:@selector(sortAction:) forControlEvents:UIControlEventValueChanged];
		[[self contentView] addSubview:control];
		
		label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 2.0, 150.0, 40.0)];
    label.backgroundColor = [UIColor clearColor];
		[[self contentView] addSubview:label];
		self.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	return self;
}

- (void)sortAction:(UISwitch*)c {
	[[NSNotificationCenter defaultCenter] postNotificationName:self.notification object:c];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
	[super setSelected:selected animated:animated];
}

@end

enum {
  kPageSection,
  kTitleSection,
  kFormatSection,
};

@implementation StyleSettingsController

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

- (void)viewDidLoad
{	
  [super viewDidLoad];
  self.tableView.scrollsToTop = NO;
}

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

- (void)viewWillAppear:(BOOL)animated
{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  sortOrder = appDelegate.tuneSortOrder;
}

- (void)viewWillDisappear:(BOOL)animated
{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  if ( sortOrder != appDelegate.tuneSortOrder ) {
    appDelegate.tuneSortOrder = sortOrder;
    [appDelegate sortTunes];
  }
}


#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return 3;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
  switch ( section ) {
  case kPageSection:
    return @"Page";
  case kTitleSection:
    return @"Title";
  case kFormatSection:
    return @"Music";
	}
  return @"Other";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  switch ( section ) {
  case kPageSection:
    return 1;
  case kTitleSection:
    return 2;
  case kFormatSection:
    return 1;
	}
  return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//	if (indexPath.section == kViewSection && indexPath.row == 0)
//    return 90.0;
	return (indexPath.section == kPageSection && indexPath.row == 0) ? 90.0 : 44.0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

	if ( indexPath.section == kPageSection) {
		OnOffTableCell* cell = (OnOffTableCell*)[tableView dequeueReusableCellWithIdentifier:@"OnOffCell"];
		if ( cell == nil ) {
			CGRect rect;
			rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
			cell = [[[OnOffTableCell alloc] initWithFrame:rect reuseIdentifier:@"OnOffCell"] autorelease];
			if ( indexPath.row == 0 ) {
        cell.label.text = @"Guitar Chords";
        cell.switchControl.on = appDelegate.guitarChords;
        [cell.switchControl addTarget:self action:@selector(guitarChordsAction:) forControlEvents:UIControlEventValueChanged];
      } else {
        cell.label.text = @"Loop Playback";
        cell.switchControl.on = appDelegate.loopPlayback;
        [cell.switchControl addTarget:self action:@selector(loopPlaybackAction:) forControlEvents:UIControlEventValueChanged];
      }
    }
		return cell;
#if TARGET_IPAD
	} else if ( indexPath.section == kTitleSection) {
		OnOffTableCell* cell = (OnOffTableCell*)[tableView dequeueReusableCellWithIdentifier:@"OnOffCell"];
		if ( cell == nil ) {
			CGRect rect;
			rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
			cell = [[[OnOffTableCell alloc] initWithFrame:rect reuseIdentifier:@"OnOffCell"] autorelease];
			if ( indexPath.row == 0 ) {
        cell.label.text = @"Guitar Chords";
        cell.switchControl.on = appDelegate.guitarChords;
        [cell.switchControl addTarget:self action:@selector(guitarChordsAction:) forControlEvents:UIControlEventValueChanged];
      } else {
        cell.label.text = @"Loop Playback";
        cell.switchControl.on = appDelegate.loopPlayback;
        [cell.switchControl addTarget:self action:@selector(loopPlaybackAction:) forControlEvents:UIControlEventValueChanged];
      }
    }
		return cell;
#endif
	} else if ( indexPath.section == kViewSection) {
		OnOffTableCell* cell = (OnOffTableCell*)[tableView dequeueReusableCellWithIdentifier:@"OnOffCell"];
		if ( cell == nil ) {
			CGRect rect;
			rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
			cell = [[[OnOffTableCell alloc] initWithFrame:rect reuseIdentifier:@"OnOffCell"] autorelease];
			if ( indexPath.row == 0 ) {
        cell.label.text = @"Guitar Chords";
        cell.switchControl.on = appDelegate.guitarChords;
        [cell.switchControl addTarget:self action:@selector(guitarChordsAction:) forControlEvents:UIControlEventValueChanged];
      } else {
        cell.label.text = @"Loop Playback";
        cell.switchControl.on = appDelegate.loopPlayback;
        [cell.switchControl addTarget:self action:@selector(loopPlaybackAction:) forControlEvents:UIControlEventValueChanged];
      }
    }
		return cell;
	} else if ( indexPath.section == kMusicSection) {
		OnOffTableCell* cell = (OnOffTableCell*)[tableView dequeueReusableCellWithIdentifier:@"OnOffCell"];
		if ( cell == nil ) {
			CGRect rect;
			rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
			cell = [[[OnOffTableCell alloc] initWithFrame:rect reuseIdentifier:@"OnOffCell"] autorelease];
			if ( indexPath.row == 0 ) {
        cell.label.text = @"Guitar Chords";
        cell.switchControl.on = appDelegate.guitarChords;
        [cell.switchControl addTarget:self action:@selector(guitarChordsAction:) forControlEvents:UIControlEventValueChanged];
      } else {
        cell.label.text = @"Loop Playback";
        cell.switchControl.on = appDelegate.loopPlayback;
        [cell.switchControl addTarget:self action:@selector(loopPlaybackAction:) forControlEvents:UIControlEventValueChanged];
      }
    }
		return cell;
	}

  return cell;
}

- (IBAction)sortAction:(id)sender
{
  UISegmentedControl *control = (UISegmentedControl *)sender;
  sortOrder = control.selectedSegmentIndex;
}

- (IBAction)guitarChordsAction:(id)sender
{
  UISwitch *control = (UISwitch *)sender;
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.guitarChords = control.on;
}

- (IBAction)loopPlaybackAction:(id)sender
{
  UISwitch *control = (UISwitch *)sender;
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.loopPlayback = control.on;
}

#if TARGET_IPAD
- (IBAction)transitionAction:(id)sender
{
  UISegmentedControl *control = (UISegmentedControl *)sender;
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.transitionType = control.selectedSegmentIndex;
}
#endif

- (IBAction)portNumberAction:(id)sender
{
  UITextView *control = (UITextView *)sender;
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.serverPort = [control.text intValue];
  printf("port %d\n", appDelegate.serverPort);
}

@end


@interface FontTableCell : UITableViewCell<UIPickerViewDataSource, UIPickerViewDelegate> {
	UILabel *label;
	UIPickerView *control;
	NSString* notification;
  NSArray* fontNames;
}

@property (nonatomic, retain) UILabel *label;
@property (nonatomic, retain) UIPickerView *control;
@property (nonatomic, retain) NSString *notification;
@property (nonatomic, retain) NSArray* fontNames;

@end

@implementation FontTableCell

@synthesize label;
@synthesize control;
@synthesize notification;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
	if ( self ) {
    fontNames = [UIFont familyNames];
    [fontNames retain];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    CGRect frame = CGRectMake(18.0, 44.0, 300.0, 162.0);
    control = [[UIPickerView alloc] initWithFrame:frame];
    [control setDelegate:self];
//    control.selectedSegmentIndex = appDelegate.tuneSortOrder;
//    [control addTarget:self action:@selector(sortAction:) forControlEvents:UIControlEventValueChanged];
		[[self contentView] addSubview:control];
		
		label = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 2.0, 150.0, 40.0)];
    label.backgroundColor = [UIColor clearColor];
		[[self contentView] addSubview:label];
		self.selectionStyle = UITableViewCellSelectionStyleNone;
	}
	return self;
}

#pragma mark UIPickerView delegate methods

- (int)numberOfComponentsInPickerView:(UIPickerView *)picker
{
  return 1;
}

- (int)pickerView:(UIPickerView *)picker numberOfRowsInComponent:(int)col
{
  return [fontNames count];
}

//- (int)rowForFont:(NSString *)fontName
//{
//  int count = [fontNames count];
//  for (int i = 0; i < count; i++)
//    if ([[fontNames objectAtIndex:i] isEqualToString:fontName])
//      return i;
//  return 0;
//}

#pragma mark UIPickerView delegate methods

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//{
//  return 40.0f;
//}

//- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
//{
//  return 20.0f;
//}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
  return [fontNames objectAtIndex:row];
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

//- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

//- (UIPickerTableCell *)pickerView:(UIPickerView *)picker tableCellForRow:(int)row inColumn:(int)col
//{
//  UIPickerTableCell *cell = [[UIPickerTableCell alloc] init];
//  NSString *fontName = [fontNames objectAtIndex:row];
//  [cell setTitle:fontName];
//  [[cell titleTextLabel] setFont:[UIFont fontWithName:fontName size:22.0f]];
//  return cell;
//}
//
//- (void)pickerView:(UIPickerView *)picker didSelectRow:(int)row inComponent:(int)component
//{
////    if ([delegate respondsToSelector:@selector(setFont:)])
////        [delegate performSelector:@selector(setFont:)
////                       withObject:[fontNames objectAtIndex:row]];
//}

