/**
 *
 *  You can modify and use this source freely
 *  only for the development of application related Live2D.
 *
 *  (c) Live2D Inc. All rights reserved.
 */

#import "Live2DSampleViewController.h"
#include "Live2DModelOpenGL.h"
#include "DrawProfileUnity.h"
#include "UtSystem.h"
#include <vector>


using namespace live2d;

@interface Live2DSampleViewController ()

@property (strong, nonatomic) EAGLContext *context;


- (void)setupGL;
- (void)tearDownGL;

@end

@implementation Live2DSampleViewController
{
	Live2DModelOpenGL* live2DModel;
}

- (void)dealloc
{
    [self tearDownGL];
    
    if ([EAGLContext currentContext] == self.context) {
        [EAGLContext setCurrentContext:nil];
    }
    
    [_context release];
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease];
	
    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
    
    [self setupGL];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
	
    if ([self isViewLoaded] && ([[self view] window] == nil)) {
        self.view = nil;
        
        [self tearDownGL];
        
        if ([EAGLContext currentContext] == self.context) {
            [EAGLContext setCurrentContext:nil];
        }
        self.context = nil;
    }
	
    // Dispose of any resources that can be recreated.
}

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];
	
	
	Live2D::init() ;//Live2Dの初期化。一度だけ呼んでください
	
	
	NSString* MODEL_FILE = @"haru" ;
	NSString* TEXTURE_PATH[] ={
		@"texture_00" ,
		@"texture_01" ,
		@"texture_02" ,
		NULL
	} ;
	
	NSString *modelpath = [[NSBundle mainBundle] pathForResource:MODEL_FILE ofType:@"moc"];
	
	live2DModel = live2d::Live2DModelOpenGL::loadModel( [modelpath UTF8String] ) ;
	
	for( int i = 0 ; TEXTURE_PATH[i] != NULL ; i++ ){
		NSString* filePath = [[NSBundle mainBundle] pathForResource:TEXTURE_PATH[i] ofType:@"png"];
		GLKTextureInfo* textureInfo =
		[GLKTextureLoader textureWithContentsOfFile:filePath
											options:[NSDictionary dictionaryWithObjectsAndKeys:
													 [NSNumber numberWithBool:YES] ,GLKTextureLoaderApplyPremultiplication,
													 [NSNumber numberWithBool:YES] ,GLKTextureLoaderGenerateMipmaps,nil ]
											  error:nil];
		int glTexNo = [textureInfo name] ;
		
		//テクスチャとモデルを結びつける
		//テクスチャはLive2D側では解放しないので、個別に解放してください
		live2DModel->setTexture( i , glTexNo ) ;
	}
}

- (void)tearDownGL
{
	if(live2DModel)delete live2DModel;
	Live2D::dispose();//Live2Dの解放。モデルなどは個別に解放してください
    [EAGLContext setCurrentContext:self.context];
}


- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
	CGSize screen = [[UIScreen mainScreen] bounds].size;
	
	
	
	float scx = 2.0 / live2DModel->getCanvasWidth() ;
	float scy = -2.0 / live2DModel->getCanvasWidth() *(screen.width/screen.height);
	float x = -1 ;
	float y = 1 ;
	float matrix []= {
		scx , 0 , 0 , 0 ,
		0 , scy ,0 , 0 ,
		0 , 0 , 1 , 0 ,
		x , y , 0 , 1
	} ;
	
	
	double t = (live2d::UtSystem::getUserTimeMSec()/1000.0) * 2 * M_PI ;
	live2DModel->setParamFloat("PARAM_ANGLE_X", (float)(30 * sin( t/3.0 )) );
	
	live2DModel->setMatrix(matrix) ;
	live2DModel->update() ;
	live2DModel->draw() ;
	
	
    
}
@end
