//
//  ViewController.swift
//  Home
//
//  Created by Richard Kuckovsky on 21.4.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var lbl_loading: UILabel!
    var outputs = [outputsType]()

    var loaded = false

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
    }
    
    override func viewDidAppear(_ animated: Bool) {
        
        UIView.animate(withDuration: 5,animations: {
            self.lbl_loading.isHidden = false
        })
        
        let shared = UserDefaults(suiteName: "group.kuckovsky.home.key")
        
        if(shared?.value(forKey: "favourite_outputs_array") != nil) {
            favourite_outputs = shared?.value(forKey: "favourite_outputs_array") as! [Int]
        }
        
        if(shared?.value(forKey: "favourite_sensors_array") != nil) {
            favourite_sensors = shared?.value(forKey: "favourite_sensors_array") as! [Int]
        }
        
        SocketIOManager.sharedInstance.establishConnection()
        SocketIOManager.sharedInstance.checkConnect() { (messageInfo) -> Void in
            SocketIOManager.sharedInstance.listRooms() { (messageInfo) -> Void in
                if(!self.loaded) {
                    SocketIOManager.sharedInstance.stopCheckConnect()
                    self.performSegue(withIdentifier: "connected", sender: nil)
                    self.loaded = true
                }
            }
        }
    }
    
    /*
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if(segue.identifier == "connected") {
            if let TabBarController = segue.destination as? UITabBarController {
                if let NavigationController = TabBarController.viewControllers?[0] as? UINavigationController {
                    if let DestinationController = NavigationController.viewControllers[0] as? FavouriteTableController {
                        
                        DestinationController.outputs = outputs
                    }
                }
            }
        }
    }
 */

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}

extension UIViewController {
    func hideKeyboardOnTap(_ selector: Selector) {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: selector)
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)
    }
}
