import lxml

from . import get_version, utils


"""
# Class > dict > JSON
import json
from device_inventory import inventory

dev = inventory.Computer()
processor = inventory.Processor(dev.lshw_json)
json.dumps(processor.__dict__)

"""
def export_to_devicehub_schema(device, user_input=None, debug=False):
    if user_input is None:
        user_input = {}
    
    components = []
    for comp_name in device.COMPONENTS:
        comp = getattr(device, comp_name)
        
        # We could receive an array of components (e.g. HDDs)
        # Or only a component (e.g. motherboard)
        if not hasattr(comp, '__iter__'):
            comp = [comp]
        
        for item in comp:
            comp_data = utils.strip_null_or_empty_values(item.__dict__)
            comp_data.update({"@type": type(item).__name__})
            components.append(comp_data)
    
    device_serialized = utils.strip_null_or_empty_values({
        "@type": type(device).__name__,
        "type": device.type,
        "manufacturer": device.manufacturer,
        "model": device.model,
        "serialNumber": device.serialNumber,
    })
    
    snapshot = {
        "@type": "Snapshot",
        "device": device_serialized,
        "components": components,
        "version": get_version(),
    }
    
    # Include user's custom fields (e.g. label, comment)
    snapshot.update(user_input)
    
    # Include full output (debugging purposes)
    if debug:
        snapshot['debug'] = {
            "lshw": lxml.etree.tostring(device.lshw_xml),
            "dmi": device.dmi,
            #'smart": ,
        }
    
    return snapshot

@@ -1371,16 +1371,405 @@
 _input)%0A
+    # Move visual and functional state to more a structured format.%0A    state = %7B%7D%0A    visual_state = snapshot.pop('visualState')%0A    if visual_state:%0A        state%5B'visual'%5D = %7B'general': visualState%7D%0A    functional_state = snapshot.pop('functionalState')%0A    if functional_state:%0A        state%5B'functional'%5D = %7B'general': functionalState%7D%0A    if state:%0A        snapshot%5B'state'%5D = state%0A
     %0A   

Structure visual and functional states in snapshot