from base_class import BaseVariableFunction
from base_class import *
old_print = print


def timestamped_print(*args, **kwargs):
    old_print(datetime.datetime.utcnow().replace(
        microsecond=0), *args, **kwargs)


print = timestamped_print
baseclass = BaseVariableFunction(__file__)

print('\n'*5)
print(f"{'{:<6}'.format('ENTER')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

 
arr_ori_contracttokentx_file = [file for file in os.listdir(baseclass.ori_contracttokentx_path) if file.endswith(".csv")]
 
 
for file in arr_ori_contracttokentx_file:
    tokenaddress = file.split("_")[0]
    if tokenaddress =="":
        continue
    if os.path.exists(baseclass.contracttokentx_path / f"{tokenaddress}.csv"):
        (baseclass.contracttokentx_path / f"{tokenaddress}.csv").unlink()
    
for file in arr_ori_contracttokentx_file:
    tokenaddress = file.split("_")[0]
    if tokenaddress =="":
        continue
    
    df =pd.read_csv(baseclass.ori_contracttokentx_path/f"{file}" , dtype=object)  
    df =df.astype({
        "blockNumber":int,
        "timeStamp":int, 
    })
    
    df =df[~ df["to"].isin( ["0x0000000000000000000000000000000000000000","0x000000000000000000000000000000000000dead"] )].reset_index(drop=True)
    
    
    
    if not os.path.exists(baseclass.contracttokentx_path / f"{tokenaddress}.csv" ):
        df.to_csv(
                 baseclass.contracttokentx_path / f"{tokenaddress}.csv",  mode='w', index=False)
    else:
        df.to_csv(
                baseclass.contracttokentx_path / f"{tokenaddress}.csv",  mode='a', header=False, index=False)
            

  
 
print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")

