
# 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

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

baseclass.makedirpath(baseclass.browsercookie_path)
baseclass.makedirpath(baseclass.dalao_solscan_info_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---------------")
 

async def do_some_thing(playwright,   arr_url_dalao , url_type):

    # browser =  playwright.chromium.launch(headless=True)
    context = await playwright.firefox.launch_persistent_context(user_data_dir=USER_DIR_PATH, accept_downloads=True, headless=False)
    # context = browser.new_context()
    context.set_default_timeout(19000)
    # context = await browser.new_context()
    arr_page = [None]*2
    arr_page[0] = await context .new_page()
    await arr_page[0].goto(
        "https://solscan.io/")
    await asyncio.sleep(30)

    # print(context.pages)

    for idx_url in range(0, len(arr_url_dalao)):

        str_dalaoTransferUrl = arr_url_dalao[idx_url]
        str_dalaoaddress =obj_url_2_dalao[str_dalaoTransferUrl]
        
        print(f"enter str_dalaoaddress= {str_dalaoaddress}")
        print(f"enter str_dalaoTransferUrl= {str_dalaoTransferUrl}")
        print(f"cur_arr_str_dalaoaddress remainder = {len(arr_url_dalao)-idx_url}")
        arr_page[1] = await context.new_page()
        page = arr_page[1]
        await asyncio.sleep(3)

        # tbody_ = page.locator(
        #     'table.w-full tbody')
        await page.wait_for_selector('#account-tabs',timeout=15000)
        await page.wait_for_selector('#account-tabs iv.flex.flex-col.gap-4.items-stretch.justify-start',timeout=15000)
        arr_div =await page.locator("#account-tabs div.flex.flex-col.gap-4.items-stretch.justify-start div.flex.gap-1.flex-row.items-center.justify-start.flex-wrap").all()
        # await asyncio.sleep(3)
        str_tx_info =(await arr_div[0].text_content().strip())
        #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()

        print(str_tx_info)
 
 


        time.sleep(1)
        await page.close()

    await context.close()


async def get_onedriver_swapactivities( arr_url_dalao , url_type):
    async with async_playwright() as playwright:

        await do_some_thing(playwright, arr_url_dalao, url_type)


async def main():
    print("enter main()")
    if len(arr_dalao_url)>0:
        await get_onedriver_swapactivities(
            arr_url_dalao = arr_dalao_url , url_type = "transfer_counts")
        await asyncio.sleep(3)
        
    # if len(arr_dalao_token_inflow_transfer_url)>0:
    #     await get_onedriver_swapactivities(
    #         arr_url_dalao = arr_dalao_token_inflow_transfer_url , url_type = "swap_counts")
    #     await asyncio.sleep(3)
 


df = pd.read_excel(baseclass.dalao_tran_gmgnOrdex_path /
                 "input_dalao.xlsx", dtype=object)

arr_str_dalaoaddress = df['dalaoaddress'].tolist()
arr_str_dalaoaddress=arr_str_dalaoaddress[0:4]
# arr_dalao_sol_transfer_url =[]
arr_dalao_url = []
arr_dalao_swap = []
obj_url_2_dalao={
    
}
def init():
    now_time = time.time()
    global obj_url_2_dalao
    global arr_dalao_sol_transfer_url ,arr_dalao_token_inflow_transfer_url ,arr_dalao_token_outflow_transfer_url , arr_dalao_usdc_transfer_url
    for str_dalaoaddress in arr_str_dalaoaddress:
        # file = baseclass.dalao_solscan_info_path / f"ori_txhash_{str_dalaoaddress}.csv"
        dalao_transfer_counts_file = baseclass.dalao_solscan_info_path / f"dalao_transfer_counts.json"
        # token_inflow_transfer_file = baseclass.dalao_solscan_info_path / f"token_inflow_{str_dalaoaddress}.csv"
   
        url_transfer = f"https://solscan.io/account/{str_dalaoaddress}"  
        # url_transfer = f"https://solscan.io/account/{str_dalaoaddress}#transfers"  
        url_swap = f"https://solscan.io/account/{str_dalaoaddress}#defiactivities"     
        arr_dalao_url.append(url_transfer)
        obj_url_2_dalao[url_transfer] = str_dalaoaddress
        
 
            


init()
 
downloads_path = baseclass.dalao_solscan_info_path
USER_DIR_PATH = baseclass.browsercookie_path
asyncio.run(main())
print(f"{'{:<6}'.format('END')} {baseclass.scriptfilename} ----------------NOTE-----------NOTE---------------")
