//
//  SlideExplicitAnimations.swift
//  SceneKitSessionWWDC2014
//
//  Created by haharsw on 2019/10/13.
//  Copyright © 2019 haharsw. All rights reserved.
//

import SceneKit
import SpriteKit

class SlideExplicitAnimations: SlideBase {
    
    var animatedNode: SCNNode!
    
    override func numberOfSteps() -> Int {
        return 5
    }
    
    override func setupSlide(slideManager: SlideManager) {
        // Set the slide's title and subtitle and add some text
        let _ = textManager.setTitle("Animations")
        let _ = textManager.setSubtitle("Explicit animations")
        
        let _ = textManager.addCode("// Create an animation \n" +
                                    "animation = [#CABasicAnimation# animationWithKeyPath:@\"rotation\"]; \n\n" +
                                    "// Configure the animation \n" +
                                    "animation.#duration# = 2.0; \n" +
                                    "animation.#toValue# = [NSValue valueWithSCNVector4:SCNVector4Make(0,1,0,M_PI*2)]; \n" +
                                    "animation.#repeatCount# = MAXFLOAT; \n\n" +
                                    "// Play the animation \n" +
                                    "[aNode #addAnimation:#animation #forKey:#@\"myAnimation\"];")
        
        // A simple torus that we will animate to illustrate the code
        animatedNode = SCNNode()
        animatedNode.position = SCNVector3(9, 5.7, 20)
        
        // Use an extra node that we can tilt it and cumulate that with the animation
        let torusNode = SCNNode()
        torusNode.geometry = SCNTorus(ringRadius: 4.0, pipeRadius: 1.5)
        torusNode.rotation = SCNVector4(1, 0, 0, -Float.pi * 0.5)
        torusNode.geometry?.firstMaterial?.diffuse.contents = SKColor.red
        torusNode.geometry?.firstMaterial?.specular.contents = SKColor.white
        torusNode.geometry?.firstMaterial?.reflective.contents = UIImage(named: "envmap")
        torusNode.geometry?.firstMaterial?.fresnelExponent = 0.7
        
        animatedNode.addChildNode(torusNode)
        contentNode.addChildNode(animatedNode)
    }
    
    override func presentStep(index: Int, slideManager: SlideManager) {
        // Animate by default
        SCNTransaction.begin()
        
        switch index {
            case 0:
                // Disable animations for first step
                SCNTransaction.animationDuration = 0
                
                // Initially dim the torus
                animatedNode.opacity = 0.25
                
                textManager.highlightCodeChunks([])
            case 1:
                textManager.highlightCodeChunks([0])
            case 2:
                textManager.highlightCodeChunks([1, 2, 3])
            case 3:
                textManager.highlightCodeChunks([4, 5])
            case 4:
                SCNTransaction.animationDuration = 0
                
                // Show the torus
                animatedNode.opacity = 1.0
                
                // Animate explicitly
                let animation = CABasicAnimation(keyPath: "rotation")
                animation.duration = 2.0
                animation.toValue = SCNVector4Make(0, 1, 0, Float.pi * 2)
                animation.repeatCount = .greatestFiniteMagnitude
                animatedNode.addAnimation(animation, forKey: "myAnimation")
                
                SCNTransaction.begin()
                SCNTransaction.animationDuration = 1.0
                {
                    // Dim the text
                    textManager.textNode.opacity = 0.75
                    
                    slideManager.cameraHandle.position = slideManager.cameraHandle.convertPosition(SCNVector3(9, 8, 20), to: slideManager.cameraHandle.parent)
                    slideManager.cameraPitch.rotation = SCNVector4(1, 0, 0, -Float.pi / 10)
                }()
                SCNTransaction.commit()
        default:
            break
        }
        
        SCNTransaction.commit()
    }
}
