//
//  FavouriteController.swift
//  Home
//
//  Created by Richard Kuckovsky on 21.4.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit
import Charts

class RoomSensorCellController: UITableViewCell {
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var lbl_value: UILabel!
    @IBOutlet weak var vw_chart: LineChartView!
    
    var data = [Double]()

}

class RoomSwitchCellController: UITableViewCell {
    
    var switchDelegate: switchCellDelegate?
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var `switch`: UISwitch!
    @IBAction func switch_action(_ sender: Any) {
        if let delegate = switchDelegate {
            delegate.switchCellTapped(index: (sender as AnyObject).tag, value: self.switch.isOn) { (done: Bool) -> Void in
                if(!done) {
                    self.switch.isOn = !self.switch.isOn
                }
            }
        }
    }
    
}

class RoomTermostatCellController: UITableViewCell {
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var lbl_temp: UILabel!
    @IBOutlet weak var img_temp: UIImageView!
    

}

class RoomSliderCellController: UITableViewCell {
    
    var sliderDelegate: sliderCellDelegate?
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var slider: UISlider!
    @IBAction func slider_action(_ sender: Any) {
        if let delegate = sliderDelegate {
            delegate.sliderCellTapped(index: (sender as AnyObject).tag, value: Int(self.slider.value)) { (done: Bool) -> Void in

            }
        }
    }
}


class RoomEventCellController: UITableViewCell {
    
    var eventDelegate: eventCellDelegate?
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var `switch`: UISwitch!
    @IBAction func switch_action(_ sender: Any) {
        if let delegate = eventDelegate {
            delegate.eventCellTapped(id: (sender as AnyObject).tag, value: self.switch.isOn) { (done: Bool) -> Void in
                if(!done) {
                    self.switch.isOn = !self.switch.isOn
                }
            }
        }
    }
}


protocol switchCellDelegate {
    func switchCellTapped(index: Int, value: Bool, completionHandler: @escaping (_ status: Bool) -> Void)
}

protocol sliderCellDelegate {
    func sliderCellTapped(index: Int, value: Int, completionHandler: @escaping (_ status: Bool) -> Void)
}

protocol buttonCellDelegate {
    func buttonCellTapped(index: Int, value: Int)
}

protocol colorCellDelegate {
    func colorCellTapped(index: Int, value: Int)
}

protocol eventCellDelegate {
    func eventCellTapped(id: Int, value: Bool, completionHandler: @escaping (_ status: Bool) -> Void)
}





class RoomController: UITableViewController, switchCellDelegate, sliderCellDelegate, buttonCellDelegate, colorCellDelegate, eventCellDelegate {
    
    var outputs = [outputsType]()
    var sensors = [sensorsType]()
    var events = [eventsType]()
    
    var room = Int()
    var room_index = Int()
    
    var sections = [String]()
    var sensors_selected = [Bool]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.tableView.estimatedRowHeight = 44
        self.tableView.rowHeight = UITableViewAutomaticDimension
        
    }
    
    
    @IBAction func show_room_options(_ sender: Any) {
        let option_menu = UIAlertController(title: nil, message: "Pridať", preferredStyle: .actionSheet)
        
        option_menu.addAction(UIAlertAction(title: "Senzor", style: .default, handler: { (alert:UIAlertAction!)->Void in

            SocketIOManager.sharedInstance.showUnusedSensors(completionHandler: { (sensors) in
                self.performSegue(withIdentifier: "add_sensor", sender: sensors)
            })
        }))
        
        option_menu.addAction(UIAlertAction(title: "Výstup", style: .default, handler: { (alert:UIAlertAction!)->Void in
            SocketIOManager.sharedInstance.showUnusedOutputs(completionHandler: { (outputs) in
                self.performSegue(withIdentifier: "add_output", sender: outputs)
            })
        }))
        
        option_menu.addAction(UIAlertAction(title: "Plánovanú udalosť", style: .default, handler: { (alert:UIAlertAction!)->Void in
            self.performSegue(withIdentifier: "add_time_event", sender: self.outputs)
        }))
        
        option_menu.addAction(UIAlertAction(title: "Udalosť zo senzora", style: .default, handler: { (alert:UIAlertAction!)->Void in
            self.performSegue(withIdentifier: "add_sensor_event", sender: self)
        }))
        
        option_menu.addAction(UIAlertAction(title: "Zrušiť", style: .cancel, handler: nil))
        self.present(option_menu, animated: true)

    }
    
    
    
    
    let selectedCellHeight: CGFloat = 144.0
    let unselectedCellHeight: CGFloat = 44.0
    var selectedCellIndexPath: NSIndexPath?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        var ids = [Int]()
        
        for room in rooms {
            if(room.id == self.room) {
                outputs = room.outputs
                break
            }
            room_index += 1
        }
        for output in outputs {
            ids.append(output.id)
        }

        for _ in sensors {
            sensors_selected.append(false)
        }
        
        sections.removeAll()
        if(sensors.count > 0) {
            sections.append("sensors")
        }
        if(outputs.count > 0) {
            sections.append("outputs")
        }
        if(events.count > 0) {
            sections.append("events")
        }
        
        tableView.reloadData()
        
        SocketIOManager.sharedInstance.outputUpdate(ids: ids) { (id, value, temp) -> Void in
            
            let output_index = ids.index(of: id)!
            let room_index = self.room_index

            let indexPath : IndexPath = NSIndexPath(row: output_index, section: self.sections.index(of: "outputs")!) as IndexPath

            if(rooms[room_index].outputs[output_index].type == "switch") {
                let cell = self.tableView.cellForRow(at: indexPath) as! RoomSwitchCellController
                if(value == 0) {
                    cell.switch.setOn(false, animated: true)
                    rooms[room_index].outputs[output_index].value = 0
                } else {
                    cell.switch.setOn(true, animated: true)
                    rooms[room_index].outputs[output_index].value = 1
                }
            } else if(rooms[room_index].outputs[output_index].type == "termostat") {
                
                let cell = self.tableView.cellForRow(at: indexPath) as! RoomTermostatCellController
                if(value == 1) {
                    cell.img_temp.tintColor = UIColor(red: 1, green: 100/255, blue: 100/255, alpha: 1)
                } else {
                    cell.img_temp.tintColor = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1)
                }
                rooms[room_index].outputs[output_index].value = value
                self.outputs[indexPath.row].value = value
                
                cell.lbl_temp.text = String(temp) + "°C"
                
            } else if(rooms[room_index].outputs[output_index].type == "slider") {
                let cell = self.tableView.cellForRow(at: indexPath) as! RoomSliderCellController
                cell.slider.setValue(Float(value), animated: true)
                rooms[room_index].outputs[output_index].value = value
            }
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        SocketIOManager.sharedInstance.dismissOutputUpdate()
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if(segue.identifier == "termostat") {
            if let DestinationController = segue.destination as? TermostatViewController {
                
                let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow! as NSIndexPath
                
                DestinationController.output = outputs[indexPath.row]
                DestinationController.indexPath = indexPath
                DestinationController.origin = "room"
            }
        } else if(segue.identifier == "add_sensor") {
            if let NavigationController = segue.destination as? UINavigationController {
                if let DestinationController = NavigationController.viewControllers[0] as? AddSensorViewController {
                    DestinationController.sensors = (sender as? [sensorsType])!
                    DestinationController.new_room = room
                    DestinationController.new_room_index = room_index
                    DestinationController.new_sensor_index = sensors.count
                }
            }
        } else if(segue.identifier == "add_output") {
            if let NavigationController = segue.destination as? UINavigationController {
                if let DestinationController = NavigationController.viewControllers[0] as? AddOutputViewController {
                    DestinationController.outputs = (sender as? [outputsType])!
                    DestinationController.new_room = room
                    DestinationController.new_room_index = room_index
                    DestinationController.new_output_index = outputs.count
                }
            }
        } else if(segue.identifier == "add_time_event") {
            if let NavigationController = segue.destination as? UINavigationController {
                if let DestinationController = NavigationController.viewControllers[0] as? AddTimeEventViewController {
                    DestinationController.outputs = (sender as? [outputsType])!
                    DestinationController.room_id = room
                    DestinationController.room_index = room_index
                    DestinationController.event_index = events.count
                }
            }
        } else if(segue.identifier == "add_sensor_event") {
            if let NavigationController = segue.destination as? UINavigationController {
                if let DestinationController = NavigationController.viewControllers[0] as? AddSensorEventViewController {
                    DestinationController.outputs = self.outputs
                    var push_sensors = [sensorsType]()
                    for sensor in sensors {
                        if(sensor.type == "push") { push_sensors.append(sensor) }
                    }
                    DestinationController.sensors = push_sensors
                    DestinationController.room_id = room
                    DestinationController.room_index = room_index
                    DestinationController.event_index = events.count
                }
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        var rows = outputs.count
        
        if(sections[section] == "sensors") {
            rows = sensors.count
        } else if(sections[section] == "outputs") {
            rows = outputs.count
        } else if(sections[section] == "events") {
            rows = events.count
        }
        
        return rows
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var return_cell = UITableViewCell()
        
        
        if(sections[indexPath.section] == "outputs") {
            
        if(outputs[indexPath.row].type == "switch") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "switch") as! RoomSwitchCellController
            
            if cell.switchDelegate == nil {
                cell.switchDelegate = self
            }
            
            if(outputs[indexPath.row].value == 0) {
                cell.switch.setOn(false, animated: false)
            } else {
                cell.switch.setOn(true, animated: false)
            }
            
            cell.lbl_name.text = outputs[indexPath.row].name
            cell.switch.tag = indexPath.row
            
            return_cell = cell
        } else if(outputs[indexPath.row].type == "termostat") {
            let cell = tableView.dequeueReusableCell(withIdentifier: "termostat") as! RoomTermostatCellController
            
            if(outputs[indexPath.row].value == 1) {
                cell.img_temp.tintColor = UIColor(red: 1, green: 100/255, blue: 100/255, alpha: 1)
            } else {
                cell.img_temp.tintColor = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1)
            }
            cell.lbl_name.text = outputs[indexPath.row].name
            cell.lbl_temp.text = String(describing: outputs[indexPath.row].temp) + "°C"
            //cell.slider.tag = indexPath.row
            
            return_cell = cell
        } else if(outputs[indexPath.row].type == "slider") {

            let cell = tableView.dequeueReusableCell(withIdentifier: "slider") as! RoomSliderCellController
            
            
            if cell.sliderDelegate == nil {
                cell.sliderDelegate = self
            }
            cell.slider.maximumValue = Float(outputs[indexPath.row].max_value)
            cell.slider.minimumValue = Float(outputs[indexPath.row].min_value)
            cell.slider.value = Float(outputs[indexPath.row].value)
            
            cell.lbl_name.text = outputs[indexPath.row].name
            cell.slider.tag = indexPath.row
            
            return_cell = cell
        }
            
        } else if(sections[indexPath.section] == "sensors") {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "sensor") as! RoomSensorCellController
            
            
            if sensors_selected[indexPath.row] {
                cell.vw_chart.isHidden = false
            } else {
                cell.vw_chart.isHidden = true
            }
            
            cell.lbl_name.text = sensors[indexPath.row].name
            if(sensors[indexPath.row].type == "request") {
                cell.lbl_value.text = String(sensors[indexPath.row].value) + " " + sensors[indexPath.row].unit
            } else {
                if(sensors[indexPath.row].value < 30) {
                    cell.lbl_value.text = "pred 30 sekundami"
                } else if(sensors[indexPath.row].value < 60) {
                    cell.lbl_value.text = "pred minútou"
                } else if(sensors[indexPath.row].value < 120) {
                    cell.lbl_value.text = "pred 2 minútami"
                } else if(sensors[indexPath.row].value < 300) {
                    cell.lbl_value.text = "pred 5 minútami"
                } else if(sensors[indexPath.row].value < 900) {
                    cell.lbl_value.text = "pred 15 minútami"
                } else if(sensors[indexPath.row].value < 1800) {
                    cell.lbl_value.text = "pred 30 minútami"
                } else if(sensors[indexPath.row].value < 3600) {
                    cell.lbl_value.text = "pred hodinou"
                } else if(sensors[indexPath.row].value < 86400) {
                    cell.lbl_value.text = "dnes"
                } else if(sensors[indexPath.row].value < 172800) {
                    cell.lbl_value.text = "včera"
                } else {
                    cell.lbl_value.text = "viac ako 2 dni"
                }
                
            }
            cell.vw_chart.chartDescription?.text = ""
            cell.vw_chart.noDataText = "Niesu dostupné žiadne dáta"
            cell.vw_chart.backgroundColor = UIColor(red:255/255, green:255/255,blue:255/255,alpha:0)
            
            cell.vw_chart.xAxis.drawAxisLineEnabled = false
            cell.vw_chart.xAxis.drawLimitLinesBehindDataEnabled = false
            cell.vw_chart.xAxis.gridColor = UIColor(red:220/255, green:220/255,blue:220/255,alpha:1)
            cell.vw_chart.xAxis.gridLineWidth = 0.2
            cell.vw_chart.xAxis.drawGridLinesEnabled = true
            cell.vw_chart.xAxis.drawLabelsEnabled = true
            cell.vw_chart.xAxis.labelPosition = .bottomInside
            cell.vw_chart.xAxis.avoidFirstLastClippingEnabled = true
            
            if(sensors[indexPath.row].type == "request") {
                cell.vw_chart.leftAxis.axisMinimum = 10
                cell.vw_chart.leftAxis.axisMaximum = 30
            } else {
                cell.vw_chart.leftAxis.axisMinimum = 0
            }
            cell.vw_chart.leftAxis.removeAllLimitLines()
            cell.vw_chart.leftAxis.drawZeroLineEnabled = false
            cell.vw_chart.leftAxis.zeroLineWidth = 0
            cell.vw_chart.leftAxis.drawTopYLabelEntryEnabled = false
            cell.vw_chart.leftAxis.drawAxisLineEnabled = false
            cell.vw_chart.leftAxis.drawGridLinesEnabled = false
            cell.vw_chart.leftAxis.drawLabelsEnabled = false
            cell.vw_chart.leftAxis.drawLimitLinesBehindDataEnabled = false
            
            if(sensors[indexPath.row].type == "request") {
                cell.vw_chart.rightAxis.axisMinimum = 10
                cell.vw_chart.rightAxis.axisMaximum = 30
            } else {
                cell.vw_chart.rightAxis.axisMinimum = 0
            }
            cell.vw_chart.rightAxis.removeAllLimitLines()
            cell.vw_chart.rightAxis.drawZeroLineEnabled = false
            cell.vw_chart.rightAxis.drawTopYLabelEntryEnabled = true
            cell.vw_chart.rightAxis.drawAxisLineEnabled = false
            cell.vw_chart.rightAxis.drawGridLinesEnabled = false
            cell.vw_chart.rightAxis.drawLabelsEnabled = false
            cell.vw_chart.rightAxis.labelPosition = .insideChart
            cell.vw_chart.rightAxis.drawLimitLinesBehindDataEnabled = false
            cell.vw_chart.legend.enabled = false
            cell.vw_chart.minOffset = 0
            
            
            cell.vw_chart.scaleYEnabled = false
            cell.vw_chart.scaleXEnabled = false
            cell.vw_chart.pinchZoomEnabled = false
            cell.vw_chart.doubleTapToZoomEnabled = false
            
            cell.vw_chart.animate(yAxisDuration: 1.5, easingOption: .easeInOutQuart)
            
            let colors = [UIColor.red, UIColor.black, UIColor.blue, UIColor.brown, UIColor.cyan, UIColor.darkGray, UIColor.gray, UIColor.green, UIColor.lightGray, UIColor.magenta, UIColor.orange, UIColor.purple, UIColor.yellow]
            
            
            let gradColors = [colors[sensors[indexPath.row].color].cgColor, UIColor.clear.cgColor]
            let colorLocations:[CGFloat] = [0.0, 1.0]
            
            
            var dataEntries: [BarChartDataEntry] = []

            let date = Date()
            let calendar = Calendar.current
            let actual_hour = calendar.component(.hour, from: date)-1
            var xaxis = [String]()
            for i in 0 ..< cell.data.count {
                xaxis.append(String(((actual_hour + i < 24) ? (actual_hour + i) : (actual_hour + i-24))))
                let dataEntry = BarChartDataEntry(x: Double(i), y: Double(cell.data[i]))
                dataEntries.append(dataEntry)
            }
            
            cell.vw_chart.xAxis.valueFormatter = IndexAxisValueFormatter(values: xaxis)
            
            let chartDataSet = LineChartDataSet(values: dataEntries, label: "")
            chartDataSet.drawCirclesEnabled = false
            //chartDataSet.drawSteppedEnabled = true

            if(sensors[indexPath.row].type == "request") {
                chartDataSet.mode = .cubicBezier
            } else {
                chartDataSet.mode = .stepped
            }
            chartDataSet.drawFilledEnabled = true
            //chartDataSet.setDrawHighlightIndicators(false)
            
            chartDataSet.drawHorizontalHighlightIndicatorEnabled = false
            chartDataSet.highlightColor = UIColor(red:220/255, green:220/255,blue:220/255,alpha:1)
            chartDataSet.setColor(colors[sensors[indexPath.row].color].withAlphaComponent(0.5))
            chartDataSet.fillColor = NSUIColor(red: 255/255, green: 0/255, blue: 0/255, alpha: 1)
            
            
            //chartDataSet
            let chartData = LineChartData(dataSet: chartDataSet)
            
            if let gradient = CGGradient(colorsSpace: CGColorSpaceCreateDeviceRGB(), colors: gradColors as CFArray, locations: colorLocations) {
                chartDataSet.fill = Fill(linearGradient: gradient, angle: -90.0)
            }
            chartData.setDrawValues(false)
            chartData.setValueTextColor(NSUIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 1))
            cell.vw_chart.data = chartData
            return_cell = cell
            
        } else if (sections[indexPath.section] == "events") {
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "event") as! RoomEventCellController
            
            if cell.eventDelegate == nil {
                cell.eventDelegate = self
            }
            
            if(events[indexPath.row].active == 0) {
                cell.switch.setOn(false, animated: false)
            } else {
                cell.switch.setOn(true, animated: false)
            }
            
            cell.lbl_name.text = events[indexPath.row].name
            cell.switch.tag = events[indexPath.row].id
            
            return_cell = cell
            
        }
        
        
        return return_cell
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        if(sections[indexPath.section] == "sensors") {
            
            if (self.sensors_selected[indexPath.row]) {

                return selectedCellHeight
            }
        }
        return unselectedCellHeight
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if(sections[indexPath.section] == "sensors") {

            self.sensors_selected[indexPath.row] = !self.sensors_selected[indexPath.row]
            
            tableView.beginUpdates()
            tableView.endUpdates()
            
            if (self.sensors_selected[indexPath.row]) {
                // This ensures, that the cell is fully visible once expanded
                tableView.scrollToRow(at: indexPath, at: .none, animated: true)
            }
            
            SocketIOManager.sharedInstance.getTempGraph(id: self.sensors[indexPath.row].id, completionHandler: { (temps) in

                let cell = tableView.dequeueReusableCell(withIdentifier: "sensor", for: indexPath) as! RoomSensorCellController
                cell.vw_chart.isHidden = !self.sensors_selected[indexPath.row]
                cell.data = temps
                tableView.reloadRows(at: [indexPath], with: UITableViewRowAnimation.fade)
                
            })
        }
 
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return sections.count
    }
    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        var title = ""
        if(sections[section] == "sensors") {
            title = "Senzory"
        } else if(sections[section] == "outputs") {
            title = "Výstupy"
        } else if(sections[section] == "events") {
            title = "Udalosti"
        }
        
        return title
    }
    
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    }
    
    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        
        let deleteAction = UITableViewRowAction(style: .default, title: "Odstrániť") {action in
            
            self.view.isUserInteractionEnabled = false
            
            if(self.sections[indexPath.section] == "sensors") {
                
                SocketIOManager.sharedInstance.removeSensor(id: self.sensors[indexPath.row].id, completionHandler: { (ok) in
                    if(ok) {
                        //rooms.remove(at: self.sensors[indexPath.row].room_index)
                        self.sensors.remove(at: indexPath.row)
                        
                        var i = 0
                        for _ in self.sensors {
                            self.sensors[i].sensor_index = i
                            i += 1
                        }
                        
                        rooms[self.room_index].sensors = self.sensors
                        
                        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
                        self.view.isUserInteractionEnabled = true
                    }
                })
            
            } else if(self.sections[indexPath.section] == "outputs") {
                
                SocketIOManager.sharedInstance.removeOutput(id: self.outputs[indexPath.row].id, completionHandler: { (ok) in
                    if(ok) {
                        //rooms.remove(at: self.outputs[indexPath.row].room_index)
                        
                        self.outputs.remove(at: indexPath.row)
                        
                        var i = 0
                        for _ in self.outputs {
                            self.outputs[i].output_index = i
                            i += 1
                        }
                        
                        rooms[self.room_index].outputs = self.outputs
                        
                        tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.left)
                        self.view.isUserInteractionEnabled = true
                    }
                })
                
            } else if(self.sections[indexPath.section] == "events") {
                
                
                
            }
            
        }
        
        return [deleteAction]
    }
    
    
    
    
    
    internal func switchCellTapped(index: Int, value: Bool, completionHandler: @escaping (_ status: Bool) -> Void) {
        var value_int : Int
        if(value) {
            value_int = self.outputs[index].max_value
        } else {
            value_int = self.outputs[index].min_value
        }
        rooms[self.room_index].outputs[index].value = value_int
        SocketIOManager.sharedInstance.changeOutput(output: self.outputs[index], value: value_int) { (status) -> Void in
            completionHandler(status)
        }
    }
    
    internal func sliderCellTapped(index: Int, value: Int, completionHandler: @escaping (_ status: Bool) -> Void) {
        rooms[self.room_index].outputs[index].value = value

        SocketIOManager.sharedInstance.changeOutput(output: self.outputs[index], value: value) { (status) -> Void in
            completionHandler(status)
        }
    }
    
    internal func buttonCellTapped(index: Int, value: Int) {
        SocketIOManager.sharedInstance.changeOutput(output: self.outputs[index], value: value) { (messageInfo) -> Void in

        }
    }
    
    internal func colorCellTapped(index: Int, value: Int) {
        SocketIOManager.sharedInstance.changeOutput(output: self.outputs[index], value: value) { (messageInfo) -> Void in

        }
    }
    
    
    internal func eventCellTapped(id: Int, value: Bool, completionHandler: @escaping (_ status: Bool) -> Void) {
        var value_int : Int
        if(value) {
            value_int = 1
        } else {
            value_int = 0
        }
        SocketIOManager.sharedInstance.changeEventActivity(id: id, value: value_int) { (status) -> Void in
            completionHandler(status)
        }
        
    }
    
}
