from Rest.Answer import try_catch_error_answer, Answer, MessageCodes


@try_catch_error_answer
class DevicesREST:
    @staticmethod
    def set_device_active(user, device_id, device_name):
        answer = Answer()
        user.device_activity(device_id, device_name)
        return answer.add_message(MessageCodes.DEVICE_ACTIVE)

    @staticmethod
    def get_devices(user, device_id):
        answer = Answer()
        devices = user.devices
        if device_id:
            device = [device for device in devices if device.device_id == device_id]
            if not device:
                return answer.add_message(MessageCodes.DEVICE_DOES_NOT_EXIST)
            else:
                return answer.add_message(MessageCodes.WORKED).add_result(device)
        else:
            if not devices:
                return answer.add_message(MessageCodes.DEVICE_USER_HAS_NO_DEVICES)
            else:
                return answer.add_message(MessageCodes.WORKED).add_result(devices)
