//
//  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 {
    
    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 categories = ["Accessories",
                              "Art",
                              "Bath & Beauty",
                              "Bags & Purses",
                              "Books",
                              "Candles",
                              "Ceramics & Pottery",
                              "Children",
                              "Clothing",
                              "Crochet",
                              "Dolls & Miniatures",
                              "Everything Else",
                              "Furniture",
                              "Geekery",
                              "Glass",
                              "Holidays",
                              "Houseware",
                              "Jewelry",
                              "Knitting",
                              "Music",
                              "Needlecraft",
                              "Paper Goods",
                              "Patterns",
                              "Plants and Edibles",
                              "Quilts",
                              "Supplies",
                              "Toys",
                              "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]
            
            //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
    }
    
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.selectedCategory = self.categories[indexPath.row]
        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
            (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)
            //print(self.categoryPicker.selectedRowInComponent(0))
        }
    }
    
    
    @IBAction func cancelCategorySelection(sender: AnyObject) {
        
        self.dismissViewControllerAnimated(true, completion: nil)
    }
    
    
}
