import datetime
import echo_serial.serial_controller as EchoSerial
import echo_db.database as EchoDB
from bson import json_util, ObjectId
import json

import echo_controller.switch as Switch

class Sensor:

    @staticmethod
    def handle_action(device, action, data):
        actionData = device['actions'][action]

        # 
        # Climate Sensors
        # 

        if action == "gather_climate_data":
            EchoSerial.EchoSerial.send_commmand(device, action)
            return {'status': 'success', 'message': actionData['message']}, 200
        
        if action == "get_climate_data":
            collection = EchoDB.EchoDB.database['ClimateLogs']
            data = collection.find().sort([('timestamp', -1)]).limit(1)
            data_json = json.loads(json_util.dumps(data))
            return {'status': 'success', 'message': actionData['message'], 'data': data_json}, 200

        # 
        #  Light Sensors
        # 
        
        if action == "gather_light_data":
            EchoSerial.EchoSerial.send_commmand(device, action)
            return {'status': 'success', 'message': actionData['message']}, 200

        if action == "get_light_data":
            collection = EchoDB.EchoDB.database['LightLog']
            data = collection.find().sort([('timestamp', -1)]).limit(1)
            data_json = json.loads(json_util.dumps(data))
            return {'status': 'success', 'message': actionData['message'], 'data': data_json}, 200
        
        # 
        #  Movement Sensors
        # 

        if action == "get_movment_data":
            collection = EchoDB.EchoDB.database['MovementLog']
            data = collection.find().sort([('timestamp', -1)]).limit(20)
            data_json = json.loads(json_util.dumps(data))
            return {'status': 'success', 'message': actionData['message'], 'data': data_json}, 200
        
    @staticmethod
    def handle_data(device, action, data):
        # 
        # Climate Sensors
        # 

        if action == 'climate_data':
            collection = EchoDB.EchoDB.database['ClimateLogs']
            climate_data = data[2].split(',')
            
            logData = {
                'uuid': device['uuid'],
                'temperature': float(climate_data[0]),
                'humidity': float(climate_data[1]),
                'timestamp': datetime.datetime.now()
            }

            collection.insert(logData)
        
        # 
        # Light Sensors
        # 
        
        if action == 'light_data':
            collection = EchoDB.EchoDB.database['LightLog']
            lightAmount = data[2]
            
            logData = {
                'uuid': device['uuid'],
                'light_amount': int(lightAmount),
                'timestamp': datetime.datetime.now()
            }

            collection.insert(logData)
        
        # 
        # Movement Sensors
        # 
        
        if action == 'movement_data':
            collection = EchoDB.EchoDB.database['MovementLog']
            typeMovement = data[2]
            
            logData = {
                'uuid': device['uuid'],
                'type_movement': typeMovement,
                'timestamp': datetime.datetime.now()
            }

            collection.insert(logData)

            # Handle Automatic Lights
            Switch.Switch.handle_movement_switch()
