
from time import time
from typing import Union


def getItemFullName(shortItem:str)->str:
    # def searchitem(SearchItem = '001aba', QTY = 0, dbTable="itemlist", column="FullName"):
    FullName = shortItem
    return FullName

def getCustomerFullName(shortCustomerName:str)->str:
    # def searchitem(SearchItem = '001aba', QTY = 0, dbTable="itemlist", column="FullName"):
    FullName = shortCustomerName
    return FullName

def getRefNumber()->str:
    return "refnumber345"

def prepareData(dt:str):
    dtLines = dt.strip().split('\n')
    smoothdtLines:list[str] = []
    for dtLine in dtLines:
        if dtLine.strip()=='':
            continue
        smoothdtLines.append(dtLine)
    for smIdx, smdtLine in enumerate(smoothdtLines):
        itemName, Qty, memoLine = ('', '', '')
        if smIdx==0 and ':' in smdtLine:
            CustomerName = smdtLine.split(':')[1].strip()
            CustomerRef_FullName = getCustomerFullName(CustomerName)
            RefNumber = getRefNumber()
            continue
        elif smIdx==0 and ':' not in smdtLine:
            return False
        else: #proccess the second till end lines
            for splitChar in [':', '=']:
                if splitChar in smdtLine:
                    objects = smdtLine.split(splitChar)
                    if len(objects)==2:
                        itemName, Qty = tuple(smdtLine.split(splitChar))
                    if len(objects)==3:
                        itemName, Qty, memoLine = tuple(smdtLine.split(splitChar))
                    break
        itemName = itemName.strip()
        Qty=Qty.strip()
        memoLine = memoLine.strip()
        print(CustomerRef_FullName, RefNumber, itemName, Qty, memoLine)
        ItemRef_FullName = getItemFullName(itemName)

        if memoLine:
            print(memoLine)
        else:
            print("no memo")


    


def makeLinkedTxnList(x:list, key:str=None)->list:
    txnIDs=[]
    if isinstance(x, dict):
        x=[x]
    print(f'{type(x)=}')
    for y in x: #y = each dict of LinkedTxn, and it has a listofdict or a dict inside
        linkedTxns=y.get('LinkedTxn',None)
        if linkedTxns is None:
            return
        if isinstance(linkedTxns, dict):
            linkedTxns = [linkedTxns]
        for linkedTxn in linkedTxns:
            # print(f'{key = }')
            if key and linkedTxn.get('TxnType')==key:
                txnIDs.append(linkedTxn.get('TxnID',None))
    print(f'{txnIDs = }')
    return txnIDs
            
def makeAList(x:Union[str , dict , list], dictvalue:bool=False, listofdict:bool=False)->list :
    if isinstance(x,str):
        x=[x]
    elif isinstance(x, list) and not listofdict: #lists of str or int values
        pass
    elif isinstance(x, list) and not dictvalue: #lists of dict, and get all the keys
        res=[]
        for z in x:
            for k in z:
                res.append(k)
        return res
    elif isinstance(x, list) and dictvalue: #lists of dict, and get all the values
        res=[]
        for z in x:
            for k,v in z.items():
                res.append(v)
        return res
    elif isinstance(x, list): #lists of str or int values
        res=[]
        for z in x:
            for k in z:
                res.append(k)
        return res
    elif isinstance(x, dict) and not dictvalue: #get a list of keys from dict
        x=[k for k in x]
    elif isinstance(x, dict):   #get a list of values from dict
        x=[v for k,v in x.items()]
    
    return x

def timing(f):
    # @wraps(f)
    def wrap(*args, **kw):
        ts = time()
        result = f(*args, **kw)
        te = time()
        print('func:%r args:[%r, %r] took: %2.6f sec' % \
          (f.__name__, args, kw, te-ts))
        return result
    return wrap


def cleanIncludeRetElements(includeRetElements_allowed:list, includeRetElements:Union[list, str], default_val:str=None):
    if default_val==None:
        default_val = []
    # else:
    #     default = [default]
    iREs = []
    # print(f'{includeRetElements_allowed = }\n{includeRetElements = }')
    if isinstance(includeRetElements, str): #if user put 1 str argument in IncludeRetElements, change it to list
        includeRetElements = [includeRetElements]
    for iRE in includeRetElements:
        for iRE_a in includeRetElements_allowed:
            if iRE.lower() == iRE_a.lower():
                iREs.append(iRE_a)
                break
    if len(iREs)>0:
        return iREs
    else:
        return [default_val]

if __name__=='__main__':
    #Example makelist
    print(makeAList("hallo"), makeAList({'world':1, 'case':'another'}), makeAList(['here', 'we', 'go']), makeAList(1), makeAList({'world':1, 'case':'another'}, True))
    alistofdict = [{'RefNumber': '0001'}, {'RefNumber': '0022'}, {'RefNumber': '0004'}, {'RefNumber': '0006'}, {'RefNumber': '0008'}, {'RefNumber': '0009'}, {'RefNumber': '0010'}, {'RefNumber': '0015'}, {'RefNumber': '0065'}, {'RefNumber': '0017'}, {'RefNumber': '0019'}, {'RefNumber': '0023'}, {'RefNumber': '0024'}, {'RefNumber': '0025'}, {'RefNumber': '0027'}, {'RefNumber': '0029'}, {'RefNumber': '0030'}, {'RefNumber': '0045'}, {'RefNumber': '0032'}, {'RefNumber': '0034'}, {'RefNumber': '0035'}, {'RefNumber': '0036'}, {'RefNumber': '0040'}, {'RefNumber': '0003'}, {'RefNumber': '0031'}, {'RefNumber': '0033'}, {'RefNumber': '0037'}, {'RefNumber': '0038'}, {'RefNumber': '0039'}, {'RefNumber': '0047'}, {'RefNumber': '0005'}, {'RefNumber': '0007'}, {'RefNumber': '0012'}, {'RefNumber': '0013'}, {'RefNumber': '0014'}, {'RefNumber': '0016'}, {'RefNumber': '0011'}, {'RefNumber': '0021'}, {'RefNumber': '0002'}, {'RefNumber': '0018'}, {'RefNumber': '0020'}, {'RefNumber': '0026'}, {'RefNumber': '0028'}, {'RefNumber': '0041'}, {'RefNumber': '0051'}, {'RefNumber': '0062'}, {'RefNumber': '0042'}, {'RefNumber': '0043'}, {'RefNumber': '0053'}, {'RefNumber': '0055'}, {'RefNumber': '0057'}, {'RefNumber': '0058'}, {'RefNumber': '0059'}, {'RefNumber': '0061'}, {'RefNumber': '0068'}, {'RefNumber': '0044'}, {'RefNumber': '0046'}, {'RefNumber': '0048'}, {'RefNumber': '0049'}, {'RefNumber': '0050'}, {'RefNumber': '0086'}, {'RefNumber': '0054'}, {'RefNumber': '0056'}, {'RefNumber': '0060'}, {'RefNumber': '0052'}, {'RefNumber': '0064'}, {'RefNumber': '0066'}, {'RefNumber': '0070'}, {'RefNumber': '0071'}, {'RefNumber': '0072'}, {'RefNumber': '0075'}, {'RefNumber': '0076'}, {'RefNumber': '0078'}, {'RefNumber': '0080'}, {'RefNumber': '0081'}, {'RefNumber': '0085'}, {'RefNumber': '0067'}, {'RefNumber': '0069'}, {'RefNumber': '0073'}, {'RefNumber': '0074'}, {'RefNumber': '0077'}, {'RefNumber': '0079'}, {'RefNumber': '0082'}, {'RefNumber': '0083'}, {'RefNumber': '0084'}, {'RefNumber': '0087'}, {'RefNumber': '0089'}, {'RefNumber': '0091'}, {'RefNumber': '0092'}, {'RefNumber': '0093'}, {'RefNumber': '0094'}, {'RefNumber': '0095'}, {'RefNumber': '0096'}, {'RefNumber': '0167'}, {'RefNumber': '0088'}, {'RefNumber': '0097'}, {'RefNumber': '0098'}, {'RefNumber': '0099'}, {'RefNumber': '0100'}, {'RefNumber': '0103'}]
    print(makeAList(alistofdict,listofdict=True, dictvalue=True))