//
//  GameScene.swift
//  sleazyburgerpad
//
//  Created by Aslak Gronflaten on 1/23/15.
//  Copyright (c) 2015 Javelin. All rights reserved.
//

import SpriteKit

class GameSceneIPad: SKScene {
    var game : Game!
    var firstTouch, secondTouch : UITouch?
    var panning = false
    var part : SKSpriteNode?
    var zooming = false
    
    override func didMoveToView(view: SKView) {
        game = Game(scene: self, view: view, scaleIt: false)
        let panner = UIPanGestureRecognizer(target: self, action: NSSelectorFromString("pan:"))
        view.addGestureRecognizer(panner)
        let rotater = UIRotationGestureRecognizer(target: self, action: NSSelectorFromString("rotate:"))
        //view.addGestureRecognizer(rotater)  // This doesn't really work yet
        
        if false {
        let b = UIButton(frame: CGRectMake(100, 700, 80, 50))
        
        b.titleLabel?.text = "Zooms"
        b.setTitle("Zoom", forState: UIControlState.Normal)
        b.addTarget(self, action: "zoom:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(b)
        }
        
        view.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: "pinch:"))
    }
    
    var origPos = CGPointZero
    
    func pinch(e : UIPinchGestureRecognizer) {
        if game.world.hasActions() {
            return
        }
        if e.state == .Began {
            origPos = game.world.position
        }
        switch e.state {
            case .Began, .Changed:
                game.world.setScale(e.scale)
                let inv = 1 - e.scale
                let fy = 3.14 * (min(inv , 0.5))
                let y = 0 - origPos.y
                let sy = sin(fy)
                let ssy = sy * y

                
                let gx = game.plateX // 2
                let x = gx - origPos.x
                
                let s = -( 0.5 - e.scale)
                let yp = y * s
                game.world.position.y = origPos.y + ssy
                game.world.position.x = gx - (x * e.scale)
            default:
                game.world.setScale(1.0)
                game.world.position = origPos
        }

    }
    
    func zoom(e : AnyObject?) {
     //   game.world.zoomOut()
        game.outro()
    }
    
    func rotate(e : AnyObject?) {
        if let r = e as? UIRotationGestureRecognizer {
            println("Rotation: \(r.rotation)")
        }
    }
    
    func pan(e : AnyObject?) {
        if let p = e as? UIPanGestureRecognizer {
            let pos = p.locationInView(game.view)
            var location = pos
            location.y = 768 - location.y
            if p.numberOfTouches() == 2 {
                if panning {
                    // Still dragging
//                    print(".")
                    if let s = part {
                        location.y -= game.world.position.y
                        s.position = location
                        /// Now the rotation
                        let l1 = p.locationOfTouch(0, inView: game.view)
                        let l2 = p.locationOfTouch(1, inView: game.view)
                        var p1 : CGPoint
                        var p2 : CGPoint
                        
                        if l1.x < l2.x {
                            p1 = l1
                            p2 = l2
                        } else {
                            p1 = l2
                            p2 = l1
                        }
                        let xdiff = p1.x - p2.x
                        let ydiff = p1.y - p2.y
                        let angle = atan2(ydiff, -xdiff)
                        s.zRotation = angle
                    }
                } else {
//                    println("Start panning")
                    if let grabbed = game.getPartAt(location) {
                        panning = true
                        part = grabbed
                    }
                    
                }
            } else {
                if panning {
//                    println("Stopped panning with two fingers")
                    panning = false
                    if let sprite = part {
                        game.droppedPart(sprite)
                        part = nil
                    }
                }
            }
        }
    }
        
    /*
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        
        println("Began touches with \(touches.count) touched")
        for t: AnyObject in touches {
            if let touch = t as? UITouch {
            let location = touch.locationInNode(game.world)
//            game.clickAt(location)
            if firstTouch == nil {
                firstTouch = touch
                println("Registered the first touch: \(NSStringFromCGPoint(location))")
            } else if secondTouch == nil && touch != firstTouch {
                secondTouch = touch
                println("Registered the second touch: \(NSStringFromCGPoint(location))")
            }
            
            break
        }
        }
    }
*/
    
    /*
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
        if let ft = firstTouch {
            if let st = secondTouch {
                let p1 = ft.locationInNode(game.world)
                let p2 = ft.locationInNode(game.world)
                println("Moved touches to:  \(NSStringFromCGPoint(p1)) ,  \(NSStringFromCGPoint(p2))")
            }
        }
    }
*/
    
   
    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}
