//
//  Isgl3dViewController.m
//  Apollonian Viewer
//
//  Created by Kean on 5/4/12.
//  Copyright 2012 Autodesk. All rights reserved.
//

#import "Isgl3dViewController.h"
#import "isgl3d.h"

@implementation Isgl3dViewController

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

- (BOOL) shouldAutorotateToInterfaceOrientation
  :(UIInterfaceOrientation)interfaceOrientation
{
	isgl3dAllowedAutoRotations allowedAutoRotations =
    [Isgl3dDirector sharedInstance].allowedAutoRotations;
	if ([Isgl3dDirector sharedInstance].autoRotationStrategy ==
        Isgl3dAutoRotationNone
  )
  {
		return NO;
	
	}
  else if ([Isgl3dDirector sharedInstance].autoRotationStrategy ==
            Isgl3dAutoRotationByIsgl3dDirector)
  {		
		if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft &&
        allowedAutoRotations != Isgl3dAllowedAutoRotationsPortraitOnly
    )
    {
			[Isgl3dDirector sharedInstance].deviceOrientation =
        Isgl3dOrientationLandscapeRight;	
		}
    else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight &&
             allowedAutoRotations != Isgl3dAllowedAutoRotationsPortraitOnly
    )
    {
			[Isgl3dDirector sharedInstance].deviceOrientation =
        Isgl3dOrientationLandscapeLeft;
	
		}
    else if (interfaceOrientation == UIInterfaceOrientationPortrait &&
             allowedAutoRotations != Isgl3dAllowedAutoRotationsLandscapeOnly
    )
    {
			[Isgl3dDirector sharedInstance].deviceOrientation =
        Isgl3dOrientationPortrait;	
		}
    else if (interfaceOrientation == UIDeviceOrientationPortraitUpsideDown &&
             allowedAutoRotations != Isgl3dAllowedAutoRotationsLandscapeOnly
    )
    {
			[Isgl3dDirector sharedInstance].deviceOrientation =
        Isgl3dOrientationPortraitUpsideDown;
		}
	
		// Return true only for portrait
		return  (interfaceOrientation == UIInterfaceOrientationPortrait);

	}
  else if ([Isgl3dDirector sharedInstance].autoRotationStrategy ==
           Isgl3dAutoRotationByUIViewController
  )
  {
		if (UIInterfaceOrientationIsLandscape(interfaceOrientation) &&
        allowedAutoRotations != Isgl3dAllowedAutoRotationsPortraitOnly)
    {
			return YES;			
		}
    else if (UIInterfaceOrientationIsPortrait(interfaceOrientation) &&
             allowedAutoRotations != Isgl3dAllowedAutoRotationsLandscapeOnly
    )
    {
			return YES;			
		}
    else
    {
			return NO;
		}		
	}
  else
  {
		NSLog(
      @"Isgl3dViewController:: ERROR : Unknown auto rotation strategy of Isgl3dDirector."
    );
		return NO;
	}
}

- (void) willRotateToInterfaceOrientation
  :(UIInterfaceOrientation)toInterfaceOrientation
  duration:(NSTimeInterval)duration
{	
	if ([Isgl3dDirector sharedInstance].autoRotationStrategy ==
        Isgl3dAutoRotationByUIViewController)
  {
		CGRect screenRect = [[UIScreen mainScreen] bounds];
		CGRect rect = CGRectZero;
		
		if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
        toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
    {		
			rect = screenRect;		
		}
    else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
             toInterfaceOrientation == UIInterfaceOrientationLandscapeRight
    )
    {
			rect.size =
        CGSizeMake( screenRect.size.height, screenRect.size.width );
		}
		
		UIView * glView =
      [Isgl3dDirector sharedInstance].openGLView;
		float contentScaleFactor =
      [Isgl3dDirector sharedInstance].contentScaleFactor;
		
		if (contentScaleFactor != 1)
    {
			rect.size.width *= contentScaleFactor;
			rect.size.height *= contentScaleFactor;
		}
		glView.frame = rect;
	}	
}

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

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

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

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

@end
