#import "TuneViewController.h"
#import "Tune.h"

@implementation TuneViewController

@synthesize tune;

-(id<TuneViewDelegate>)tuneViewDelegate
{
	return tuneView.delegate;
}

-(void)setTuneViewDelegate:(id<TuneViewDelegate>)delegate
{
	tuneView.delegate = [delegate retain];
}

-(id)initWithTune:(Tune *)aTune
{
	self = [super initWithNibName:@"TuneView" bundle:nil];
	if ( self != nil ) {
    self.tune = aTune;
		self.title = aTune.title;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewTapped:) name:@"tap" object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDoubleTapped:) name:@"doubleTap" object:nil]; 
	}
	return self;
}

-(void)viewDidLoad {
  barsHidden = false;
	scrollView = [[UIScrollView alloc] initWithFrame:[mainView frame]];
	[scrollView setContentSize:CGSizeMake(576 * kTuneScale, 792 * kTuneScale)];
	[scrollView setScrollEnabled:YES];
  scrollView.minimumZoomScale = 0.5f;
  scrollView.maximumZoomScale = 6.0f;
  scrollView.delegate = self;
	scrollView.autoresizesSubviews = YES;
  scrollView.clipsToBounds = YES;
	scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

  tuneView = [[TuneView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.0, 1.0)];
  tuneView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  tuneView.scale = 1.0f;

	tuneView.frame = CGRectMake( 0, 0, 576 * kTuneScale, 792 * kTuneScale );
  tuneView.tune = tune;
  tuneView.contentMode = UIViewContentModeRedraw;
  tuneView.tuneTransform = CGAffineTransformIdentity;

	[scrollView addSubview:tuneView];
	[mainView addSubview:scrollView];

	[scrollView release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}

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

-(void)dealloc {
	[tuneView.delegate release];
	[tuneView release];
	[super dealloc];
}

- (IBAction)hideStatusBar {	
  [[self navigationController] setNavigationBarHidden:YES animated:YES];
  barsHidden = true;
}

- (IBAction)showStatusBar {	
  [[self navigationController] setNavigationBarHidden:NO animated:YES];
  barsHidden = false;
}

- (void)viewTapped:(id)sender {
  if ( barsHidden )
    [self showStatusBar];
  else
    [self hideStatusBar];
}

- (void)viewDoubleTapped:(id)sender {
  tuneView.scale = 1.0f;
  tuneView.transform = CGAffineTransformIdentity;
	tuneView.frame = CGRectMake( 0, 0, 576*kTuneScale, 792*kTuneScale );
	scrollView.contentSize = CGSizeMake( 576*kTuneScale, 792*kTuneScale );
  scrollView.contentOffset = CGPointMake(0.0f, 0.0f);
  [tuneView setNeedsLayout];
  [tuneView setNeedsDisplay];
  [self hideStatusBar];
  [self scrollViewDidEndZooming:scrollView withView:self.view atScale:1.0f];
}

#pragma mark UIViewController delegate methods

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
  return tuneView;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)sv withView:(UIView *)view atScale:(float)scale;
{
  float oldScale = tuneView.scale;
  scale *= oldScale;

  [tuneView removeFromSuperview];
  [tuneView release];

  tuneView = [[TuneView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.0, 1.0)];
  tuneView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  tuneView.tune = tune;
  tuneView.contentMode = UIViewContentModeRedraw;
  tuneView.scale = scale;

  CGSize contentSize;
  contentSize.width = 576*kTuneScale * scale;
  contentSize.height = 792*kTuneScale * scale;

  CGSize scrollFrameSize = sv.bounds.size;
  CGPoint frameOrigin;
  frameOrigin.x = (contentSize.width < scrollFrameSize.width) ? ((scrollFrameSize.width - contentSize.width) / 2) : 0;
  frameOrigin.y = (contentSize.height < scrollFrameSize.height) ? ((scrollFrameSize.height - contentSize.height) / 2) : 0;

  [tuneView setTransform:CGAffineTransformIdentity];
  [tuneView setFrame:CGRectMake(frameOrigin.x, frameOrigin.y, contentSize.width, contentSize.height)];
  [sv setContentSize:contentSize];
  [sv setNeedsDisplay];
  [sv addSubview:tuneView];

  [tuneView setNeedsDisplay];
}

@end
