//
//  AddFavouriteController.swift
//  Home
//
//  Created by Richard Kuckovsky on 22.4.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit

class AddFavouriteCellController: UITableViewCell {
    
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var lbl_room: UILabel!
}

class AddFavouriteController: UITableViewController {
    
    let shared = UserDefaults(suiteName: "group.kuckovsky.home.key")

    @IBAction func done_action(_ sender: Any) {
        self.dismiss(animated: true)
        shared?.set(favourite_outputs, forKey: "favourite_outputs_array")
        shared?.set(favourite_sensors, forKey: "favourite_sensors_array")
        shared?.synchronize()
        
    }
    
    var outputs : [outputsType] = []
    var sensors : [sensorsType] = []
    var sections = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        for room in rooms {
            self.sensors += room.sensors
            self.outputs += room.outputs
        }
        
        sections.removeAll()
        if(sensors.count > 0) {
            sections.append("sensors")
        }
        if(outputs.count > 0) {
            sections.append("outputs")
        }
    }

    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 sections.count
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        if(sections[section] == "sensors") {
            return sensors.count
        } else if(sections[section] == "outputs") {
            return outputs.count
        }
        return 0
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AddFavouriteCellController
        
        if (sections[indexPath.section] == "sensors") {
            
            cell.lbl_name.text = sensors[indexPath.row].name
            cell.lbl_room.text = rooms[sensors[indexPath.row].room_index].name
            
            if(favourite_sensors.contains(sensors[indexPath.row].id)) {
                cell.accessoryType = UITableViewCellAccessoryType.checkmark
            } else {
                cell.accessoryType = UITableViewCellAccessoryType.none
            }
            
        } else if(sections[indexPath.section] == "outputs") {
            
            cell.lbl_name.text = outputs[indexPath.row].name
            cell.lbl_room.text = rooms[outputs[indexPath.row].room_index].name
            
            if(favourite_outputs.contains(outputs[indexPath.row].id)) {
                cell.accessoryType = UITableViewCellAccessoryType.checkmark
            } else {
                cell.accessoryType = UITableViewCellAccessoryType.none
            }
            
        }
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = tableView.cellForRow(at: indexPath)
        
        if (sections[indexPath.section] == "sensors") {
            
            if(favourite_sensors.contains(sensors[indexPath.row].id)) {
                favourite_sensors.remove(at: favourite_sensors.index(of: sensors[indexPath.row].id)!)
                cell?.accessoryType = UITableViewCellAccessoryType.none
            } else {
                favourite_sensors.append(sensors[indexPath.row].id)
                cell?.accessoryType = UITableViewCellAccessoryType.checkmark
            }
            
        } else if(sections[indexPath.section] == "outputs") {
            
            if(favourite_outputs.contains(outputs[indexPath.row].id)) {
                favourite_outputs.remove(at: favourite_outputs.index(of: outputs[indexPath.row].id)!)
                cell?.accessoryType = UITableViewCellAccessoryType.none
            } else {
                favourite_outputs.append(outputs[indexPath.row].id)
                cell?.accessoryType = UITableViewCellAccessoryType.checkmark
            }
            
        }
    }
    
    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"
        }
        
        return title
    }

    

}
