//
//  SlideIntroduction.swift
//  SceneKitSessionWWDC2014
//
//  Created by haharsw on 2019/10/9.
//  Copyright © 2019 haharsw. All rights reserved.
//

import SceneKit
import SpriteKit

class SlideIntroduction: SlideBase {
    
    var boxes: Array<SCNNode> = []
    var icon1: SCNNode? = nil
    var icon2: SCNNode? = nil
    
    override func numberOfSteps() -> Int {
        return 3
    }
    
    override func setupSlide(slideManager: SlideManager) {
        
        // Set the slide's title and subtitle and add some text.
        let _ = textManager.setTitle("SceneKit")
        let _ = textManager.setSubtitle("Introduction")
        let _ = textManager.addBullet("High level API for 3D integration", at: 0)
        let _ = textManager.addBullet("Data visualization", at: 1)
        let _ = textManager.addBullet("User interface", at: 1)
        let _ = textManager.addBullet("Casual games", at: 1)
        
        // Build the Cocoa graphics stack
        let redColor = SKColor(red: 168/255.0, green: 21/255.0, blue: 1/255.0, alpha: 1)
        let grayColor = SKColor.gray
        let greenColor = SKColor(red: 105/255.0, green: 145/255.0, blue: 14/255.0, alpha: 1)
        let orangeColor = SKColor.orange
        let purpleColor = SKColor(red: 152/255.0, green: 57/255.0, blue: 189/255.0, alpha: 1)
        
        addBoxWithTitle("Cocoa", frame: CGRect(x: 0, y: 0, width: 500, height: 70), level: 3, color:grayColor)
        addBoxWithTitle("Core Image", frame: CGRect(x: 0, y: 0, width: 100, height: 70), level: 2, color:greenColor)
        addBoxWithTitle("Core Animation", frame: CGRect(x: 390, y: 0, width: 110, height: 70), level: 2, color:greenColor)
        addBoxWithTitle("SpriteKit", frame: CGRect(x: 250, y: 0, width: 135, height: 70), level: 2, color:greenColor)
        addBoxWithTitle("SceneKit", frame: CGRect(x: 105, y: 0, width: 140, height: 70), level: 2, color:orangeColor)
        addBoxWithTitle("OpenGL/OpenGL ES", frame: CGRect(x: 0, y: 0, width: 500, height: 70), level: 1, color:purpleColor)
        addBoxWithTitle("Graphics Hardware", frame: CGRect(x: 0, y: 0, width: 500, height: 70), level: 0, color:redColor)
    }
    
    override func presentStep(index: Int, slideManager: SlideManager) {
    
        switch index {
        case 0: break
        case 1:
            textManager.flipOutTextOfType(.bullet)
            let _ = textManager.addBullet("Available on OS X 10.8+ and iOS 8.0", at: 0)
            textManager.flipInTextOfType(.bullet)
        
            //show some nice icons
            icon1 = SCNNode.planeNodeWithImageName("Badge_X.png", size: 7.5, isLit: false)
            icon1?.position = SCNVector3(-20, 3.5, 5)
            groundNode.addChildNode(icon1!)

            icon2 = SCNNode.planeNodeWithImageName("Badge_iOS.png", size: 7, isLit: false)
            icon2?.position = SCNVector3(20, 3.5, 5)
            groundNode.addChildNode(icon2!)

            SCNTransaction.begin()
            SCNTransaction.animationDuration = 0.75
            icon1?.position = SCNVector3(-6, 3.5, 5)
            icon2?.position = SCNVector3(6, 3.5, 5)
            SCNTransaction.commit()
        case 2:
            SCNTransaction.begin()
            SCNTransaction.animationDuration = 0.75
            icon1?.position = SCNVector3(-6, 3.5, -5)
            icon2?.position = SCNVector3(6, 3.5, -5)
            icon1?.opacity = 0.0
            icon2?.opacity = 0.0
            SCNTransaction.commit()
            
            var delayInMilliseconds: Int = 0
            for node in boxes {
                DispatchQueue.main.asyncAfter(deadline: .now() + DispatchTimeInterval.milliseconds(delayInMilliseconds), execute: {
                    SCNTransaction.begin()
                    SCNTransaction.animationDuration = 0.5
                    node.rotation = SCNVector4(1, 0, 0, 0)
                    node.scale = SCNVector3(0.02, 0.02, 0.02)
                    node.opacity = 1.0
                    SCNTransaction.commit()
                })
                
                delayInMilliseconds += 50
            }
            
            textManager.flipOutTextOfType(.bullet)
            textManager.flipOutTextOfType(.subtitle)
            
            let _ = textManager.setSubtitle("Graphic Frameworks")
            
            textManager.flipInTextOfType(.bullet)
            textManager.flipInTextOfType(.subtitle)
            
        default:
            break;
        }
    }
    
    func addBoxWithTitle(_ title: String, frame: CGRect, level: Int, color: SKColor) {
        let node = SCNNode.boxNodeWithTitle(title, frame: frame, color: color, cornerRadius: 2.0, centered: true)
        node.pivot = SCNMatrix4MakeTranslation(0, Float(frame.height / 2), 0)
        node.scale = SCNVector3(0.02, 0, 0.02)
        node.position = SCNVector3(-5.0, (0.02 * Float(frame.height) / 2.0) + (1.5 * Float(level)), 10.0)
        node.rotation = SCNVector4(1.0, 0.0, 0.0, Float.pi / 2.0)
        node.opacity = 0.0
        contentNode.addChildNode(node)
        
        boxes.append(node)
    }
}
