
# from datetime import datetime, timezone
# from playwright.async_api import async_playwright, Playwright
# from playwright.sync_api import expect
from playwright.async_api import Playwright, async_playwright, expect
import random
import time
import datetime
import asyncio
import pathlib
import shutil

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

baseclass.makedirpath(baseclass.dalao_activities_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')}  ----------------NOTE-----------NOTE---------------")


def makedirpath(   folder):
    pathlib.Path(folder).mkdir(exist_ok=True)

def rmfolder(  folder):
    if pathlib.Path(folder).exists():
        shutil.rmtree(folder)


activities_columns = ['tokenaddress', 'tokenname',  'Last Active', 
                         
                      'Profit',   'Profit_percent', 
                    'buy_usd', 
                        'sell_usd',   '30d_txns_buy','30d_txns_sell',  ]


# [
#     'txhash', 'date', 'timestamp', 'action', 'from', 'in_tokenaddress',
#                       'out_tokenaddress',
#                       'in_tokenname',
#                       'out_tokenname',
#                       'in_tokenamount',
#                       'out_tokenamount',
#                       'router',
#                       'israydium',
#                       ]


df =  pd.read_excel( baseclass.dalao_merge_path / f"merge.xlsx", dtype=object)

arr_str_dalaoaddress = df['dalaoaddress'].drop_duplicates().tolist()


 
temp_arr_dalaoaddress = copy.deepcopy(arr_str_dalaoaddress)
for str_dalaoaddress in temp_arr_dalaoaddress:
    if (baseclass.dalao_activities_solscan_path /
            f"gmgn_act_{str_dalaoaddress}.csv").exists():
        arr_str_dalaoaddress.remove(str_dalaoaddress)
        
arr_str_dalaoUrl = [
f"https://gmgn.ai/sol/address/{str_dalaoaddress}" for str_dalaoaddress in arr_str_dalaoaddress]

print('arr_str_dalaoaddress', len(arr_str_dalaoaddress))
driver_len = 1 if len(arr_str_dalaoaddress) >= 1 else len(
    arr_str_dalaoaddress)
arr_driver = [None]*driver_len
arr_wait = [None]*driver_len
arr_home_handle = [None]*driver_len
arr_split_str_dalaoaddress = [None]*driver_len
arr_split_str_dalaoSwapUrl = [None]*driver_len
len_split_str_dalaoaddress = int(len(arr_str_dalaoaddress)/driver_len)+1

for idx_driver in range(0, driver_len):
    arr_split_str_dalaoaddress[idx_driver] = arr_str_dalaoaddress[idx_driver *
                                                                  len_split_str_dalaoaddress: (idx_driver+1)*len_split_str_dalaoaddress]
    arr_split_str_dalaoSwapUrl[idx_driver] = arr_str_dalaoUrl[idx_driver *
                                                                  len_split_str_dalaoaddress: (idx_driver+1)*len_split_str_dalaoaddress]


set_str_dalaoaddress = set(arr_str_dalaoaddress)




async def do_some_thing(playwright, cur_arr_str_dalaoaddress, cur_arr_str_dalaoUrl):


    browser = await playwright.firefox.launch(headless=False)
    # context = await playwright.chromium.launch_persistent_context(user_data_dir=USER_DIR_PATH, accept_downloads=True, headless=False)
    # context = await playwright.firefox.launch_persistent_context(user_data_dir=USER_DIR_PATH , headless=False)
    context =await browser.new_context()
    context.set_default_timeout(19000)
    # context = await browser.new_context()
    arr_page = [None]*2
    # arr_page[0] = await context.new_page()
    # # page = arr_page[0]
    # await arr_page[0].goto(
    #     "https://gmgn.ai/" , timeout=100000)
   
    
     # global split_len
    global set_str_dalaoaddress
    # cur_set_str_dalaoaddress = set(arr_str_dalaoaddress)
    arr_str_dalaoaddress = cur_arr_str_dalaoaddress
    arr_str_dalaoUrl = cur_arr_str_dalaoUrl
    
    for str_dalaoaddress_idx in range(0, len(cur_arr_str_dalaoaddress)):

        str_dalaoaddress = arr_str_dalaoaddress[str_dalaoaddress_idx]
        str_dalaoUrl = arr_str_dalaoUrl[str_dalaoaddress_idx]
        print(f"enter str_dalaoaddress={str_dalaoaddress}")
        arr_page[0] = await context.new_page()
        await arr_page[0].goto(str_dalaoUrl, timeout=18000)
        await asyncio.sleep(1)
        page = None
        for item_page in context.pages:
            if item_page.url == str_dalaoUrl:
                await item_page.bring_to_front()
                page = item_page
                break

        # res_df = pd.DataFrame(columns=activities_columns)
        prev_page_height = await page.evaluate(
            "document.documentElement.scrollHeight")
        while True:
            isfull = await check_tbody_isfull(page=page , str_dalaoaddress=str_dalaoaddress)
            if isfull:
                break
            await page.evaluate(
                "window.scrollTo(0, document.body.scrollHeight);")
            await asyncio.sleep(1)
            await page.evaluate(
                "window.scrollTo(0, document.body.scrollHeight);")
            await asyncio.sleep(1)
            cur_page_height = await page.evaluate(
                "document.documentElement.scrollHeight")
            print(f"prev_page_height={prev_page_height} cur_page_height={cur_page_height}  ")
            if cur_page_height > prev_page_height:
                prev_page_height = cur_page_height
            elif cur_page_height == prev_page_height:
                break
        tbody_data = None
        while True:
            try:
                tbody_data = await get_tbody_data(page=page, str_dalaoaddress=str_dalaoaddress)

            except Exception as e:
                 
                print(traceback.format_exc())
                await asyncio.sleep(2)
                raise
                continue
            if tbody_data is None:
                await asyncio.sleep(0.8)
                raise("tbody_data is None")
                # continue
            else:
                break
        dalao_df = pd.DataFrame(
            tbody_data, columns=activities_columns)
        dalao_df.to_csv(baseclass.dalao_activities_gmgn_path /
                f"gmgn_pnl_{str_dalaoaddress}.csv", index=False)
    
        set_str_dalaoaddress.discard(str_dalaoaddress)
        await arr_page[0].close()
        
    # await page.wait_for_timeout(3000)
    # time.sleep(5.5)
    # await page.close()
    await asyncio.sleep(3)
    await context.close()
    await browser.close()

async def check_tbody_isfull(page ,str_dalaoaddress):
    table = page.locator('div[aria-labelledby="tabs-leftTabs--tab-0"]')
    tbody_ = table.locator(
        'tbody')
    arr_trs = await tbody_.locator('tr').all()
    print( f"{str_dalaoaddress} arr_trs len =", len(arr_trs))
    if len(arr_trs)>=300:
        return True
    if len(arr_trs)==0:
        return False

    last_tr = arr_trs[-1]
    
    arr_tds = await last_tr.locator('td').all()
    
    p_token_last_active =  arr_tds[0].locator("p")
    str_token_last_active =( await p_token_last_active.text_content()).strip()
    if "--"   in str_token_last_active :
        return True
    
    if "d" not in str_token_last_active :
        return False
    str_token_last_active=str_token_last_active.replace("d","")
    number_token_last_active =  int(str_token_last_active)
    print( f" {str_dalaoaddress} number_token_last_active =",   (number_token_last_active))
    if number_token_last_active>30:
        return True
    else :
        return False
     
        
 
async def get_tbody_data(page, str_dalaoaddress):
    arr_tokenlink = None
    listen_tr_ = None
    listen_td_ = None
   
    try:
        print(f"enter {str_dalaoaddress} get_tbody_data")
        table = page.locator('div[aria-labelledby="tabs-leftTabs--tab-0"]')
        tbody_ = table.locator( 'tbody')        
        
    
        await asyncio.sleep(3)
        arr_trs = await tbody_.locator('tr').all()
        arr_trs_datares = []
        for idx_tr_ in range(0, len(arr_trs)):
            if(idx_tr_ %20==0):
                print(f"{str_dalaoaddress} {idx_tr_}")
            arr_tds_datares = [None]*9
            arr_trs_datares.append(arr_tds_datares)
            tr_ = arr_trs[idx_tr_]
            listen_tr_ = tr_
            arr_tds = await tr_.locator('td').all()
            # 共有10列 
            # 'Token/Last Active', 'Unrealized',  '30D Realized Profit', 
            #           'Total Profit',  'Balance',   
            #           'Position %',  'Bought', 
            #             'Sold',   '30D TXs','Ahare'
                        
            for idx_td_ in range(0, len(arr_tds)):
                td_ = arr_tds[idx_td_]
                listen_td_ =td_
                 
                if (idx_td_ == 0):
                    # print((await td_.text_content()))
                   
                    arr_tokenlink = await td_.locator("a").all()
                    href_tokenaddress_name = await  arr_tokenlink[0].get_attribute('href')
                    href_tokenaddress_name = href_tokenaddress_name.strip().split(
                        '/token/')[-1]
                    arr_tokenaddress_name = href_tokenaddress_name.split("?symbol=")
                    str_tokenaddress = arr_tokenaddress_name[0]
                    str_tokenname = arr_tokenaddress_name[1]
                    p_token_last_active =   td_.locator("p")
                                       
                    str_token_last_active = (await p_token_last_active.text_content()).strip()
                    if str_token_last_active == "--":
                        arr_trs_datares.pop()
                        break
                    arr_tds_datares[0] = str_tokenaddress
                    arr_tds_datares[1] = str_tokenname
                    arr_tds_datares[2] = str_token_last_active
                    
                     
                elif (idx_td_ == 1):
                    continue
               
                elif (idx_td_ == 2):
                    continue
           
                elif (idx_td_ == 3):
                    
                    arr_p_profit =  await td_.locator('p').all()
                      
                    str_profit_usd = (await arr_p_profit[0].text_content()).strip()
                    str_profit_percent = (await arr_p_profit[1].text_content()).strip()
                    str_profit_usd =str_profit_usd.replace("$","")
                    str_profit_percent =str_profit_percent.replace("%","")
                    
                    if str_profit_usd=="--":
                        str_profit_usd = "0"
                        
                    if str_profit_percent=="--":
                        str_profit_percent = "0"
                    
                    arr_tds_datares[3] =   str_profit_usd
                    arr_tds_datares[4] =  str_profit_percent
 
                elif (idx_td_ == 4):
                    continue
      
                elif (idx_td_ == 5):
                    continue
    
                elif (idx_td_ == 6):
                    # bought
                    arr_p_buy =  await td_.locator('p').all()
                    str_buy_usd = (await arr_p_buy[0].text_content()).strip()
                    str_buy_usd =str_buy_usd.replace("$","")
                    arr_tds_datares[5] = str_buy_usd
                    
                   
                elif (idx_td_ == 7):
                    # sold
                    arr_p_sell =  await td_.locator('p').all()
                    str_sell_usd = (await arr_p_sell[0].text_content()).strip()
                    str_sell_usd =str_sell_usd.replace("$","")
                    arr_tds_datares[6] = str_sell_usd
                    
          
                elif (idx_td_ == 8):
                    # txns
                    
                    str_30d_txns= (await td_.text_content()).strip()
                    str_30d_txns = re.sub(r'\s+', '', str_30d_txns)
                    
                    arr_30dtxns = str_30d_txns.split("/")
                    str_buy_txns = arr_30dtxns[0]
                    str_sell_txns =arr_30dtxns[1]
                    arr_tds_datares[7] = str_buy_txns
                    arr_tds_datares[8] = str_sell_txns
                    
                 

 
        return arr_trs_datares

    except Exception as e:
        print( "error",str_dalaoaddress,   "arr_tokenlink=", arr_tokenlink , "listen_td_=",(await listen_tr_.text_content()), 
              "listen_td_=",  (await listen_td_.text_content()) 
              )
        raise


async def get_onedriver_swapactivities(cur_arr_str_dalaoaddress, cur_arr_str_dalaoUrl):
    async with async_playwright() as playwright:
       
        await do_some_thing(playwright, cur_arr_str_dalaoaddress, cur_arr_str_dalaoUrl)


async def main():
    print("enter main()")
    tasks = [get_onedriver_swapactivities(
        arr_split_str_dalaoaddress[idx_driver], arr_split_str_dalaoSwapUrl[idx_driver]) for idx_driver in range(0, driver_len)]

    await asyncio.gather(*tasks)




  
USER_DIR_PATH =    "./browsercookie_path"
makedirpath(USER_DIR_PATH)
asyncio.run(main())
# main()
print(f"{'{:<6}'.format('END')}  ----------------NOTE-----------NOTE---------------")
