//
//  MLBase3DView.m
//  CodeFactory
//
//  Created by mainvolume on 4/23/15.
//  Copyright (c) 2015 mainvolume. All rights reserved.
//

#import "MLBase3DView.h"
#import "MLBase3DScene.h"


#pragma mark - Private Header
@interface MLBase3DView(){
     SCNNode *ballNode;
    SCNScene *pitchScene;
    
}
@property (nonatomic, strong) MLBase3DScene *baseScene;
@end

#pragma mark - Implementation
@implementation MLBase3DView

#pragma mark - Instantiations
-(MLBase3DScene *)baseScene{
    if (!_baseScene) {
        // Create and configure the scene.
        _baseScene = [MLBase3DScene scene];
       
    }
    return _baseScene;
}

#pragma mark - Initialization

-(void)addBallToNode:(SCNNode *)node{
    //ball
    SCNSphere *ball = [SCNSphere sphereWithRadius:0.2];
    ballNode = [SCNNode nodeWithGeometry:ball];
    ballNode.position = SCNVector3Make(0, 5, 0);
    // Floor Physics-
    ballNode.castsShadow = true;
    ballNode.categoryBitMask = 1;
    
    //ball physics
    SCNPhysicsShape *ballShape = [SCNPhysicsShape shapeWithGeometry:ball options:nil];//
    SCNPhysicsBody *ballBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:ballShape];
    
    ballNode.physicsBody = ballBody;
    ball.firstMaterial.doubleSided = false;
    ball.firstMaterial.diffuse.contents = [UIImage  imageNamed:@"1brazuca"];
    ball.firstMaterial.lightingModelName = SCNLightingModelBlinn;
    ballNode.physicsBody.mass= 1;
    [node addChildNode:ballNode];
}



-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame])
    {
        
        pitchScene = [SCNScene sceneNamed:@"Soccer Arena.dae"];
        BOOL shadows = YES;
        pitchScene.rootNode.castsShadow = shadows;
        
        //[sceneObject.rootNode addChildNode:[self cameraWithLightsAndShadows]];
        //[sceneObject.rootNode addChildNode:[self footballPitch]];
        
        //add ball
        [self addBallToNode:[pitchScene.rootNode childNodeWithName:@"Field" recursively:NO]];
        
        self.scene = pitchScene;
        self.backgroundColor = [UIColor blueColor];
        self.overlaySKScene = [SKScene sceneWithSize:self.frame.size];
        SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Menlo"];
        myLabel.text = @"Building 3d";
        myLabel.fontSize = 40;
        myLabel.fontColor = [UIColor yellowColor];
        myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
        [self.overlaySKScene addChild:myLabel];
        
    }
    return self;
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    int r = arc4random() % 8;
    int x = arc4random() % 8;
    [ballNode.physicsBody applyForce:SCNVector3Make(r - 3, x, r-3) impulse:YES];
}

#pragma mark - Update Functions
-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    // torusNode.position = SCNVector3Make(torusNode.position.x + 1,torusNode.position.y +1, 0);
    // NSLog(@"Position: )
    int s = !3;
    NSLog(@"ssss : %i", s);
    
    //check bounds
    if (ballNode.presentationNode.position.x > 21.0 ||
        ballNode.presentationNode.position.x < -21.0||
        ballNode.presentationNode.position.z > 15.0 ||
        ballNode.presentationNode.position.z < -15.0 ||
        ballNode.presentationNode.position.y < 0.0) {
        
        [ballNode removeFromParentNode];
        [self addBallToNode:[pitchScene.rootNode childNodeWithName:@"Field" recursively:NO]];
    }
    //go NSLog(@"Ball Position: x: %f y: %f z: %f",ballNode.position.x, ballNode.position.y, ballNode.position.z);
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
