import getpass


def complicate_data(raw_data, **kws):
    """Convert simple tuple PV data into CFS formatted data.

    Parameters
    ----------
    raw_data : list or list(list)
        Each list element is of the format:
        ``PV name (str), PV properties (dict), PV tags (list(str))``

    Keyword Arguments
    -----------------
    owner : str
        Owner of the data.

    Returns
    -------
    ret : list(dict)
        List of dict, each dict element is of the format:
        ``{'name': PV name (str), 'owner': str, 'properties': PV properties (list[dict]), 'tags': PV tags (list[dict])}``.

    See Also
    --------
    :func:`~phantasy.library.channelfinder.io.get_data_from_tb`
    :func:`~phantasy.library.channelfinder.io.get_data_from_db`
    :func:`~phantasy.library.channelfinder.io.get_data_from_cf`
    """
    owner = kws.get('owner', getpass.getuser())
    if isinstance(raw_data, list):
        pv_name, pv_props, pv_tags = raw_data
        retval = {'name': pv_name,
                  'owner': owner,
                  'properties': [{'name': k, 'value': v, 'owner': owner}
                                 for k, v in pv_props.items()],
                  'tags': [{'name': t, 'owner': owner} for t in pv_tags]
                  }
    else:
        retval = []
        for pv_name, pv_props, pv_tags in raw_data:
            new_rec = {'name': pv_name,
                       'owner': owner,
                       'properties': [{'name': k, 'value': v, 'owner': owner}
                                      for k, v in pv_props.items()],
                       'tags': [{'name': t, 'owner': owner} for t in pv_tags],
                       }
            retval.append(new_rec)
    return retval