from .core import _get, _micro2dt, _dt2str

def reformat_asset_info(asset_info: dict) -> dict:

    # Things to remove. Generally empty or do not provide meaningful information.
    del asset_info['@class']
    del asset_info['dataSource']
    del asset_info['calibration']
    del asset_info['physicalInfo']
    del asset_info['events']
    del asset_info['remoteResources']
    del asset_info['ooiPropertyNumber']
    del asset_info['ooiPartNumber']
    del asset_info['ooiSerialNumber']
    del asset_info['deliveryOrderNumber']
    del asset_info['institutionPropertyNumber']
    del asset_info['institutionPurchaseOrderNumber']
    del asset_info['shelfLifeExpirationDate']
    del asset_info['powerRequirements']
    del asset_info['firmwareVersion']
    del asset_info['softwareVersion']
    del asset_info['location']
    del asset_info['owner']
    del asset_info['depthRating']

    asset_info['purchase_date'] = _dt2str(_micro2dt(asset_info.pop('purchaseDate')))
    asset_info['delivery_date'] = _dt2str(_micro2dt(asset_info.pop('deliveryDate')))
    asset_info['asset_type'] = asset_info.pop('assetType')
    asset_info['serial_number'] = asset_info.pop('serialNumber')
    asset_info['model'] = asset_info.pop('modelNumber')
    asset_info['purchase_price'] = int(asset_info.pop('purchasePrice'))
    asset_info['inventory_status'] = asset_info.pop('editPhase')
    asset_info['asset_id'] = asset_info.pop('assetId')
    asset_info['last_updated'] = _dt2str(_micro2dt(asset_info.pop('lastModifiedTimestamp')))
    asset_info['asset_name'] = asset_info.pop('name')

    return asset_info


def sort_asset(asset_info: dict) -> dict:
    order = ['uid','asset_id','asset_name','asset_type','manufacturer','model', 'serial_number','description',
             'notes','status','mobile','last_updated','purchase_date','delivery_date','purchase_price']
    asset_info = {key: asset_info[key] for key in order if key in asset_info.keys()}
    return asset_info


def get_asset_info(uid: str) -> dict:
    asset_url = 'https://ooinet.oceanobservatories.org/api/m2m/12587/asset'
    params = {'uid': uid}
    json_data = _get(asset_url, params=params)
    asset_info = reformat_asset_info(json_data)
    asset_info = sort_asset(asset_info)
    return asset_info
