import subprocess
import time
import requests
import os
from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.by import By
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ==========================================
# CONFIGURATION & PARAMETERS
# ==========================================
Password_Here = "G@Udn1w@#8N8"
key = 'dreYWZN6BkiImePegTZ5et'  # Replace with your IFTTT webhook key
event = 'tbn_event'             # Replace with your IFTTT event name

feed_caramelo = True
perform_plus_caramelo = False
perform_confirm_caramelo = False

def send_ifttt_webhook(event, key, message):
    """Send a webhook message to IFTTT."""
    url = f'https://maker.ifttt.com/trigger/{event}/with/key/{key}'
    data = {'value1': message}
    try:
        requests.post(url, json=data, timeout=10)
        print(f"Webhook sent with message: {message}")
    except Exception as e:
        print(f"Failed to transmit webhook: {e}")
    return None

def pre_launch_wake_and_start():
    """Ensure the headless canvas is awake and force start the app."""
    print("Waking up virtual display canvas...")
    # NOTE: pm clear has been removed from here to preserve the saved session!
    subprocess.run("adb shell svc power stayon true", shell=True)
    subprocess.run("adb shell wm dismiss-keyguard", shell=True)

    print("Launching Petlibro via Activity Manager...")
    subprocess.run("adb shell am start -W -n com.designlibro.petlibro/.MainActivity --receiver-foreground -f 0x10000000", shell=True)
    time.sleep(5)

def start_appium_session():
    """Initialize the cloud Appium options object matching Waydroid architecture."""
    options = UiAutomator2Options()
    options.platform_name = "Android"
    options.automation_name = "UiAutomator2"
    options.device_name = "192.168.240.2:5555"
    options.udid = "192.168.240.2:5555"
    options.app_package = "com.designlibro.petlibro"
    options.app_activity = ".MainActivity"
    options.no_reset = True
    options.ensure_webviews_have_pages = True
    options.set_capability("androidHome", "/usr/lib/android-sdk")

    driver = webdriver.Remote('http://127.0.0.1:4723', options=options)
    return driver

# Pre-wake display canvas and force app execution
pre_launch_wake_and_start()

# Start the Appium UI automation driver session
driver = start_appium_session()
print("Appium session established successfully.")

# ==========================================
# LOCATORS 
# ==========================================
# Core Feeding Interface
caramelo_locator = (By.XPATH, "//android.widget.Button[@content-desc='Kitchen\nCaramelo Bona\nNext feeding:\nNo Feeding Schedule ']")
feed_now_locator = (By.XPATH, "//android.widget.ImageView[@content-desc='Feed Now']")
plus_locator = (By.XPATH, "//android.view.View[@content-desc='10 g']/android.widget.ImageView[2]") 
confirm_locator = (AppiumBy.ACCESSIBILITY_ID, "Confirm")
back_locator = (By.XPATH, "//android.widget.FrameLayout[@resource-id='android:id/content']/android.widget.FrameLayout/android.widget.FrameLayout/android.view.View/android.view.View/android.view.View/android.view.View/android.widget.ImageView[1]")

# First-Time Initialization
agree_continue_locator = (By.XPATH, "//*[@text='Agree And Continue' or @content-desc='Agree And Continue']")

try:
    # ==========================================
    # SMART STATE CHECK
    # ==========================================
    print("Checking if we are already logged in and on the dashboard...")
    needs_full_login = False
    
    try:
        # Give the cloud up to 12 seconds to load the dashboard natively
        WebDriverWait(driver, 12).until(
            EC.presence_of_element_located(caramelo_locator)
        )
        print("BINGO! Caramelo's profile found. Skipping login initialization entirely.")
        
    except Exception:
        print("Caramelo not found. The app is logged out or corrupted. Initiating full reset and login sequence...")
        needs_full_login = True

    # ==========================================
    # FULL LOGIN GAUNTLET (Only runs if needed)
    # ==========================================
    if needs_full_login:
        # 1. Nuke the app memory and restart it while keeping the Appium bridge alive
        print("Wiping app memory for a clean login slate...")
        subprocess.run("adb shell pm clear com.designlibro.petlibro", shell=True)
        time.sleep(1)
        print("Relaunching clean app state...")
        subprocess.run("adb shell am start -W -n com.designlibro.petlibro/.MainActivity --receiver-foreground -f 0x10000000", shell=True)
        time.sleep(6)

        # 2. Terms and Conditions
        try:
            agree_btn = driver.find_elements(*agree_continue_locator)
            if agree_btn:
                agree_btn[0].click()
                print("Terms and Conditions prompt handled.")
                time.sleep(3)
        except Exception:
            pass

        # 3. Notification Bypass
        try:
            print("Checking for native notification prompt...")
            deny_button = WebDriverWait(driver, 5).until(
                EC.presence_of_element_located((By.ID, "com.android.permissioncontroller:id/permission_deny_button"))
            )
            deny_button.click()
            print("Handled notification prompt successfully.")
        except Exception:
            print("No notification prompt detected. Moving on...")

        # 4. Flutter-Adaptive Login Entry
        try:
            print("Waiting for Flutter UI to stabilize...")
            
            # Email
            email_field = WebDriverWait(driver, 15).until(
                EC.element_to_be_clickable((By.XPATH, "//*[@class='android.view.View' and (@text='Email' or contains(@content-desc, 'Email'))] | //android.widget.EditText"))
            )
            email_field.click()
            email_field.send_keys("alexandre.bona@gmail.com")
            
            # Password
            password_field = driver.find_element(By.XPATH, "//*[@class='android.view.View' and (@text='Password' or contains(@content-desc, 'Password'))] | //android.widget.EditText[2]")
            password_field.click()
            password_field.send_keys(Password_Here)
            driver.press_keycode(66) # Enter
            time.sleep(0.5)

            # Checkbox
            checkbox = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "//android.widget.ImageView[@index='5' and @clickable='true']"))
            )
            checkbox.click()

            # Login Button
            login_button = WebDriverWait(driver, 10).until(
                EC.element_to_be_clickable((By.XPATH, "//android.widget.Button[@index='7' and @content-desc='Log In' and @clickable='true']"))
            )
            login_button.click()
            print("Login payload transmitted!")

            # 5. Post-Login Intercept Gauntlet
            print("Waiting 8 seconds for Kalay cloud sync...")
            time.sleep(8) 
            
            print("Checking for Message Center hijack...")
            try:
                WebDriverWait(driver, 5).until(
                    EC.presence_of_element_located((AppiumBy.XPATH, "//android.view.View[@content-desc='MESSAGE CENTER']"))
                )
                print("Trapped in Message Center! Clicking back arrow...")
                back_arrow = driver.find_element(AppiumBy.XPATH, "//android.widget.ImageView[@index='0' and @clickable='true']")
                back_arrow.click()
                time.sleep(3) 
            except Exception:
                print("No Message Center intercepted.")

        except Exception as login_err:
            print(f"Login routine fault: {login_err}")


    # ==========================================
    # CORE PET FEEDING PIPELINE
    # ==========================================
    # This runs whether we just logged in, or if we were already logged in!
    if feed_caramelo:
        print("Locating Caramelo's machine entry profile...")
        driver.find_element(*caramelo_locator).click()
        
        print("Waiting 4 seconds for the device page to load...")
        time.sleep(4)

        # INTERCEPT: Device-Level "Done" Tooltip
        # We only check for this here because it only spawns inside the device menu!
        print("Checking for First-Time 'Done' tooltip on the device page...")
        try:
            target = WebDriverWait(driver, 5).until(
                EC.presence_of_element_located((AppiumBy.ACCESSIBILITY_ID, "Done"))
            )
            print("Tooltip detected! Firing native click...")
            target.click()
            time.sleep(3)
        except Exception:
            try:
                target = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().descriptionContains("Done")')
                driver.execute_script("mobile: clickGesture", {"elementId": target.id})
                time.sleep(3)
            except Exception:
                print("No 'Done' tooltip detected. Device screen is clear.")

        # Execute Feeding
        print("Triggering instant feed canvas overlay...")
        driver.find_element(*feed_now_locator).click()
        time.sleep(2)
        
        if perform_plus_caramelo:
            print("Incrementing weight portion size (+10g)...")
            driver.find_element(*plus_locator).click()
            time.sleep(2)

        if not perform_confirm_caramelo:
            print("Aborting feeding action loop: issuing back keycode.")
            driver.press_keycode(4)
            time.sleep(2)

        if perform_confirm_caramelo:
            print("Feeding validation confirmed. Delivering treats immediately.")
            driver.find_element(*confirm_locator).click()
            time.sleep(2)

        print("Returning interface map to root layer...")
        driver.find_element(*back_locator).click()
        time.sleep(2)

except Exception as e:
    print(f"Critical execution block fault tripped: {e}")
    send_ifttt_webhook(event, key, "Feed Failed!")

    try:
        print("Snapping emergency triage screenshots and layout XML...")
        driver.save_screenshot('/home/ubuntu/caramelo_fault.png')
        with open('/home/ubuntu/caramelo_layout.xml', 'w', encoding='utf-8') as f:
            f.write(driver.page_source)
        print("Diagnostics saved to ~/caramelo_fault.png and ~/caramelo_layout.xml")
    except Exception as diag_err:
        print(f"Failed to capture diagnostics: {diag_err}")

finally:
    print("Safely disconnecting client driver stream links...")
    try:
        driver.quit()
    except:
        pass

    print("Force-stopping application package execution layers cleanly...")
    subprocess.run("adb shell am force-stop com.designlibro.petlibro", shell=True)
    print("System optimization cleanup complete. Session closed.")