//
//  MenuPanelViewController.swift
//  Vendoo
//
//  Created by Okechi Onyeje on 6/12/16.
//  Copyright © 2016 Okechi Onyeje. All rights reserved.
//

import UIKit
import FirebaseAuth
import Locksmith

class MenuPanelViewController: UIViewController{

    //Outlets
    @IBOutlet weak var table: UITableView!
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.table.dataSource = self
        self.table.delegate = self
        // Do any additional setup after loading the view.
    }

    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 MenuPanelViewController: UITableViewDelegate
{

    /*
     // 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
     }
     */

}

extension MenuPanelViewController: UITableViewDataSource
{

    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 6
    }
    
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell: UITableViewCell
        switch(indexPath.row){
        case 0:
            
            cell = (self.table.dequeueReusableCellWithIdentifier("User_Cell", forIndexPath: indexPath) as! UserCell)
                
            (cell as! UserCell).name.text = "Hi " + (NSUserDefaults.standardUserDefaults().objectForKey("name") as? String)!
            (cell as! UserCell).email.text = (NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!
            break
        case 1:
            cell = self.table.dequeueReusableCellWithIdentifier("Notifications_Cell")!
            break
        case 2:
            cell = self.table.dequeueReusableCellWithIdentifier("Settings_Cell")!
            break
        case 3:
            cell = self.table.dequeueReusableCellWithIdentifier("Sales_Cell")!
            break
        case 4:
            cell = self.table.dequeueReusableCellWithIdentifier("FAQ_Cell")!
            break
        default:
            cell = self.table.dequeueReusableCellWithIdentifier("Getting_Started_Cell")!
            break
        }
        
        return cell
    }

}

extension MenuPanelViewController{

    @IBAction func logoutUser(sender: AnyObject) {
        
        do{
            try FIRAuth.auth()?.signOut()
            try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo")
            
            do {
                try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_ebay")
            }catch {
                (error)
                print(error)
            }
            
            do {
               try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_etsy")
            }catch {
                (error)
                print(error)
            }
            
            do {
                try Locksmith.deleteDataForUserAccount((NSUserDefaults.standardUserDefaults().objectForKey("email") as? String)!, inService: "vendoo_oauth_amazon")
            }catch {
                (error)
                print(error)
            }

            for key in Array(NSUserDefaults.standardUserDefaults().dictionaryRepresentation().keys) {
                NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
            }
            
            let vc = self.storyboard?.instantiateViewControllerWithIdentifier("SignInViewController")
            self.presentViewController(vc!, animated: true, completion: nil)
        }
        catch{
            (error)
            print(error)
        }
    }
}

