//
//  Outro.swift
//  sleazyburger
//
//  Created by Aslak Gronflaten on 1/25/15.
//  Copyright (c) 2015 Javelin. All rights reserved.
//

import SpriteKit

protocol OutroDelegate {
    func gotoMinigame()
}

class Outro : SKScene {

    var game : Game!
    var outroDelegate : OutroDelegate?
    
    init(game: Game) {
        self.game = game
        super.init(size: game.view.bounds.size)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    let panelNames = ["left", "middle", "right"]
    var panels : [SKSpriteNode] = []
    var positions : [CGPoint] = []
    
    override func didMoveToView(view: SKView) {
        for n in panelNames {
            let panel = SKSpriteNode(imageNamed: "topple_\(n).png")
            panels.append(panel)
            self.addChild(panel)
        }
        positions.append(  CGPointMake(panels[0].size.width / 2, 768/2 ) )
        positions.append( CGPointMake(512, 768/2 ) )
        positions.append( CGPointMake(1024 - (panels[2].size.width / 2), 768/2 ) )
        
        for i in 0...2 {
            panels[i].position = CGPointMake(positions[i].x, 2000)
            let wait : NSTimeInterval = 0.8 * Double(i)
            panels[i].runAction(SKAction.waitForDuration(wait)) {
                self.panels[i].runAction(SKAction.moveToY(768/2, duration: 0.3)) {
                    if i == 2 {
                        self.shakeIt()
                    }
                }
            }
        }
    }
    
    func shakeIt() {
        // Animate in theme text
        let text = SKSpriteNode(imageNamed: "whatdowedonow.png")
        text.setScale(0.001)
        text.position = CGPointMake(512, 768/2)
        self.addChild(text)
        text.runAction(SKAction.scaleTo(1.0, duration: 0.3)) {
            // Shake? How?
            self.shake(text)
        }
    }
    
    func shake(s : SKSpriteNode) {
        let v = 0.05
        let y = [-3,3,0, 0, 4,-4, 0,0]
        let x = [ 0,0,3,-3,-4, 4,-3,3]
        var actions : [SKAction] = []
        for rep in 0...2 {
        for i in 0..<y.count {
            let yy =  CGFloat(y[i])
            let xx = CGFloat(x[i])
            let a = SKAction.moveBy(CGVectorMake(xx, yy), duration: v)
            actions.append(a)
        }
        }
        s.runAction(SKAction.sequence(actions)) {
            s.runAction(SKAction.waitForDuration(2.5)) {
                self.removeAllChildren()
                self.sleazeIt()
            }
        }
    }
    
    func sleazeIt() {
        let s = SKSpriteNode(imageNamed: "topple_sleaze.png")
        s.position = CGPointMake(512, 768/2)
        s.runAction(SKAction.rotateByAngle(3.14*2*5, duration: 1))
        s.runAction(SKAction.scaleTo(1.0, duration: 1)) {
            s.runAction(SKAction.waitForDuration(2.5)) {
                // Switch to minigame
                if let d = self.outroDelegate {
                    d.gotoMinigame()
                }
            }
        }
        self.addChild(s)
    }
}
