//
//  CategoriesTableViewController.swift
//  Vendoo
//
//  Created by Okechi Onyeje on 7/16/16.
//  Copyright © 2016 Okechi Onyeje. All rights reserved.
//

import UIKit

class CategoriesTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPopoverPresentationControllerDelegate {
    
    private var itemListingDictionary: Dictionary<String, AnyObject>! = Dictionary<String, AnyObject>()
    private var graphManager: FacebookGraphAPIManager! = nil
    private var firManager: FirebaseManager! = nil
    private var etsyManager: EtsyRESTAPIManager! = nil
    private var ebayManager: EbayWebServiceManager! = EbayWebServiceManager()
    private var selectedCategory: String!
    private var selectedSub: String!
    private var potentialEbay = false
    private var potentialEtsy = false
    private var potentialAmazon = false

    private var categories = ["Accessories",
                              "Antiques",
                              "Art",
                              "Baby",
                              "Bags & Purses",
                              "Bath, Beauty & Health",
                              "Books",
                              "Business & Industrial",
                              "Cameras & Photo",
                              "Candles",
                              "Cell Phones & Accessories",
                              "Ceramics & Pottery",
                              "Children",
                              "Clothing & Shoes",
                              "Coins & Paper Money",
                              "Collectibles",
                              "Computers/Tablets & Networking",
                              "Consumer Electronics",
                              "Crafts",
                              "Crochet",
                              "Dolls & Miniatures",
                              "DVDs & Movies",
                              "Entertainment Memorabilia",
                              "Everything Else",
                              "Furniture",
                              "Geekery",
                              "Gift Cards & Coupons",
                              "Glass",
                              "Holidays",
                              "Home/Houseware & Garden",
                              "Jewelry & Watches",
                              "Knitting",
                              "Music",
                              "Musical Instruments & Gear",
                              "Needlecraft",
                              "Paper Goods",
                              "Patterns",
                              "Pet Supplies",
                              "Pottery & Glass",
                              "Plants and Edibles",
                              "Quilts",
                              "Real Estate",
                              "Specialty Services",
                              "Sporting Goods",
                              "Sports Memorabilia & Fan Shop",
                              "Stamps",
                              "Supplies",
                              "Tickets & Experiences",
                              "Toys & Hobbies",
                              "Travel",
                              "Video Games & Consoles",
                              "Vintage",
                              "Wedding",
                              "Woodworking"]
    
    @IBOutlet weak var table: UITableView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.table.delegate = self
        self.table.dataSource = self
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Table view data source
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.categories.count
    }
    
    
    func setListingDictionary(listingDictionary: Dictionary<String, AnyObject>){
        
        self.itemListingDictionary = listingDictionary
        
    }
    
    func setManagers(fbManager: FacebookGraphAPIManager, fireManager: FirebaseManager, ebayManager: EbayWebServiceManager, etsyManager: EtsyRESTAPIManager){
        self.graphManager = fbManager
        self.firManager = fireManager
        self.ebayManager = ebayManager
        self.etsyManager = etsyManager
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: CategoryCell! = tableView.dequeueReusableCellWithIdentifier("CategoryCell", forIndexPath: indexPath) as? CategoryCell
        
        // Configure the cell...
        
        
        dispatch_async(dispatch_get_main_queue(), {
            cell.categoryName.text = self.categories[indexPath.row]
            cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true //facebook
            cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
            cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
            cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
            
            //leaving this in for category debugging purposes
            /*
            switch (cell.categoryName.text!) {
            case ("Accessories") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Antiques") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Art") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Baby") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Bags & Purses") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Bath, Beauty & Health") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Books") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Business & Industrial") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Cameras & Photo") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Candles") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Cell Phones & Accessories") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Clothing & Shoes") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Ceramics & Pottery") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Children") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Coins & Paper Money") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Collectibles") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Computers/Tablets & Networking") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Consumer Electronics") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Crafts") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Dolls & Miniatures") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("DVDs & Movies") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Entertainment Memorabilia") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Everything Else") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Furniture") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Gift Cards & Coupons") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Geekery") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Glass") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Holidays") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Home/Houseware & Garden") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Jewelry & Watches") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Knitting") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Music") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Musical Instruments & Gear") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Needlecraft") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Paper Goods") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Patterns") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Pet Supplies") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Pottery & Glass") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Plants and Edibles") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Quilts") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Real Estate") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Specialty Services") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Sporting Goods") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Sports Memorabilia & Fan Shop") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Stamps") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Supplies") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Tickets & Experiences") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Toys & Hobbies") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Travel") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Video Games & Consoles") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = false //ebay
                //ask to drill into ebay categories
                cell.shouldAskAboutLeafCategories = true
                break
            case ("Vintage") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Wedding") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            case ("Woodworking") :
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = false //facebook
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = false //etsy
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true //amazon
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true //ebay
                break
            default:
                break
            }*/
            
            /*//ebay
            if(true){
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 0, inSection: 0))?.hidden = true
            }
            //amazon
            if(true){
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 1, inSection: 0))?.hidden = true
            }
            //etsy
            if(false){
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 2, inSection: 0))?.hidden = true
                
            }
            //facebook
            if(false){
                cell.networks.cellForItemAtIndexPath(NSIndexPath(forRow: 3, inSection: 0))?.hidden = true
            }*/
            
        })
        
        
        return cell
    }
    
    /*
     For certain marketplaces such as ebay, leaf categories must be used when posting
     */
    func navigateCategories () {
        self.providesPresentationContextTransitionStyle = true
        self.definesPresentationContext = true
        self.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
        var popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("NavigateCategory") as! CategoryPopUpController
        popoverContent.modalPresentationStyle = UIModalPresentationStyle.Popover
        var popover = popoverContent.popoverPresentationController
        popoverContent.preferredContentSize = CGSizeMake(50,50)
        popover!.delegate = self
        popover!.sourceView = self.view
        popover!.sourceRect = CGRectMake(100,100,50,50)
        
        popoverContent.setManagers(self.ebayManager)
        popoverContent.loadCategories(self.selectedCategory, selectedCatCompletion: {
            (categoryID, error) -> Void in
            self.selectedSub = categoryID as! String
            self.dismissViewControllerAnimated(true, completion: {
                () -> Void in
                self.performSegueWithIdentifier("NetworkSelectionSegue", sender: self)
            })
        })
        
        self.presentViewController(popoverContent, animated: true, completion: nil)
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        switch self.categories[indexPath.row] {
        case "Antiques":
            self.selectedCategory = "Antiques"
            break
        case "Art":
            self.selectedCategory = "Art"
            self.potentialEtsy = true
            break
        case "Baby":
            self.selectedCategory = "Baby"
            break
        case "Bath, Beauty & Health":
            self.selectedCategory = "Health and Beauty"
            self.potentialEtsy = true
            break
        case "Books":
            self.selectedCategory = "Books"
            self.potentialEtsy = true
            break
        case "Business & Industrial":
            self.selectedCategory = "Business and Industrial"
            break
        case "Cameras & Photo":
            self.selectedCategory = "Cameras and Photo"
            break
        case "Cell Phones & Accessories":
            self.selectedCategory = "Cell Phones and Accessories"
            break
        case "Clothing & Shoes":
            self.selectedCategory = "Clothing, Shoes and Accessories"
            self.potentialEtsy = true
            break
        case "Coins & Paper Money":
            self.selectedCategory = "Coins and Paper Money"
            break
        case "Collectibles":
            self.selectedCategory = "Collectibles"
            break
        case "Computers/Tablets and Networking":
            self.selectedCategory = "Computers/Tablets and Networking"
            break
        case "Consumer Electronics":
            self.selectedCategory = "Consumer Electronics"
            break
        case "Crafts":
            self.selectedCategory = "Crafts"
            break
        case "Dolls & Miniatures":
            self.selectedCategory = "Dolls and Bears"
            self.potentialEtsy = true
            break
        case "DVDs & Movies":
            self.selectedCategory = "DVDs & Movies"
            break
        case "Entertainment Memorabilia":
            self.selectedCategory = "Entertainment Memorabilia"
            break
        case "Everything Else":
            self.selectedCategory = "Everything Else"
            self.potentialEtsy = true
            break
        case "Gift Cards & Coupons":
            self.selectedCategory = "Gift Cards and Coupons"
            break
        case "Home/Houseware & Garden":
            self.selectedCategory = "Home and Garden"
            self.potentialEtsy = true
            break
        case "Jewelry & Watches":
            self.selectedCategory = "Jewelry and Watches"
            self.potentialEtsy = true
            break
        case "Musical Instruments & Gear":
            self.selectedCategory = "Musical Instruments and Gear"
            break
        case "Pet Supplies":
            self.selectedCategory = "Pet Supplies"
            break
        case "Pottery & Glass":
            self.selectedCategory = "Pottery and Glass"
            break
        case "Real Estate":
            self.selectedCategory = "Real Estate"
            break
        case "Specialty Services":
            self.selectedCategory = "Specialty Services"
            break
        case "Sporting Goods":
            self.selectedCategory = "Sporting Goods"
            break
        case "Sports Memorabilia & Fan Shop":
            self.selectedCategory = "Sports Mem, Cards and Fan Shop"
            break
        case "Tickets & Experiences":
            self.selectedCategory = "Tickets and Experiences"
            break
        case "Toys & Hobbies":
            self.selectedCategory = "Toys and Hobbies"
            self.potentialEtsy = true
            break
        case "Travel":
            self.selectedCategory = "Travel"
            break
        case "Video Games & Consoles":
            self.selectedCategory = "Video Games and Consoles"
            break
        default:
            self.selectedCategory = self.categories[indexPath.row]
            self.potentialEtsy = true
            break
        }
        
        let cell: CategoryCell! = tableView.cellForRowAtIndexPath(indexPath) as! CategoryCell//tableView.dequeueReusableCellWithIdentifier("CategoryCell", forIndexPath: indexPath) as? CategoryCell
        if(self.ebayManager.isAuthorized && cell.shouldAskAboutLeafCategories){
            self.potentialEbay = true
            self.navigateCategories()
        }
        else {
            self.performSegueWithIdentifier("NetworkSelectionSegue", sender: self)
        }
    }
    
    /*
     // Override to support conditional editing of the table view.
     override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */
    
    /*
     // Override to support editing the table view.
     override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
     if editingStyle == .Delete {
     // Delete the row from the data source
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
     } else if editingStyle == .Insert {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }    
     }
     */
    
    /*
     // Override to support rearranging the table view.
     override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
     
     }
     */
    
    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */
    
    
    // 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.
        
        if(segue.identifier == "NetworkSelectionSegue") {
            self.itemListingDictionary["category"] = self.selectedCategory
            self.itemListingDictionary["ebaySubCategoryID"] = self.selectedSub
            (segue.destinationViewController as! NetworksTableViewController).setListingDictionary(self.itemListingDictionary)
            (segue.destinationViewController as! NetworksTableViewController).setManagers(self.graphManager,
                                                                                          fireManager: self.firManager, ebayManager: self.ebayManager, etsyManager: self.etsyManager )
            
            (segue.destinationViewController as! NetworksTableViewController).setNetworkSelectFunctionality(true)
            
            //check what networks to load in network selection view
            if self.graphManager.isAuthorized {
                (segue.destinationViewController as! NetworksTableViewController).toggleMarketPlaceVisibitlity("facebook")
            }
            if self.potentialEbay && self.ebayManager.isAuthorized {
                (segue.destinationViewController as! NetworksTableViewController).toggleMarketPlaceVisibitlity("ebay")
            }
            if self.potentialEtsy && self.etsyManager.isAuthorized {
                (segue.destinationViewController as! NetworksTableViewController).toggleMarketPlaceVisibitlity("etsy")
            }
            if self.potentialAmazon {
                (segue.destinationViewController as! NetworksTableViewController).toggleMarketPlaceVisibitlity("amazon")
            }
            //print(self.categoryPicker.selectedRowInComponent(0))
        }
    }
    
    
    @IBAction func cancelCategorySelection(sender: AnyObject) {
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
}
