//
//  Sprite.m
//  SimpleGLESgame
//
//  Created by Vedran Murgić on 25.01.2014..
//  Copyright (c) 2014. Vedran Murgić. All rights reserved.
//

#import "Sprite.h"

@interface Sprite()

@property (nonatomic, weak) GLKBaseEffect *baseEffect;

@end

@implementation Sprite

-(id) initWithEffect:(GLKBaseEffect *)baseEffect
{
    if((self = [super init])){
        self.baseEffect = baseEffect;
    }
    return self;
}

-(void) render:(NSString *) s{
    
    self.baseEffect.texture2d0.name = self.textureInfo.name;
    self.baseEffect.texture2d0.target = self.textureInfo.target;
    
    GLKMatrix4 modelviewMatrix = GLKMatrix4Translate(GLKMatrix4Identity,self.position.x, self.position.y, 0);
    if([s isEqualToString:@"mini"] || [s isEqualToString:@"kotac"]){
        modelviewMatrix = GLKMatrix4Scale(modelviewMatrix, 2.0f, 2.0f, 0.0f);
    }
    if([s isEqualToString:@"kazaljka"]){
        modelviewMatrix = GLKMatrix4Translate(modelviewMatrix,250.0f, 250.0f, 0);
    }
    modelviewMatrix = GLKMatrix4Rotate(modelviewMatrix,GLKMathDegreesToRadians(self.rotation), 0.0f, 0.0f, 1.0f);
    if([s isEqualToString:@"kotac"]){
        modelviewMatrix = GLKMatrix4Translate(modelviewMatrix,-19.0f, -19.0f, 0);
    }
    if([s isEqualToString:@"kazaljka"]){
        modelviewMatrix = GLKMatrix4Translate(modelviewMatrix,-250.0f, -250.0f, 0);
    }
    self.baseEffect.transform.modelviewMatrix = modelviewMatrix;
    
    if([s isEqualToString:@"att"]){
        self.baseEffect.constantColor = GLKVector4Make(20.0f/255.0f, 133.0f/255.0f, 204.0f/255.0f, 1.0f);
    }else if([s isEqualToString:@"med"]){
        self.baseEffect.constantColor = GLKVector4Make(255.0f/255.0f, 252.0f/255.0f, 25.0f/255.0f, 1.0f);
    }else if([s isEqualToString:@"blink1"]){
        self.baseEffect.constantColor = GLKVector4Make(1.0f, 0.0f, 0.0f, 1.0f);
    }

    //self.baseEffect.constantColor = GLKVector4Make(1.0f, 0.0f, 0.0f, 1.0f);
    [self.baseEffect prepareToDraw];
    glEnable(GL_TEXTURE_2D);
    
    //NSLog(@"%@", s);
    if([s isEqualToString:@"back"]){
        glDrawArrays(GL_TRIANGLES, 0, 6);
    }else if([s isEqualToString:@"cesta"]){
        glDrawArrays(GL_TRIANGLES,6, 6);
    }else if([s isEqualToString:@"oznake"]){
        glDrawArrays(GL_TRIANGLES,12, 6);
    }else if([s isEqualToString:@"mini"]){
        glDrawArrays(GL_TRIANGLES,18, 6);
    }else if([s isEqualToString:@"kotac"]){
        glDrawArrays(GL_TRIANGLES,24, 6);
    }else if([s isEqualToString:@"semaforCrveno"]){
        glDrawArrays(GL_TRIANGLES,30, 6);
    }else if([s isEqualToString:@"semaforZuto"]){
        glDrawArrays(GL_TRIANGLES,36, 6);
    }else if([s isEqualToString:@"semaforZeleno"]){
        glDrawArrays(GL_TRIANGLES,42, 6);
    }else if([s isEqualToString:@"cuka"]){
        glDrawArrays(GL_TRIANGLES,48, 6);
    }else if([s isEqualToString:@"kazaljka"]){
        glDrawArrays(GL_TRIANGLES,54, 6);
    }else if([s isEqualToString:@"min"]){
        glDrawArrays(GL_TRIANGLES,66, 6);
    }else if([s isEqualToString:@"min2"]){
        glDrawArrays(GL_TRIANGLES,72, 6);
    }else if([s isEqualToString:@"razmak"]){
        glDrawArrays(GL_TRIANGLES,60, 6);
    }else if([s isEqualToString:@"sec"]){
        glDrawArrays(GL_TRIANGLES,78, 6);
    }else if([s isEqualToString:@"sec2"]){
        glDrawArrays(GL_TRIANGLES,84, 6);
    }else if([s isEqualToString:@"razmak"]){
        glDrawArrays(GL_TRIANGLES,60, 6);
    }else if([s isEqualToString:@"mili"]){
        glDrawArrays(GL_TRIANGLES,90, 6);
    }else if([s isEqualToString:@"mili2"]){
        glDrawArrays(GL_TRIANGLES,96, 6);
    }else if([s isEqualToString:@"mili3"]){
        glDrawArrays(GL_TRIANGLES,102, 6);
    }
    
    self.baseEffect.constantColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
}

-(void) update{
    
}
@end
