//
//  TermostatViewController.swift
//  Home
//
//  Created by Richard Kuckovsky on 6.5.17.
//  Copyright © 2017 Richard Kuckovsky. All rights reserved.
//

import UIKit
import GLKit

class TermostatViewController: UIViewController {
    
    var output = outputsType()
    var indexPath = NSIndexPath()
    var origin = ""
    
    @IBOutlet weak var temp_graph: TermostatView!
    @IBOutlet weak var lbl_temp: UILabel!
    @IBOutlet weak var lbl_name: UILabel!
    @IBOutlet weak var btn_power: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        lbl_name.text = output.name
        lbl_temp.text = String(describing: output.temp) + "°"
        if(output.value == 0) {
            btn_power.tintColor = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1)
        } else {
            btn_power.tintColor = UIColor(red: 255/255, green: 100/255, blue: 100/255, alpha: 1)
        }
        
        temp_graph.max_i = Int(((output.temp - 15.0) * 4) + 1)
        temp_graph.setNeedsDisplay()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func close(_ sender: Any) {
        
        if(origin == "room") {
            
            if let presenter = presentingViewController as? UINavigationController {
                if let room = presenter.viewControllers[1] as? RoomController {
                    room.outputs[indexPath.row] = output
                    
                    room.tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)
                }
            }
            
            SocketIOManager.sharedInstance.changeOutput(output: output, value: output.value) { (status) -> Void in
                self.dismiss(animated: true)
            }
            
        } else if(origin == "set_time_event") {
            
            if let presenter = presentingViewController as? UINavigationController {
                if let destination = presenter.viewControllers[0] as? AddTimeEventViewController {
                    
                    if(output.value == 1) {
                        destination.output_img_temp.tintColor = UIColor(red: 1, green: 100/255, blue: 100/255, alpha: 1)
                    } else {
                        destination.output_img_temp.tintColor = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1)
                    }
                    
                    destination.output_lbl_temp.text = String(describing: output.temp) + "°C"
                    
                    //destination.tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)
                }
            }
            
        } else if(origin == "favourite") {
            
            if let presenter = presentingViewController as? UINavigationController {
                if let room = presenter.viewControllers[0] as? FavouriteTableController {
                    room.outputs[indexPath.row] = output
                    
                    room.tableView.reloadRows(at: [indexPath as IndexPath], with: UITableViewRowAnimation.fade)
                }
            }
            print(output)
            SocketIOManager.sharedInstance.changeOutput(output: output, value: output.value) { (status) -> Void in
                self.dismiss(animated: true)
            }
            
        }
    }
 
    @IBAction func minus(_ sender: Any) {
        if(output.temp > 15) {
            output.temp -= 0.5
        }
        lbl_temp.text = String(describing: output.temp) + "°"
        temp_graph.max_i = Int(((output.temp - 15.0) * 4) + 1)
        temp_graph.setNeedsDisplay()
    }
    @IBAction func plus(_ sender: Any) {
        if(output.temp < 30) {
            output.temp += 0.5
        }
        lbl_temp.text = String(describing: output.temp) + "°"
        temp_graph.max_i = Int(((output.temp - 15.0) * 4) + 1)
        temp_graph.setNeedsDisplay()
    }
    
    @IBAction func power(_ sender: Any) {
        if(output.value == 0) {
            output.value = 1
            btn_power.tintColor = UIColor(red: 255/255, green: 100/255, blue: 100/255, alpha: 1)
        } else {
            output.value = 0
            btn_power.tintColor = UIColor(red: 85/255, green: 85/255, blue: 85/255, alpha: 1)
        }
        
        
    }

}
@IBDesignable
class TermostatView: UIView {
    
    let π:CGFloat = .pi
    var max_i:Int = Int(((15.0-15.0) * 4) + 1)
    
    override func draw(_ rect: CGRect) {
        
        let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
        var radius: CGFloat = max(bounds.width, bounds.height)
        var arcWidth: CGFloat
        let startAngle: CGFloat = 3 * π / 4
        let endAngle: CGFloat = π / 4
        
        for i in 0 ..< max_i {
            
            if (i % 10) == 0 {
                arcWidth = 10
                radius = max(bounds.width, bounds.height)
            } else {
                arcWidth = 5
                radius = max(bounds.width-10, bounds.height-10)
            }

            
            let start = startAngle + CGFloat(i) * ((startAngle - endAngle) / 20)
            let end = start + 0.03
            
            let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: start, endAngle: end, clockwise: true)
            path.lineWidth = arcWidth
            UIColor(red: 1, green: CGFloat(CGFloat(61-i)/71), blue: CGFloat(CGFloat(61-i)/71), alpha: 1).setStroke()
            path.stroke()
            
        }
        
    }
    
}
