from dotenv import load_dotenv
from playwright.async_api import Page

from pyba.utils.load_yaml import load_config

# Adjust this import path based on where you save base.py
from .base import BaseLogin

load_dotenv()  # Loading the username and passwords
config = load_config("general")["automated_login_configs"]["instagram"]

# These are specific to instagram's login flow
screen_height = config["click_location"]["default_screen_height"]
x_from_left = config["click_location"]["x_from_left"]
y_from_bottom = config["click_location"]["y_from_bottom"]
y_top_left = screen_height - y_from_bottom


class InstagramLogin(BaseLogin):
    """
    The instagram login engine, inherits from the BaseLogin class. This
    still needs a module level import because it calls for more than what
    is defined in the BaseLogin class.
    """

    def __init__(self, page: Page) -> None:
        super().__init__(page, engine_name="instagram")

    async def _perform_login(self) -> bool:
        try:
            await self.page.wait_for_selector(self.config["username_selector"])
            await self.page.fill(self.config["username_selector"], self.username)
            await self.page.fill(self.config["password_selector"], self.password)
            await self.page.click(self.config["submit_selector"])
        except Exception:
            try:
                # Alternate fields that instagram uses
                await self.page.wait_for_selector(self.config["fallback"]["username_selector"])
                await self.page.fill(self.config["fallback"]["username_selector"], self.username)
                await self.page.fill(self.config["fallback"]["password_selector"], self.password)
                await self.page.click(self.config["submit_selector"])
            except Exception:
                return False

        # There is a not-now button that we need to click
        try:
            await self.page.wait_for_selector(
                self.config["additional_args"]["additional_selector_1"], timeout=30000
            )
            await self.page.click(self.config["additional_args"]["additional_selector_1"])
        except Exception:
            pass

        # Sometimes these things also come up for new updates
        try:
            await self.page.wait_for_selector(
                self.config["additional_args"]["additional_selector_2"], timeout=10000
            )
            await self.page.mouse.click(x_from_left, y_top_left)
        except Exception:
            pass

        return True
