//
//  HomeViewController.swift
//  Vendoo
//
//  Created by Okechi Onyeje on 5/26/16.
//  Copyright © 2016 Okechi Onyeje. All rights reserved.
//

import UIKit

class HomeViewController: UITabBarController {
    //acts as RESTful api call manager for etsy
    //call this from tabbar controller to use etsy REST calls
    let etsyManager: EtsyRESTAPIManager = EtsyRESTAPIManager()
    let fbGraphManager = FacebookGraphAPIManager()
    let firebaseManager = FirebaseManager()
    let ebayGraphManager = EbayWebServiceManager()
    var userListings: [Listing] = []
    var endedListings: [Listing] = []
    var soldListings: [Listing] = []
    var notificationsManager = ServiceNotificationManager()
    var notifications: [Dictionary<String, AnyObject>] = (NSUserDefaults.standardUserDefaults().objectForKey("notifications") == nil ? [] : (NSUserDefaults.standardUserDefaults().objectForKey("notifications") as! [Dictionary<String, AnyObject>]))
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        ServiceNotificationManager.delegate = self
        self.notificationsManager.setManagers(fbGraphManager, fireManager: firebaseManager, ebayManager: ebayGraphManager, etsyManager: etsyManager)
        // Do any additional setup after loading the view.
        dispatch_async(dispatch_get_main_queue(), {
            
            //get all listing information for current user
            self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").observeSingleEventOfType( .Value, withBlock: {
                (snapshot) -> Void in
                
                let userDict = snapshot.value as? [String : AnyObject]
                
                let listingDict = userDict!["user_Listings"] as? [String : AnyObject]
                
                //sync notifications
                if userDict!["notifications"] != nil {
                    self.notifications = []
                    for (_, v) in userDict!["notifications"] as! [String : AnyObject] {
                        if v["seen"] as! Bool == false/*add network visibility as well*/{
                            var is_networkAuth = false
                            switch((v["network"] as! String)){
                            case "ebay":
                                is_networkAuth = NSUserDefaults.standardUserDefaults().boolForKey("ebayAuthorized")
                                break
                            case "etsy":
                                is_networkAuth = NSUserDefaults.standardUserDefaults().boolForKey("etsyAuthorized")
                                break
                            case "facebook" :
                                is_networkAuth = NSUserDefaults.standardUserDefaults().boolForKey("fbAuthorized")
                                break
                            case "amazon":
                                break
                            default:
                                break
                            }
                            if is_networkAuth {
                                self.notifications.append(v as! Dictionary<String, AnyObject>)
                            }
                        }
                    }
                    
                    NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications")
                }
                
                NSUserDefaults.standardUserDefaults().setObject(userDict!["name"] as? String, forKey: "name")
                
                if listingDict != nil {
                    var notificationStartTracker = 0
                    dispatch_async(dispatch_get_main_queue(), {
                        //let notificationStartGroup = dispatch_group_create()
                        for (key, values) in listingDict! {
                            let serviceGroup: dispatch_group_t = dispatch_group_create()
                            //dispatch_group_enter(notificationStartGroup)
                            self.firebaseManager.ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
                                
                                let databaseDict = snapshot.value as! [String : AnyObject]
                                
                                let storageRef = self.firebaseManager.storage.referenceForURL((databaseDict["image_storage"] as? String)!)
                                let mainListingImage = storageRef.child("images/\(key)/main.jpg")
                                
                                
                                dispatch_group_enter(serviceGroup)
                                mainListingImage.dataWithMaxSize(10 * 1024 * 1024, completion: {
                                    (dataMain, error) -> Void in
                                    
                                    if(error != nil){
                                        print("problem retrieving items")
                                        notificationStartTracker += 1
                                    }else{
                                        
                                        let mainImage = UIImage(data: dataMain!)
                                        var listingImages: [UIImage] = [mainImage!]
                                        var count = 0
                                        //Get supporting images
                                        for i in 1...4 {
                                            let supportListingImg1 = storageRef.child("images/\(key)/\(i).jpg")
                                            supportListingImg1.dataWithMaxSize(10 * 1024 * 1024, completion: {
                                                (data1, error) -> Void in
                                                
                                                if(error != nil){
                                                    print("support image \(i) for key, \(key), does not exist")
                                                }else{
                                                    listingImages.append(UIImage(data: data1!)!)
                                                }
                                                print(key)
                                                count+=1
                                                if(count == 4){
                                                    dispatch_group_leave(serviceGroup)
                                                    notificationStartTracker += 1
                                                    //dispatch_group_enter(notificationStartGroup)
                                                    //dispatch_group_leave(notificationStartGroup)
                                                }
                                            })
                                            
                                        }
                                        
                                        
                                        dispatch_group_notify(serviceGroup, dispatch_get_main_queue(), {
                                            print(listingImages.count)
                                            let listingInfo = values as? [String : AnyObject]
                                            
                                            if listingInfo!["listingStatus"] == nil {
                                                self.userListings.append(
                                                    Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!,
                                                        itemCategory: listingInfo!["listingCategory"] as? String,
                                                        itemQuantity: (listingInfo!["listingQuantity"] as? String)!,
                                                        itemPrice: listingInfo!["listingPrice"] as? String,
                                                        itemDescription: listingInfo!["listingDescription"] as? String,
                                                        itemImages: listingImages,
                                                        isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!,
                                                        itemKey: key,
                                                        networksSellingOn: (listingInfo!["networks"] as? Dictionary<String, Bool>)!
                                                ))
                                            }
                                            else if listingInfo!["listingStatus"] as? String == "Ended" {
                                                self.endedListings.append(
                                                    Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!,
                                                        itemCategory: listingInfo!["listingCategory"] as? String,
                                                        itemQuantity: (listingInfo!["listingQuantity"] as? String)!,
                                                        itemPrice: listingInfo!["listingPrice"] as? String,
                                                        itemDescription: listingInfo!["listingDescription"] as? String,
                                                        itemImages: listingImages,
                                                        isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!,
                                                        itemKey: key,
                                                        networksSellingOn: (listingInfo!["networks"] as? Dictionary<String, Bool>)!
                                                ))
                                            }
                                            else if listingInfo!["listingStatus"] as? String == "Sold" {
                                                self.soldListings.append(
                                                    Listing(itemTitle: (listingInfo!["listingTitle"] as? String)!,
                                                        itemCategory: listingInfo!["listingCategory"] as? String,
                                                        itemQuantity: (listingInfo!["listingQuantity"] as? String)!,
                                                        itemPrice: listingInfo!["listingPrice"] as? String,
                                                        itemDescription: listingInfo!["listingDescription"] as? String,
                                                        itemImages: listingImages,
                                                        isDraftListing: (listingInfo!["isListingDraft"] as? Bool)!,
                                                        itemKey: key,
                                                        networksSellingOn: (listingInfo!["networks"] as? Dictionary<String, Bool>)!
                                                    ))
                                            }

                                            print(notificationStartTracker)
                                            if(notificationStartTracker == listingDict?.count){
                                                NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil)
                                                self.notificationsManager.setListings(self.userListings)
                                                self.notificationsManager.startServicePolling()
                                                /*self.notificationsManager.startServicePolling({
                                                    (_,_) -> Void in
                                                    NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil)

                                                    
                                                })*/
                                                
                                            }
                                            
                                            
                                            
                                        })
                                    
                                        
                                        
                                        
                                    }
                                })
                                
                                
                            })
                            
                            
                        }
                        /*
                        dispatch_group_notify(notificationStartGroup, dispatch_get_main_queue(), {
                            

                        })*/
                        
                        
                        
                    })
                }else {
                    NSNotificationCenter.defaultCenter().postNotificationName("finished_fetching_listings", object: nil)
                }
            
                
            })
        })
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    
    
}


extension HomeViewController: ServiceNotificationManagerDelegate {
    func listingHasFinishedRetrievingNotifications() {
        NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications")
    }
    
    func listingHasStartedRetrievingNotifications() {
        
    }
    
    func listingHasEnded(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) {
        var notificationArray: [[String: AnyObject]] = self.notifications
        
        if (fbInfo != nil) {
            notificationArray.append(
                [
                    "network": "facebook",
                    "message": (ebayInfo as! [String : AnyObject])["notification"] as! String,
                    "type": "ended",
                    "seen":false,
                    "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String,
                    "UUID": NSUUID().UUIDString
                ]
            )
        }
        
        if (ebayInfo != nil) {
            let notification = [
                "network": "ebay",
                "message": (ebayInfo as! [String : AnyObject])["notification"] as! String,
                "type": (ebayInfo as! [String : AnyObject])["type"] as! String,
                "seen":false,
                "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String,
                "UUID": NSUUID().UUIDString
            ]
            
            notificationArray.append(
                notification as! [String : AnyObject]
            )
            self.endedListings.appendContentsOf( self.userListings.filter({$0.key == ((ebayInfo as! [String : AnyObject])["listingKey"] as! String)}))
            self.userListings = self.userListings.filter({$0.key != ((ebayInfo as! [String : AnyObject])["listingKey"] as! String)})
            let itemsView = self.viewControllers![0] as! ItemTableViewController
            
            /*self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((ebayInfo as! [String : AnyObject])["listingKey"] as! String))").observeSingleEventOfType( .Value, withBlock: {
                (snapshot) -> Void in
                
                var listingDict = snapshot.value as? [String : AnyObject]
                listingDict!["listingStatus"] = "Ended"
                self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/user_Listings/\(((ebayInfo as! [String : AnyObject])["listingKey"] as! String))").setValue(listingDict)
                
                itemsView.itemTable.reloadData()
            })*/
            
            self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").observeSingleEventOfType( .Value, withBlock: {
                (snapshot) -> Void in
                
                var userDict = snapshot.value as? [String : AnyObject]
                var notifications = userDict!["notifications"] as? [String: AnyObject]
                
                if notifications == nil {
                    userDict!["notifications"] = [((ebayInfo as! [String : AnyObject])["listingKey"] as! String):notification]
                }else {
                    notifications![((ebayInfo as! [String : AnyObject])["listingKey"] as! String)] = notification
                    userDict!["notifications"] = notifications
                }
                
                var listings = userDict!["user_Listings"] as! [String: AnyObject]
                var listingDict = (listings[((ebayInfo as! [String : AnyObject])["listingKey"] as! String)] as! [String : AnyObject])
                listingDict["listingStatus"] = "Ended"
                
                //save everything back to their respective dictionaries
                listings[((ebayInfo as! [String : AnyObject])["listingKey"] as! String)] = listingDict
                userDict!["user_Listings"] = listings
                
                self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").setValue(userDict, withCompletionBlock: {
                    (error, reference) -> Void in
                    
                    if error == nil {
                        //reload data for notifications
                        NSUserDefaults.standardUserDefaults().setObject(notificationArray, forKey: "notifications")
                        itemsView.itemTable.reloadData()
                    }
                })
                
            })

        }
        
        if (amazonInfo != nil) {
            notificationArray.append(
                [
                    "network": "amazon",
                    "message": (ebayInfo as! [String : AnyObject])["notification"] as! String,
                    "type": (ebayInfo as! [String : AnyObject])["type"] as! String,
                    "seen":false,
                    "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String,
                    "UUID": NSUUID().UUIDString
                ]
            )
        }
        
        if (etsyInfo != nil) {
            let notification = [
                "network": "etsy",
                "message": (etsyInfo as! [String : AnyObject])["notification"] as! String,
                "type": "ended",
                "seen":false,
                "key": (etsyInfo as! [String : AnyObject])["listingKey"] as! String,
                "UUID": NSUUID().UUIDString
            ]
            notificationArray.append(
                notification as! [String : AnyObject]
            )
            self.endedListings.appendContentsOf( self.userListings.filter({$0.key == ((etsyInfo as! [String : AnyObject])["listingKey"] as! String)}))
            self.userListings = self.userListings.filter({$0.key != ((etsyInfo as! [String : AnyObject])["listingKey"] as! String)})
            let itemsView = self.viewControllers![0] as! ItemTableViewController
            
            self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").observeSingleEventOfType( .Value, withBlock: {
                (snapshot) -> Void in
                
                var userDict = snapshot.value as? [String : AnyObject]
                var notifications = userDict!["notifications"] as? [String: AnyObject]
                
                if notifications == nil {
                    userDict!["notifications"] = [((etsyInfo as! [String : AnyObject])["listingKey"] as! String):notification]
                }else {
                    notifications![((etsyInfo as! [String : AnyObject])["listingKey"] as! String)] = notification
                    userDict!["notifications"] = notifications
                }
                
                var listings = userDict!["user_Listings"] as! [String: AnyObject]
                var listingDict = (listings[((etsyInfo as! [String : AnyObject])["listingKey"] as! String)] as! [String : AnyObject])
                listingDict["listingStatus"] = "Ended"
                
                //save everything back to their respective dictionaries
                listings[((etsyInfo as! [String : AnyObject])["listingKey"] as! String)] = listingDict
                userDict!["user_Listings"] = listings
                
                self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").setValue(userDict, withCompletionBlock: {
                    (error, reference) -> Void in
                    
                    if error == nil {
                        //reload data for notifications
                        NSUserDefaults.standardUserDefaults().setObject(notificationArray, forKey: "notifications")
                        itemsView.itemTable.reloadData()
                    }
                })
                
            })
        }
        
    }
    
    func listingHasNewNotifications(fbInfo: AnyObject?, ebayInfo: AnyObject?, amazonInfo: AnyObject?, etsyInfo: AnyObject?) {
        
        var notificationArray: [Dictionary<String, AnyObject>] = self.notifications
        
            if (fbInfo != nil) {
                notificationArray.append(fbInfo as! [String : AnyObject])
                
                    self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").observeSingleEventOfType(.Value, withBlock: {
                        (snapshot) -> Void in
                        
                        var userDict = snapshot.value as? [String : AnyObject]
                        
                        if (userDict!["notifications"] != nil) {
                            var notificationsFir = userDict!["notifications"] as! [String : AnyObject]
                            notificationsFir[fbInfo!["UUID"] as! String] = fbInfo
                            
                            self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)/notifications").setValue(notificationsFir, withCompletionBlock: {
                                (error, reference) -> Void in
                                
                                if error == nil {
                                    NSUserDefaults.standardUserDefaults().setObject(notificationArray, forKey: "notifications")
                                }
                                
                            })
                        }else {
                            let id = (fbInfo!["UUID"] as! String)
                            print(id)
                            userDict!["notifications"] = ([ id : fbInfo as! [String:AnyObject]])
                            self.firebaseManager.ref.child("Users/\(self.firebaseManager.user_email)").setValue(userDict, withCompletionBlock: {
                                (error, reference) -> Void in
                                
                                if error == nil {
                                    NSUserDefaults.standardUserDefaults().setObject(notificationArray, forKey: "notifications")
                                }
                            })
                        }
                    })

                
            }
            
            if (ebayInfo != nil) {
                notificationArray.append(
                    [
                        "network": "ebay",
                        "message": (ebayInfo as! [String : AnyObject])["notification"] as! String,
                        "type": (ebayInfo as! [String : AnyObject])["type"] as! String,
                        "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String,
                        "UUID": NSUUID().UUIDString
                    ]
                )
            }
            
            if (amazonInfo != nil) {
                notificationArray.append(
                    [
                        "network": "amazon",
                        "message": (ebayInfo as! [String : AnyObject])["notification"] as! String,
                        "type": "",
                        "key": (ebayInfo as! [String : AnyObject])["listingKey"] as! String,
                        "UUID": NSUUID().UUIDString
                    ]
                )
            }
            
            if (etsyInfo != nil) {
                notificationArray.append(
                    [
                        "network": "etsy",
                        "message": (etsyInfo as! [String : AnyObject])["notification"] as! String,
                        "type": (etsyInfo as! [String : AnyObject])["type"] as! String,
                        "key": (etsyInfo as! [String : AnyObject])["listingKey"] as! String,
                        "UUID": NSUUID().UUIDString
                    ]
                )
            }
        
            //@TODO: add firbase logic to add notifications and delete already viewed notifications
            //reload data for notifications

    }
}
