//
//  ViewController.swift
//  skateeApp
//
//  Created by Santiago Neira on 3/29/19.
//  Copyright © 2019 skatEE. All rights reserved.
//

import UIKit
import GoogleMaps

class ViewController: UIViewController {
    
    
    
    //Variables (Labels and Such):
    
    var appLattitude = 0.0;
    var appLongitude = 0.0;
    
    
    @IBOutlet weak var ledStatusLabel: UILabel!
    
    @IBOutlet weak var latLabel: UILabel!
    
    @IBOutlet weak var longLabel: UILabel!
    
    //var appLattitude;
    
    
    
    //----------------- Button Actions ------------------
    // Unlock Fucntion ************
    
    @IBAction func unlockServo(_ sender: Any) {
        
        //now try this
        var skateeElectron : ParticleDevice?
        ParticleCloud.sharedInstance().getDevices { (devices:[ParticleDevice]?, error:Error?) -> Void in
            if let _ = error {
                print("Check your internet connectivity")
            }
            else {
                if let d = devices {
                    for device in d {
                        if device.name == "skatEE" {
                            skateeElectron = device
                        }
                    }
                }
            }
            
            //unlock that ish
            //unlock Method
            let myArgs = ["unlock"]
            var task = skateeElectron!.callFunction("ServoBasic", withArguments: myArgs) { (resultCode : NSNumber?, error : Error?) -> Void in
                if (error == nil) {
                    print("Unlock the remote!")
                }
            }
        }
    }
    
    
    //** Lock Function*****************
    
    @IBAction func lockServo(_ sender: Any) {
        
        var skateeElectron : ParticleDevice?
        ParticleCloud.sharedInstance().getDevices { (devices:[ParticleDevice]?, error:Error?) -> Void in
            if let _ = error {
                print("Check your internet connectivity")
            }
            else {
                if let d = devices {
                    for device in d {
                        if device.name == "skatEE" {
                            skateeElectron = device
                        }
                    }
                }
            }
            //Still in the same instance, Lock part here
            //Lock Method
            let myArgs = ["lock"]
            var task = skateeElectron!.callFunction("ServoBasic", withArguments: myArgs) { (resultCode : NSNumber?, error : Error?) -> Void in
                if (error == nil) {
                    print("Lock the remote!")
                }
            }
            var bytesToReceive : Int64 = task.countOfBytesExpectedToReceive
        }
    }
    
    
    //************* read status-- servo ********************
    
    @IBAction func readServoStatus(_ sender: Any) {
        
        
         
        print("reading the board...");
        // defning the young instance
        var skateeElectron : ParticleDevice?
        ParticleCloud.sharedInstance().getDevices { (devices:[ParticleDevice]?, error:Error?) -> Void in
            if let _ = error {
                print("Check your internet connectivity")
            }
            else {
                if let d = devices {
                    for device in d {
                        if device.name == "skatEE" {
                            skateeElectron = device
                            //print(skateeElectron);
                        }
                    }
                }
            }
            //read method
            skateeElectron!.getVariable("servoStatus", completion: { (result:Any?, error:Error?) -> Void in
                if let _ = error {
                    print("Failed reading variable from device")
                }
                else {
                    if let boardStatus = result as? NSNumber {
                        if boardStatus == 0 {
                            print("status is: locked ");
                            self.ledStatusLabel.text = "locked";
                        }
                        else if boardStatus == 1 {
                            print("status is: unlocked ");
                            self.ledStatusLabel.text = "unlocked";
                        }
                    }
                }
            })
        }
 
        
    }

    
    
    //*************  get board location *********************
    
    
    @IBAction func locate(_ sender: Any) {
        var skateeElectron : ParticleDevice?
        ParticleCloud.sharedInstance().getDevices { (devices:[ParticleDevice]?, error:Error?) -> Void in
            if let _ = error {
                print("Check your internet connectivity")
            }
            else {
                if let d = devices {
                    for device in d {
                        if device.name == "skatEE" {
                            skateeElectron = device;
                        }
                    }
                }
            }
            
            
            //readLattidude method
            skateeElectron!.getVariable("boardLat", completion: { (result:Any?, error:Error?) -> Void in
                if let _ = error {
                    print("Failed reading variable from device")
                }
                else {
                    if let lat = result as? NSNumber {
                        self.appLattitude = Double(truncating: lat);
                        
                        self.latLabel.text = lat.stringValue;
                        //self.appLattitude = Int(truncating: lat);
                    }
                }
            })
            
            //readLongitude method
            skateeElectron!.getVariable("boardLong", completion: { (result:Any?, error:Error?) -> Void in
                if let _ = error {
                    print("Failed reading variable from device")
                }
                else {
                    if let long = result as? NSNumber {
                        self.appLongitude = Double(truncating: long);
                        self.longLabel.text = long.stringValue;
                        //self.appLongitude = Int(truncating: long);
                    }
                }
            })
            
            print("found device!!@")
            
    

        }
    }
    
    
    
    @IBAction func mapIt(_ sender: Any) {
        
        // You don't need to modify the default init(nibName:bundle:) method.
        
    
            // Create a GMSCameraPosition that tells the map to display the
            // coordinate -33.86,151.20 at zoom level 6.
            let camera = GMSCameraPosition.camera(withLatitude: self.appLattitude, longitude: self.appLongitude, zoom: 18.0)
            let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
            view = mapView
            
            // Creates a marker in the center of the map.
            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: self.appLattitude, longitude: self.appLongitude)
            marker.title = "current location"
            marker.snippet = ""
            marker.map = mapView
        
        
    }
    
    //------------- Button Actions END -----------------
 

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
    }

}
 


