//
//  BaseViewController.swift
//  TaptheButton
//
//  Created by Chenglin Liu on 3/16/16.
//  Copyright 漏 2016 Chenglin Liu. All rights reserved.
//

import UIKit

struct Constants {
    static let blinkingInterval = 0.2
    static let buttonScaleRatio:CGFloat = 0.9
    
}

class BaseViewController: UIViewController {
    @IBOutlet weak var tapButton: UIButton!
    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var gameOverLabel: UILabel!
    @IBOutlet weak var restartStackView: UIStackView!
    @IBOutlet weak var highScoreLabel: UILabel!
    var count = 0
    var color:UInt32 = 1
    var timer = NSTimer()
    lazy var prefs = NSUserDefaults.standardUserDefaults()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setUpScalingButton()
        setUpGame()
    }
    
    func setUpGame(){
        count = 0
        scoreLabel.text = "Score: \(count)"
        timer = NSTimer.scheduledTimerWithTimeInterval(Constants.blinkingInterval, target: self, selector: Selector("turnColor"),userInfo: nil, repeats: true)
        tapButton.imageView?.image = UIImage(named: "bluebutton")
        gameOverLabel.hidden = true
        restartStackView.hidden = true
        highScoreLabel.hidden = true
        highScoreLabel.textColor = UIColor.blackColor()
    }
    
    func turnColor(){
        color = arc4random_uniform(UInt32(2)+1)
        if color==1{
            tapButton.imageView?.image = UIImage(named: "bluebutton")
            
        }else{
            tapButton.imageView?.image = UIImage(named: "redbutton")
        }
    }
    
    func gameOver(){
        timer.invalidate()
        highScoreLabel.hidden = false
        tapButton.imageView?.image = UIImage(named: "redbutton")
        if let highScore=prefs.valueForKey("highScore") as? Int where highScore > count{
            highScoreLabel.text="High Score: \(highScore)"
        }else{
            highScoreLabel.text="High Score: \(count)"
            prefs.setValue(count, forKey: "highScore")
            highScoreLabel.textColor = UIColor.greenColor()
            prefs.synchronize()
        }
        gameOverLabel.hidden = false
        restartStackView.hidden = false
    }
    
//MARK: IBActions for each buttons
    
    @IBAction func restartTapped(sender: AnyObject) {
        setUpGame()
    }
    @IBAction func exitTapped(sender: AnyObject) {
        dismissViewControllerAnimated(false, completion: nil)
    }
    @IBAction func buttonPressed(sender: UIButton) {
        guard color==1 else{
            gameOver()
            return
        }
        count++
        scoreLabel.text = "Score :\(count)"
    }
 
//MARK: Scaling button
    func setUpScalingButton(){
        tapButton.layer.cornerRadius = tapButton.frame.width/2
        tapButton.addTarget(self, action: Selector("touchedDown"), forControlEvents: UIControlEvents.TouchDown)
        tapButton.addTarget(self, action: Selector("touchedUp"), forControlEvents: UIControlEvents.TouchUpInside)
        tapButton.addTarget(self, action: Selector("touchedUp"), forControlEvents: UIControlEvents.TouchUpOutside)
    }
    
    func touchedDown(){
        let scale:CGFloat = Constants.buttonScaleRatio
        self.tapButton.transform = CGAffineTransformMakeScale(scale, scale)
    }
    func touchedUp(){
            self.tapButton.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }
}
// 鐗堟潈灞炰簬鍘熶綔鑰?// http://code4app.com (cn) http://code4app.net (en)
// 鍙戝竷浠g爜浜庢渶涓撲笟鐨勬簮鐮佸垎浜綉绔? Code4App.com