//
//  UsedDevicesViewController.swift
//  Home
//
//  Created by Richard Kuckovsky on 17.5.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit

class UsedDevicesViewController: UITableViewController {
    
    var names = [String]()
    var ids = [Int]()

    override func viewDidLoad() {
        super.viewDidLoad()

        SocketIOManager.sharedInstance.loadUsedDevices(completionHandler: { (names, ids) in
            self.names = names
            self.ids = ids
            
            self.tableView.reloadData()
        })
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return ids.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "device", for: indexPath)

        cell.textLabel?.text = "[" + String(ids[indexPath.row]) + "] " + names[indexPath.row]

        return cell
    }
    

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let deleteAction = UITableViewRowAction(style: .default, title: "Odstrániť") {action in
            
            self.view.isUserInteractionEnabled = false
            
                SocketIOManager.sharedInstance.deleteDevice(id: self.ids[indexPath.row], completionHandler: { (ok) in
                    if(ok) {
                        //rooms.remove(at: self.sensors[indexPath.row].room_index)
                        self.ids.remove(at: indexPath.row)
                        self.names.remove(at: indexPath.row)
                        
                        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
                        self.view.isUserInteractionEnabled = true
                    }
                })
            
            
        }
        
        return [deleteAction]
    }

    /*
    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> 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 prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
