//
//  AddOutputController.swift
//  Home
//
//  Created by Richard Kuckovsky on 24.4.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit

class AddOutputViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    var outputs = [outputsType]()
    var new_room = Int()
    var new_room_index = Int()
    var new_output_index = Int()
    
    
    @IBOutlet weak var btn_add: UIBarButtonItem!
    @IBOutlet weak var pckr_output: UIPickerView!
    @IBOutlet weak var txt_output: UITextField!
    @IBAction func btn_cancel_action(_ sender: Any) {
        self.dismiss(animated: true)
    }
    
    @IBAction func btn_done(_ sender: Any) {
        
        SocketIOManager.sharedInstance.addOutput(id: outputs[pckr_output.selectedRow(inComponent: 0)].id, room: self.new_room, name: self.txt_output.text!) { (ok) in
            if(ok) {
                self.outputs[self.pckr_output.selectedRow(inComponent: 0)].name = self.txt_output.text!
                self.outputs[self.pckr_output.selectedRow(inComponent: 0)].room = self.new_room
                self.outputs[self.pckr_output.selectedRow(inComponent: 0)].room_index = self.new_room_index
                self.outputs[self.pckr_output.selectedRow(inComponent: 0)].output_index = self.new_output_index
                rooms[self.new_room_index].outputs.append(self.outputs[self.pckr_output.selectedRow(inComponent: 0)])
                
                
                if let presenter = self.presentingViewController as? UITabBarController {
                    if let roomsController = presenter.viewControllers?[1] as? UINavigationController {
                        if let room = roomsController.viewControllers[1] as? RoomController {
                            room.outputs.append(self.outputs[self.pckr_output.selectedRow(inComponent: 0)])
                        }
                    }
                } else if let roomsController = self.presentingViewController as? UINavigationController {
                    if let room = roomsController.viewControllers[1] as? RoomController {
                        room.outputs.append(self.outputs[self.pckr_output.selectedRow(inComponent: 0)])
                    }
                } else if let room = self.presentingViewController as? RoomController {
                    room.outputs.append(self.outputs[self.pckr_output.selectedRow(inComponent: 0)])
                }
                
                
            }
            self.dismiss(animated: true)
        }
        
    }
    @IBAction func enable_done_button(_ sender: Any) {
        if(txt_output.text != "" && outputs.count != 0) {
            btn_add.isEnabled = true
        } else {
            btn_add.isEnabled = false
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hideKeyboardOnTap(#selector(self.dismissKeyboard))
        
        let myGroup = DispatchGroup()
        myGroup.enter()
        SocketIOManager.sharedInstance.loadRoom(room: 0, room_index: outputs.count) { (outputs, sensors, events) in
            self.outputs = outputs
            myGroup.leave()
        }
        
        myGroup.notify(queue: .main) {
            self.pckr_output.reloadAllComponents()
        }
        
        pckr_output.delegate = self
        pckr_output.dataSource = self
    }
    
    func dismissKeyboard() {
        view.endEditing(true)
        // do aditional stuff
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return outputs.count
    }
    
    // The data to return for the row and component (column) that's being passed in
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

            return "[" + String(outputs[row].device) + "] " + outputs[row].output_name
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        
    }

}
