//
//  ViewController.m
//  Mini utrke
//
//  Created by Vedran Murgić on 24.01.2014..
//  Copyright (c) 2014. Vedran Murgić. All rights reserved.
//

#import "ViewController.h"
#import "Sprite.h"
#import <tgmath.h>

#define VISINA 1536
#define SIRINA 2048

typedef struct{
    GLKVector3 positionCoordinates;
    GLKVector2 textureCoordinates;
} VertexData;

float pozicija = 0;
float sirinaCeste = 0.0f;
float pomak = 0;
float brzinaRotacije;
float rotacija;
float pomakCeste = 0.0;
float pomakOznaka = 0.0;
float zonaFaktor = 0.0;

int mls1=0,mls2=0,mls3=0,sec1=0,sec2=0,min1=0,min2=0;

BOOL kreni = NO;

int semafor = 1;

int att = 0;
int med = 0;
float zona = 0.0;
int blink = 0;
float oznakeVisina = 305.0f;
float offset0m = 700.0f;

NSTimeInterval timeInMiliseconds = 0;
NSTimeInterval trenutnoVrijeme;
BOOL crtajBlink = NO;

VertexData vertices[] = {
    //pozadina
    {{0.0f, 0.0f, -0.1f}, {0.0f, 0.0f}},
    {{2362.0f, 0.0f, -0.1f}, {1.0f, 0.0f}},
    {{0.0f, 1310.0f, -0.1f}, {0.0f, 1.0f}},
    {{2362.0f, 0.0f, -0.1f}, {1.0f, 0.0f}},
    {{0.0f, 1310.0f, -0.1f}, {0.0f, 1.0f}},
    {{2362.0f, 1310.0f, -0.1f}, {1.0f, 1.0f}},
};

@interface ViewController ()

@property (nonatomic, strong) EAGLContext *context;
@property (nonatomic, strong) GLKBaseEffect *baseEffect;
@property (nonatomic, strong) Sprite *mini;
@property (nonatomic, strong) Sprite *back;
@property (nonatomic, strong) Sprite *kotac;
@property (nonatomic, strong) Sprite *cesta;
@property (nonatomic, strong) Sprite *oznake;
@property (nonatomic, strong) Sprite *oznake50;
@property (nonatomic, strong) Sprite *oznake100;
@property (nonatomic, strong) Sprite *oznake150;
@property (nonatomic, strong) Sprite *oznake200;
@property (nonatomic, strong) Sprite *oznake250;
@property (nonatomic, strong) Sprite *oznake300;
@property (nonatomic, strong) Sprite *oznake350;
@property (nonatomic, strong) Sprite *oznake402;
@property (nonatomic, strong) Sprite *semafor;
@property (nonatomic, strong) Sprite *cuka;
@property (nonatomic, strong) Sprite *kazaljka;
@property (nonatomic, strong) Sprite *brojevi;

@end

@implementation ViewController{
    GLuint _vertexBufferID;
    GLuint _depthRenderbuffer;
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    if([[TGAccessoryManager sharedTGAccessoryManager] accessory] != nil){
        [[TGAccessoryManager sharedTGAccessoryManager] startStream];
        NSLog(@"startaoStream");
        NSLog(@"TGAccessory version: %d", [[TGAccessoryManager sharedTGAccessoryManager] getVersion]);

    }
    
    NSLog(@"Ide li stream: %hhd", [[TGAccessoryManager sharedTGAccessoryManager] connected]);
	
    NSLog(@"sad sam u viewDidLoad");
    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    
    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    [EAGLContext setCurrentContext:self.context];
    
    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.useConstantColor = YES;
    //self.baseEffect.constantColor = GLKVector4Make(boja/255.0f, 0.0f, 0.0f, 1.0f);
    
    //iPad rezolucija --> 2048pxx1536px
    self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0, SIRINA, 0, VISINA, 10.0f, -10.0f);
    
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    
    glGenBuffers(1, &_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), offsetof(VertexData, positionCoordinates));
    
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (GLvoid *) offsetof(VertexData, textureCoordinates));
    
    glEnable(GL_BLEND);
    //glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(upravljajDodirom:)];
    [self.view addGestureRecognizer:gestureRecognizer];
    
    CGImageRef imageReferenceMini = [[UIImage imageNamed:@"mini.png"] CGImage];
    
    GLKTextureInfo *textureInfoMini = [GLKTextureLoader textureWithCGImage:imageReferenceMini
                                                               options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                 error:NULL];
    //cesta
    CGImageRef imageReferenceCesta = [[UIImage imageNamed:@"cesta.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoCesta = [GLKTextureLoader textureWithCGImage:imageReferenceCesta
                                                                   options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                     error:NULL];
    
    //oznake
    CGImageRef textureReferenceOznake = [[UIImage imageNamed:@"0m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake = [GLKTextureLoader textureWithCGImage:textureReferenceOznake
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake50 = [[UIImage imageNamed:@"50m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake50 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake50
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake100 = [[UIImage imageNamed:@"100m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake100 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake100
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake150 = [[UIImage imageNamed:@"150m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake150 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake150
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake200 = [[UIImage imageNamed:@"200m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake200 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake200
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake250 = [[UIImage imageNamed:@"250m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake250 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake250
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake300 = [[UIImage imageNamed:@"300m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake300 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake300
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake350 = [[UIImage imageNamed:@"350m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake350 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake350
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    CGImageRef textureReferenceOznake402 = [[UIImage imageNamed:@"402m.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoOznake402 = [GLKTextureLoader textureWithCGImage:textureReferenceOznake402
                                                                     options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                       error:NULL];
    
    
    //kotac
    CGImageRef textureReferenceKotac = [[UIImage imageNamed:@"kotac.png"] CGImage];
    
    GLKTextureInfo *textureInfoKotac = [GLKTextureLoader textureWithCGImage:textureReferenceKotac
                                                                         options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                           error:NULL];
    
    //back
    CGImageRef imageReferenceBack = [[UIImage imageNamed:@"utrkeBack.jpg"] CGImage];
    
    GLKTextureInfo *textureInfoBack = [GLKTextureLoader textureWithCGImage:imageReferenceBack
                                                               options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                 error:NULL];
    //semafor
    CGImageRef imageReferenceSemafor = [[UIImage imageNamed:@"semafor.png"] CGImage];
    
    GLKTextureInfo *textureInfoSemafor = [GLKTextureLoader textureWithCGImage:imageReferenceSemafor
                                                                   options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                     error:NULL];
    //cuka
    CGImageRef imageReferenceCuka = [[UIImage imageNamed:@"cuka.png"] CGImage];
    
    GLKTextureInfo *textureInfoCuka = [GLKTextureLoader textureWithCGImage:imageReferenceCuka
                                                                      options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                        error:NULL];

    //Kazaljka
    CGImageRef imageReferenceKazaljka = [[UIImage imageNamed:@"kazaljka.png"] CGImage];
    
    GLKTextureInfo *textureInfoKazaljka = [GLKTextureLoader textureWithCGImage:imageReferenceKazaljka
                                                                      options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                        error:NULL];
    //Brojevi
    CGImageRef imageReferenceBrojevi = [[UIImage imageNamed:@"brojevi.png"] CGImage];
    
    GLKTextureInfo *textureInfoBrojevi = [GLKTextureLoader textureWithCGImage:imageReferenceBrojevi
                                                                       options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:GLKTextureLoaderOriginBottomLeft]
                                                                         error:NULL];

    
    self.back =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.back.textureInfo = textureInfoBack;
    self.back.position = GLKVector2Make(0, 350);
    
    self.mini = [[Sprite alloc] initWithEffect:self.baseEffect];
    self.mini.textureInfo = textureInfoMini;
    self.mini.position = GLKVector2Make(100,100);
    
    self.cesta =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.cesta.textureInfo = textureInfoCesta;
    self.cesta.position = GLKVector2Make(0, 0);
    
    self.kotac =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.kotac.textureInfo = textureInfoKotac;
    self.kotac.position = GLKVector2Make(120, 120);


    self.oznake =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake.textureInfo = textureInfoOznake;
    self.oznake.position = GLKVector2Make(offset0m, oznakeVisina);
    self.oznake50 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake50.textureInfo = textureInfoOznake50;

    self.oznake100 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake100.textureInfo = textureInfoOznake100;

    self.oznake150 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake150.textureInfo = textureInfoOznake150;

    self.oznake200 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake200.textureInfo = textureInfoOznake200;

    self.oznake250 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake250.textureInfo = textureInfoOznake250;

    self.oznake300 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake300.textureInfo = textureInfoOznake300;

    self.oznake350 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake350.textureInfo = textureInfoOznake350;

    self.oznake402 =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.oznake402.textureInfo = textureInfoOznake402;
    
    self.semafor =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.semafor.textureInfo = textureInfoSemafor;
    self.semafor.position = GLKVector2Make(700, 600);
    
    self.cuka =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.cuka.textureInfo = textureInfoCuka;
    self.cuka.position = GLKVector2Make(100, 500);
    self.kazaljka =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.kazaljka.textureInfo = textureInfoKazaljka;
    self.kazaljka.position = GLKVector2Make(100, 500);
    
    self.brojevi =[[Sprite alloc] initWithEffect:self.baseEffect];
    self.brojevi.textureInfo = textureInfoBrojevi;
    self.brojevi.position = GLKVector2Make(1100, 100);
    
    sirinaCeste = 2048 / 400;
    
    //self.mozak.rotation = 90;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    NSLog(@"Nestajem");
    if([[TGAccessoryManager sharedTGAccessoryManager] connected]) [[TGAccessoryManager sharedTGAccessoryManager] startStream];
}

#pragma mark - GLKView delegate methods

- (void) glkView:(GLKView *)view drawInRect:(CGRect)rect{
    //NSLog(@"sad sam u glkView %f", boja);
    
    if([[TGAccessoryManager sharedTGAccessoryManager] accessory] == nil){
        //NSLog(@"Nije spojen");
    }
    else {
        //NSLog(@"SPOJEN JE!!!!!!!!!!");
        //att = eSenseValues.meditation;
        //NSLog(@"att: %d", att);
    }
    
        
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    //NSLog(@"mozakTrenutno: %f %f",mozakXtrenutno, mozakYtrenutno);
    //NSLog(@"mx: %f   my: %f", mx, my);
    //self.mozak.position = GLKVector2Make(mx, my);
    
    if(([[NSDate date] timeIntervalSince1970] - timeInMiliseconds) < 1){
        crtajBlink = YES;
        //NSLog(@"crtamBlink: %f", [[NSDate date] timeIntervalSince1970] - timeInMiliseconds);
    }else{
        crtajBlink = NO;
        //NSLog(@"NE crtamBlink: %f", [[NSDate date] timeIntervalSince1970] - timeInMiliseconds);
    }
    
    pomakCeste = fmodf( pozicija, 2048.0f ) / 5000.0f;
    if(pomakCeste == 0.0){
        pomakOznaka += pomak;
    }
    self.oznake.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 0), oznakeVisina);
    self.oznake50.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 1), oznakeVisina);
    self.oznake100.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 2), oznakeVisina);
    self.oznake150.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 3), oznakeVisina);
    self.oznake200.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 4), oznakeVisina);
    self.oznake250.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 5), oznakeVisina);
    self.oznake300.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 6), oznakeVisina);
    self.oznake350.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 7), oznakeVisina);
    self.oznake402.position = GLKVector2Make(offset0m - pozicija + (1500.0 * 8), oznakeVisina);
    
    //NSLog(@"402: %f", self.oznake402.position.x);
    
    [self.back render:@"back"];
    [self.cesta render:@"cesta"];
    [self.oznake render:@"oznake"];
    [self.oznake50 render:@"oznake"];
    [self.oznake100 render:@"oznake"];
    [self.oznake150 render:@"oznake"];
    [self.oznake200 render:@"oznake"];
    [self.oznake250 render:@"oznake"];
    [self.oznake300 render:@"oznake"];
    [self.oznake350 render:@"oznake"];
    [self.oznake402 render:@"oznake"];
    switch (semafor) {
        case 1:
            [self.semafor render:@"semaforCrveno"];
            break;
        case 2:
            [self.semafor render:@"semaforZuto"];
            break;
        case 3:
            [self.semafor render:@"semaforZeleno"];
            break;
        default:
            break;
    }
    
    self.kazaljka.rotation = -(zonaFaktor * 100.0);
    [self.cuka render:@"cuka"];
    [self.kazaljka render:@"kazaljka"];
    
    [self.mini render:@"mini"];
    self.kotac.rotation = rotacija;
    self.kotac.position = GLKVector2Make(209, 163);
    [self.kotac render:@"kotac"];
    self.kotac.position = GLKVector2Make(598, 163);
    [self.kotac render:@"kotac"];
    
    if(timeInMiliseconds != 0 && (pozicija < 12100.0)){
        trenutnoVrijeme = [[NSDate date] timeIntervalSince1970] - timeInMiliseconds;
        NSLog(@"trenutno - %f", trenutnoVrijeme);
        long milis = trenutnoVrijeme*1000.0f;
        int mls = (int)fmodf( milis, 1000 );
        mls3 = mls % 10;
        mls2 = (mls / 10) % 10;
        mls1 = (mls / 100);
        NSLog(@"%d %d %d",mls1, mls2, mls3);
        int sec = (int)fmodf( milis / 1000, 60 );
        sec2 = sec % 10;
        sec1 = (sec / 10) % 10;
        int min = (int)fmodf( milis / 1000 / 60, 60 );
        min2 = min % 10;
        min1 = (min / 10) % 10;
    }
    
    self.brojevi.position = GLKVector2Make(1100, 100);
    [self.brojevi render:@"min"];
    self.brojevi.position = GLKVector2Make(1200, 100);
    [self.brojevi render:@"min2"];
    self.brojevi.position = GLKVector2Make(1300, 100);
    [self.brojevi render:@"razmak"];
    self.brojevi.position = GLKVector2Make(1400, 100);
    [self.brojevi render:@"sec"];
    self.brojevi.position = GLKVector2Make(1500, 100);
    [self.brojevi render:@"sec2"];
    self.brojevi.position = GLKVector2Make(1600, 100);
    [self.brojevi render:@"razmak"];
    self.brojevi.position = GLKVector2Make(1700, 100);
    [self.brojevi render:@"mili"];
    self.brojevi.position = GLKVector2Make(1800, 100);
    [self.brojevi render:@"mili2"];
    self.brojevi.position = GLKVector2Make(1900, 100);
    [self.brojevi render:@"mili3"];

    float sirinaBroja = 1.0f / 11.0f;

    NSLog(@"mili1 %f", (float)mls1 + 0.1f);
    
    VertexData mozak[] = {
        //pozadina
        {{0.0f, 0.0f, -0.1f}, {0.0f, 0.0f}},
        {{2362.0f, 0.0f, -0.1f}, {1.0f, 0.0f}},
        {{0.0f, 1310.0f, -0.1f}, {0.0f, 1.0f}},
        {{2362.0f, 0.0f, -0.1f}, {1.0f, 0.0f}},
        {{0.0f, 1310.0f, -0.1f}, {0.0f, 1.0f}},
        {{2362.0f, 1310.0f, -0.1f}, {1.0f, 1.0f}},
        
        //cesta
        {{0.0f, 0.0f, -0.1f}, {pomakCeste, 0.0f}},
        {{2520.0f, 0.0f, -0.1f}, {0.5f + pomakCeste, 0.0f}},
        {{0.0f, 400.0f, -0.1f}, {pomakCeste, 1.0f}},
        {{2520.0f, 0.0f, -0.1f}, {0.5f + pomakCeste, 0.0f}},
        {{0.0f, 400.0f, -0.1f}, {pomakCeste, 1.0f}},
        {{2520.0f, 400.0f, -0.1f}, {0.5f + pomakCeste, 1.0f}},
        
        //oznake
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{100.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 40.0f, -3.0f}, {0.0f, 1.0f}},
        {{0.0f, 40.0f, -3.0f}, {0.0f, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{100.0f, 40.0f, -3.0f}, {1.0f, 1.0f}},
        
        //mini
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{0.0f, 143.0f, -3.0f}, {0.0f, 1.0f}},
        {{300.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 143.0f, -3.0f}, {0.0f, 1.0f}},
        {{300.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{300.0f, 143.0f, -3.0f}, {1.0f, 1.0f}},
        
        //attBack
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{38.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 38.0f, -3.0f}, {0.0f, 1.0f}},
        {{0.0f, 38.0f, -3.0f}, {0.0f, 1.0f}},
        {{38.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{38.0f, 38.0f, -3.0f}, {1.0f, 1.0f}},
        
        //semaforCrveno
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.0f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {0.33f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.0f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {0.33f, 0.0f}},
        {{110.0f, 310.0f, -3.0f}, {0.33f, 1.0f}},
        
        //semaforZuto
        {{0.0f, 0.0f, -3.0f}, {0.34f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.34f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {0.67f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.34f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {0.67f, 0.0f}},
        {{110.0f, 310.0f, -3.0f}, {0.67f, 1.0f}},
        
        //semaforZeleno
        {{0.0f, 0.0f, -3.0f}, {0.66f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.68f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 310.0f, -3.0f}, {0.68f, 1.0f}},
        {{110.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{110.0f, 310.0f, -3.0f}, {1.0f, 1.0f}},
        
        //cuka
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{0.0f, 500.0f, -3.0f}, {0.0f, 1.0f}},
        {{500.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 500.0f, -3.0f}, {0.0f, 1.0f}},
        {{500.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{500.0f, 500.0f, -3.0f}, {1.0f, 1.0f}},
        
        //kazaljka
        {{0.0f, 0.0f, -3.0f}, {0.0f, 0.0f}},
        {{0.0f, 500.0f, -3.0f}, {0.0f, 1.0f}},
        {{500.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{0.0f, 500.0f, -3.0f}, {0.0f, 1.0f}},
        {{500.0f, 0.0f, -3.0f}, {1.0f, 0.0f}},
        {{500.0f, 500.0f, -3.0f}, {1.0f, 1.0f}},
        
        //razmak 60
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * 10.0f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * 10.0f, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * 10.0f + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * 10.0f, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * 10.0f + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * 10.0f + 0.1f, 1.0f}},
        
        //min 66
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min1, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min1 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min1 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min1 + 0.1f, 1.0f}},
        //72
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min2, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min2 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)min2 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)min2 + 0.1f, 1.0f}},
        
        //sec 78
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec1, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec1 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec1 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec1 + 0.1f, 1.0f}},
        //84
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec2, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec2 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)sec2 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)sec2 + 0.1f, 1.0f}},
        
        //mili 90
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls1, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls1 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls1, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls1 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls1 + 0.1f, 1.0f}},
        //96
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls2, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls2 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls2, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls2 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls2 + 0.1f, 1.0f}},
        //102
        {{0.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls3, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls3, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls3 + 0.1f, 0.0f}},
        {{0.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls3, 1.0f}},
        {{100.0f, 0.0f, -3.0f}, {sirinaBroja * (float)mls3 + 0.1f, 0.0f}},
        {{100.0f, 100.0f, -3.0f}, {sirinaBroja * (float)mls3 + 0.1f, 1.0f}}
        
    };
    
    //NSLog(@"attV: %d %f", att, attV);
    
    if((pozicija < 12100.0) && kreni){
        zonaFaktor += (zona - zonaFaktor) / 10.0f;
        pomak += 20.0 * zonaFaktor;
        pozicija += (zonaFaktor * 750.0f) / 30.0f;
        //NSLog(@"pozicija %f", pozicija);
        brzinaRotacije = 20.0 * zonaFaktor;
        rotacija += -brzinaRotacije;
    }else{
        zonaFaktor = 0;
    }
    
    glBufferData(GL_ARRAY_BUFFER, sizeof(mozak), mozak, GL_DYNAMIC_DRAW);
}

- (void) update{
    //NSLog(@"sad sam u update");
    
}

#pragma mark - ns metode
- (void)dataReceived:(NSDictionary *)data {
    /*NSLog(@"Data received!");
    NSLog(@"Raw: %d", [[data valueForKey:@"raw"] intValue]);
    NSLog(@"Attention: %d", [[data valueForKey:@"eSenseAttention"] intValue]);*/
    //NSLog(@"Čisti ulaz: %@", data);
    if([data valueForKey:@"eSenseAttention"]){
        att = [[data valueForKey:@"eSenseAttention"] intValue];
        med =[[data valueForKey:@"eSenseMeditation"] intValue];
        zona = ((float)(att + med) / 2.0f) / 100.0f;
        NSLog(@"Zona: %f", zona);
        //NSLog(@"poorSignal: %d", [[data valueForKey:@"poorSignal"] intValue]);
        if([[data valueForKey:@"poorSignal"] intValue] >= 50){
            semafor = 1;
        }else if(([[data valueForKey:@"poorSignal"] intValue] < 50) && ([[data valueForKey:@"poorSignal"] intValue] > 0)){
            semafor = 2;
        }else{
            semafor = 3;
        }
    }
    if([data valueForKey:@"blinkStrength"]){
        blinkStrength = [[data valueForKey:@"blinkStrength"] intValue];
        if(semafor == 3){
            kreni = YES;
        }
        if (timeInMiliseconds == 0){
            timeInMiliseconds = [[NSDate date] timeIntervalSince1970];
        }
        /*if(pozicija >= 12100.0){
            pozicija = 0;
            timeInMiliseconds = [[NSDate date] timeIntervalSince1970];
        }*/
    }
    //NSLog(@"Blink: %d", blinkStrength);
}

- (void)accessoryDidConnect:(EAAccessory *)accessory {
    NSLog(@"%@ was connected to this device.", [accessory name]);
    UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"MindWave se spojio!"
                                                 message:[NSString stringWithFormat:@"ThinkGear dodatak %@ se spojio na uređaj.", [accessory name]]
                                                delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
    [a show];

    [[TGAccessoryManager sharedTGAccessoryManager] startStream];
}
- (void)accessoryDidDisconnect {
    NSLog(@"An accessory was disconnected.");
    UIAlertView * a = [[UIAlertView alloc] initWithTitle:@"MindWave se odspojio"
                                                 message:[NSString stringWithFormat:@"ThinkGear dodatak se odspojio."]
                                                delegate:nil
                                       cancelButtonTitle:@"OK"
                                       otherButtonTitles:nil];
    [a show];
}

#pragma mark - dodir
-(void) upravljajDodirom:(UITapGestureRecognizer *)gestureRecognizer{
    NSLog(@"dodir");
    if([[TGAccessoryManager sharedTGAccessoryManager] accessory] == nil){
        timeInMiliseconds = [[NSDate date] timeIntervalSince1970];
    }
    if(pozicija >= 12100.0){
        pozicija = 0;
        timeInMiliseconds = [[NSDate date] timeIntervalSince1970];
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //CGPoint tocka = [[touches anyObject] locationInView:self.view];
    //NSLog(@"x: %f  y: %f", tocka.x, tocka.y);
    /*if([[TGAccessoryManager sharedTGAccessoryManager] accessory] == nil){
        att = 100.0 - (tocka.y / 760.5) * 100;
        med = (tocka.x / 1022.5) * 100;
    }*/
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    CGPoint tocka = [[touches anyObject] locationInView:self.view];
    //NSLog(@"x: %f  y: %f", tocka.x, tocka.y);
    if([[TGAccessoryManager sharedTGAccessoryManager] accessory] == nil){
        att = 100.0 - (tocka.y / 760.5) * 100;
        med = (tocka.x / 1022.5) * 100;
    }
}

@end
