
from base_class import BaseVariableFunction
from base_class import *
baseclass = BaseVariableFunction(__file__)

baseclass.makedirpath(baseclass.dalao_pnl_gmgn_path)

old_print = print
 

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


print = timestamped_print
print('\n'*5)

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

def makescreenurl( urlname,urlparam):
    _url = urlname + urlparam
    return '=HYPERLINK("{}","{}")'.format(_url, _url)


# weth 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
# USDT 0xdac17f958d2ee523a2206206994597c13d831ec7
# USDC 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48
def get_temp_dalao_pnl(temp_df ,str_dalaoaddress ):
    
    temp_df["isnengen"] = -1
    temp_df["beizhu"] = -1
    temp_df["definedurl"] = temp_df.apply(lambda ser: makescreenurl("https://www.defined.fi/sol/" ,f"{ser['tokenaddress']}?maker={str_dalaoaddress}")  , axis=1)
    temp_df["dexurl"] = temp_df.apply(lambda ser: makescreenurl( f"https://dexscreener.com/solana/",f"{ser['tokenaddress']}?maker={str_dalaoaddress}")  , axis=1)
    


    temp_df.to_excel(baseclass.dalao_pnl_gmgn_path / f"temp_gmgn_pnl_{str_dalaoaddress}.xlsx", index=False )
    
    
arr_exclude_tokenaddress = [
                        "0x0000000000000000000000000000000000000000",
                        "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" ,
                        "0xdac17f958d2ee523a2206206994597c13d831ec7",
                        "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"]

def hanle_dalao_ori_pnl_gmgn(arr_str_dalaoaddress):
        print("enter hanle_dalao_ori_pnl_gmgn")
        
        for str_dalaoaddress in arr_str_dalaoaddress:
                try:
                        if not (baseclass.dalao_ori_pnl_gmgn_path / f"ori_gmgn_pnl_{str_dalaoaddress}.csv").exists():
                                continue
                        
                        print(f"hanle_dalao_ori_pnl_gmgn str_dalaoaddress= {str_dalaoaddress}")
                        dalao_df = pd.read_csv(baseclass.dalao_ori_pnl_gmgn_path /
                                f"ori_gmgn_pnl_{str_dalaoaddress }.csv", dtype= object)
                        

                        dalao_df  =dalao_df[~ dalao_df["tokenaddress"].isin( arr_exclude_tokenaddress )].reset_index(drop=True)
                        
                        dalao_df.to_csv(baseclass.dalao_ori_pnl_gmgn_path /
                                f"ori_gmgn_pnl_{str_dalaoaddress }.csv", index=False)
                        
                        
                        arr_last_active = dalao_df["last_active"].tolist()
                        for idx , str_last_active in enumerate(arr_last_active ):
                                if "min"   in str_last_active  or  "h" in str_last_active or  "s" in str_last_active:
                                        arr_last_active[idx] = "0"
                                elif "d" in str_last_active:
                                        arr_last_active[idx] = str_last_active.replace("d","")
                                dalao_df["last_active"] = arr_last_active
                        dalao_df['30d_txns_buy'] = dalao_df['30d_txns_buy'].str.replace(',', '')
                        dalao_df['30d_txns_sell'] = dalao_df['30d_txns_sell'].str.replace(',', '')
                        dalao_df['profit_percent'] = dalao_df['profit_percent'].str.replace('>', '')
                        dalao_df['30d_txns_buy'] = dalao_df['30d_txns_buy'].str.replace("K", '000', regex=True)
                        dalao_df['30d_txns_sell'] = dalao_df['30d_txns_sell'].str.replace("K", '000', regex=True)
                        dalao_df['30d_txns_buy'] = dalao_df['30d_txns_buy'].str.replace(r'[,\.]', '', regex=True)
                        dalao_df['30d_txns_sell'] = dalao_df['30d_txns_sell'].str.replace(r'[,\.]', '', regex=True)
        
                        dalao_df = dalao_df.astype({
                                "last_active" :  int ,
                                "profit_eth" :  float ,
                                "profit_percent" :  float ,
                                "buy_eth" :  float ,
                                "sell_eth" : float  ,
                                "30d_txns_buy" :  int ,
                                "30d_txns_sell":  int ,
                        
                        })
        
        
                        # dalao_df=dalao_df[dalao_df["profit_eth"] !=0].reset_index(drop=True)
                        if len(dalao_df)==0:
                                continue
                        
                        temp_df = dalao_df.copy()
                        get_temp_dalao_pnl(temp_df=temp_df ,str_dalaoaddress=str_dalaoaddress)
                        
                        dalao_df.to_csv( baseclass.dalao_pnl_gmgn_path / f"gmgn_pnl_{str_dalaoaddress}.csv",index  = False )
                        
                        
        
             
                except Exception as e:
                        print(f"str_dalaoaddress= {str_dalaoaddress}",e)
                        raise 
        
        

arr_file =os.listdir(baseclass.dalao_ori_pnl_gmgn_path )


df =  pd.read_excel( baseclass.dalao_merge_path / f"merge.xlsx", dtype=object)
arr_str_dalaoaddress = df['dalaoaddress'].drop_duplicates().tolist()

hanle_dalao_ori_pnl_gmgn(arr_str_dalaoaddress=arr_str_dalaoaddress)

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

