//
//  AddSensorTableViewController.swift
//  Home
//
//  Created by Richard Kuckovsky on 7.5.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit

class AddSensorViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    
    var sensors = [sensorsType]()
    var colors = [String]()
    var new_room = Int()
    var new_room_index = Int()
    var new_sensor_index = Int()
    
    @IBOutlet weak var txt_name: UITextField!
    @IBOutlet weak var pckr_sensor: UIPickerView!
    @IBOutlet weak var pckr_color: UIPickerView!
    @IBOutlet weak var btn_done: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.hideKeyboardOnTap(#selector(self.dismissKeyboard))
        
        colors = ["Červená", "Čierna", "Modrá", "Hnedá", "Tyrkysová", "Tmavošedá", "Sivá", "Zelená", "Svetlosivá", "Magenta", "Oranžová", "Fialová", "Žltá"]

        // 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()
        
        
        pckr_color.delegate = self
        pckr_color.dataSource = self
        
        pckr_sensor.delegate = self
        pckr_sensor.dataSource = self
    
    }
    
    func dismissKeyboard() {
        view.endEditing(true)
        // do aditional stuff
    }
    
    @IBAction func txt_edit(_ sender: Any) {
        if(self.txt_name.text != "" && sensors.count != 0) {
            btn_done.isEnabled = true
        } else {
            btn_done.isEnabled = false
        }
    }
    @IBAction func cancel_btn(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }
    @IBAction func done_btn(_ sender: Any) {
        
        
        SocketIOManager.sharedInstance.addSensor(id: sensors[pckr_sensor.selectedRow(inComponent: 0)].id, room: new_room, name: self.txt_name.text!, color: pckr_color.selectedRow(inComponent: 0)) { (ok) in
            if(ok) {
                self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)].name = self.txt_name.text!
                self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)].room = self.new_room
                self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)].room_index = self.new_room_index
                self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)].sensor_index = self.new_sensor_index
                self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)].color = self.pckr_color.selectedRow(inComponent: 0)
                rooms[self.new_room_index].sensors.append(self.sensors[self.pckr_sensor.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.sensors.append(self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)])
                        }
                    }
                } else if let roomsController = self.presentingViewController as? UINavigationController {
                    if let room = roomsController.viewControllers[1] as? RoomController {
                        room.sensors.append(self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)])
                    }
                } else if let room = self.presentingViewController as? RoomController {
                    room.sensors.append(self.sensors[self.pckr_sensor.selectedRow(inComponent: 0)])
                }
                
            }
            
            
            self.dismiss(animated: true)
        }
    }
    

    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 {
        if(pickerView == pckr_sensor) {
            return sensors.count;
        } else if (pickerView == pckr_color) {
            return colors.count
        } else {
            return 0;
        }
    }
    
    // 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? {
        if(pickerView == pckr_sensor) {
            return "[" + String(sensors[row].device) + "] " + sensors[row].sensor_name;
        } else if (pickerView == pckr_color) {
            return colors[row]
        } else {
            return "";
        }
        
        
        //    return "[" + String(outputs[row-1].device) + "] " + outputs[row-1].name

    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {

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

}
