//
//  AppDelegate.swift
//  SocketChat
//
//  Created by Gabriel Theodoropoulos on 1/31/16.
//  Copyright © 2016 AppCoda. All rights reserved.
//

import UIKit
import SocketIOClientSwift

let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
    var socket: SocketIOClient?
    var tcpClient:TCPClient?
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        self.window = UIWindow(frame: CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT))
        
        //注册推送
        let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
        let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
        application.registerUserNotificationSettings(pushNotificationSettings)
        application.registerForRemoteNotifications()
        
        
        
        //是否已登陆
        if(self.isLogin()){
            self.showRootViewContorller()
        }
        else{
            self.showLoginViewContorller()
        }
        tcpClient?.connect(USER_APP_SERVER as! String)
        
        
        
        //关联SB
        
        
        return true
    }
    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
        print("DEVICE TOKEN = \(deviceToken)")
    }
    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
        print("Fail to register APNs ----ErrorMsg:\(error)")
    }
    func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
        print(userInfo)
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        
        //        SocketIOManager.sharedInstance.closeConnection()
    }
    
    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        
        //        SocketIOManager.sharedInstance.establishConnection()
    }
    
    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    func isLogin()->Bool{
        let ret = ManagerUserDefault().SelectUserDefault("isLogin") as! Int
        
        if ret == 0{ return false}
        else{return true}
        
        
        
    }
    func showLoginViewContorller(){
        let loginvc:LoginViewController = self.mainStoryboard.instantiateViewControllerWithIdentifier("LoginView") as! LoginViewController
        self.window?.rootViewController = loginvc
        self.window?.makeKeyAndVisible()
        
    }
    func showRootViewContorller(){
        
        let tabbarController = UITabBarController()
        let RecentController = UINavigationController(rootViewController: RecentViewController())
        let ContactController = UINavigationController(rootViewController: ContactViewController())
        let NoticeController = UINavigationController(rootViewController: NoticeViewController())
        let SettingController = UINavigationController(rootViewController: SettingViewController())
        tabbarController.addChildViewController(RecentController)
        tabbarController.addChildViewController(ContactController)
        tabbarController.addChildViewController(NoticeController)
        tabbarController.addChildViewController(SettingController)
        
        //tabbarController.viewControllers = [RecentController,ContactController,NoticeController,SettingController]
        let tabitem1 = UITabBarItem(title: "消息", image: UIImage(named: "msg_icon"), selectedImage: UIImage(named: "msg_icon_"))
        let tabitem2 = UITabBarItem(title: "联系人", image: UIImage(named: "contact_icon"), selectedImage: UIImage(named: "contact_icon_sel"))
        let tabitem3 = UITabBarItem(title: "通知", image: UIImage(named: "function_icon_sel"), selectedImage: UIImage(named: "function_icon_sel"))
        let tabitem4 = UITabBarItem(title: "设置", image: UIImage(named: "setting_icon"), selectedImage: UIImage(named: "setting_icon_sel"))
        RecentController.tabBarItem = tabitem1
        ContactController.tabBarItem = tabitem2
        SettingController.tabBarItem = tabitem3
        NoticeController.tabBarItem = tabitem4
        //var MyAlert:UIAlertController?
        //self.mainStoryboard.instantiateViewControllerWithIdentifier("RootView") as! RootViewController
        //self.window?.rootViewController = window.rootViewController).selectedViewController;
        self.window?.rootViewController = tabbarController
        self.window?.makeKeyAndVisible()
        
    }
    
    
}

