import sys
sys.stdout.reconfigure(encoding='utf-8')
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp('http://localhost:9222')
    context = browser.contexts[0]
    page = context.pages[0] if context.pages else context.new_page()
    
    # 1. Fill Current town/city
    city_input = page.locator("input[placeholder='Current town/city'], input[aria-label*='town/city'], input[aria-label*='Current']").first
    if city_input.count() == 0:
        # If form is not open, click the button
        print('Opening Current city button...')
        page.locator("text=/Current city or town/i").first.click()
        page.wait_for_timeout(2000)
        city_input = page.locator("input[placeholder='Current town/city'], input[aria-label*='town/city']").first
        
    print('Filling Current City: San Jose, California...')
    city_input.click()
    city_input.fill('San Jose, California')
    page.wait_for_timeout(2000)
    
    # Select from dropdown
    opts = page.locator("div[role='listbox'] div[role='option'], div[role='option'], ul[role='listbox'] li")
    print('City options found:', opts.count())
    if opts.count() > 0:
        print('Selecting:', opts.first.inner_text().replace('\n', ' '))
        opts.first.click()
    else:
        page.keyboard.press('Enter')
    page.wait_for_timeout(1500)
    
    # Click Save
    save_btn = page.locator("div[role='button']:has-text('Save'), span:has-text('Save')").first
    print('Clicking Save for Current City...')
    save_btn.click()
    page.wait_for_timeout(4000)
    print('Current City saved!')
    
    # 2. Fill Home town
    print('\nLooking for Home town button...')
    home_btn = page.locator("text=/Home town/i").filter(has_not=page.locator("h2, span.x193iq5w")).first
    # Find the clickable element
    home_btn = page.locator("div[role='button']:has-text('Home town'), span:has-text('Home town')").first
    print('Clicking Home town button...')
    home_btn.click()
    page.wait_for_timeout(2000)
    
    home_input = page.locator("input[placeholder*='Home town'], input[aria-label*='Home town'], input[placeholder*='hometown']").first
    if home_input.count() == 0:
        home_input = page.locator("div[role='main'] input[type='text'], div[role='main'] input[type='search']").first
        
    print('Filling Hometown: Ho Chi Minh City, Vietnam...')
    home_input.click()
    home_input.fill('Ho Chi Minh City, Vietnam')
    page.wait_for_timeout(2000)
    
    home_opts = page.locator("div[role='listbox'] div[role='option'], div[role='option'], ul[role='listbox'] li")
    print('Hometown options found:', home_opts.count())
    if home_opts.count() > 0:
        print('Selecting:', home_opts.first.inner_text().replace('\n', ' '))
        home_opts.first.click()
    else:
        page.keyboard.press('Enter')
    page.wait_for_timeout(1500)
    
    # Click Save
    save_btn2 = page.locator("div[role='button']:has-text('Save'), span:has-text('Save')").first
    print('Clicking Save for Hometown...')
    save_btn2.click()
    page.wait_for_timeout(4000)
    print('Hometown saved!')
    
    page.screenshot(path=r'C:\Projects\Historical\FacebookAutoPoster\places_final.png')
    print('Screenshot places_final.png saved!')
