"""login.py

This module performs a programmatical login into a Twitter account using
Selenium.

"""

import argparse
import time
import string
import random
from constance import config
import logging
import requests
import sys

from selenium.webdriver.common.by import By
from selenium.webdriver.common import action_chains, keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from bot.models import Account

from autotweet.driver import getOrCreateWebdriver
from autotweet.utils import sleep
from autotweet.utils import ok_click
from autotweet.utils import send_post_request

logger = logging.getLogger(__name__)

def rnd_str_gen(size=6, chars= string.ascii_letters + string.digits):
    return ''.join(random.choice(chars) for _ in range(size))


class AutoLogin:

    def __init__(self, username):
        if not username:
            return
        self._username = username
        logger.debug(f"{self._username}")
        try:
            account = Account.objects.get(username=self._username)
            logger.debug(f"{account} {account.username} {account.password}")
        except Account.DoesNotExist:
            return
        self._password = account.password
        self._email = account.email
        try:
            self._phone = account.phone.as_e164
        except AttributeError:
            self._phone = ""
        self._authenticity_token = None
        self._driver = getOrCreateWebdriver(js=False)

    def ok_email(self):
        logger.debug("...")
        sleep()
        source = self._driver.page_source
        logger.warn(f" Page source: {source[:500]}")
        if config.email_verification in source:
            if not self._email:
                logger.error(f"Email field for  @{self._username} is empty.")
                return
            try:
                self._driver.find_element_by_xpath(
                    '//input[@id="challenge_response"]'
                    ).send_keys(self._email)
                sleep()
                self._driver.find_element_by_xpath(
                    '//input[@id="email_challenge_submit"]'
                    ).click()
            except NoSuchElementException:
                logger.error('Email verification failed')
                return
            sleep()

    def ok_phone(self):
        logger.debug("...")
        sleep()
        source = self._driver.page_source
        logger.warn(f" Page source: {source[:500]}")
        if config.phone_verification in source:
            if not self._phone:
                logger.error(f"Phone field for  @{self._username} is empty.")
                return
            try:
                self._driver.find_element_by_xpath(
                    '//input[@id="challenge_response"]'
                    ).send_keys(self._phone)
                sleep()
                self._driver.find_element_by_xpath(
                    '//input[@id="email_challenge_submit"]'
                    ).click()
            except NoSuchElementException:
                logger.error('Phone verification failed')
                return
            sleep()

    def get_authenticity_token(self):
        if self._authenticity_token:
            return self._authenticity_token
        try:
            auth_elem = self._driver.find_element_by_xpath(
                '//input[@name="authenticity_token"]'
            )
        except NoSuchElementException:
            logger.error('Authenticity token not found')
            return            
        auth_token = auth_elem.get_attribute('value')
        self._authenticity_token = auth_token
        logger.debug(f"{auth_token=}")
        return auth_token

    def login_mobile_twitter(self):
        if not self._password:
            logger.error(f"Password field for  @{self._username} is empty.")
            return
        self._driver.get("https://mobile.twitter.com/login")
        ok_click()
        sleep()
        try:
            username_input = self._driver.find_element_by_xpath(
                '//input[@id="session[username_or_email]"]'
            )
        except NoSuchElementException as e:
            source = self._driver.page_source
            logger.error(
                f"Could not find this element: \n"
                f"//input[@id='session[username_or_email]']\n"
                f"Error: {e}\n"
                f"Page source: {source}"
            )
        try:
            password_input = self._driver.find_element_by_xpath(
                '//input[@id="session[password]"]'
            )
        except NoSuchElementException as e:
            source = self._driver.page_source
            logger.error(
                f"Could not find this element: \n"
                f"//input[@id='session[username_or_email]']\n"
                f"Error: {e}\n"
                f"Page source: {source}"
            )
        try:
            login_button = self._driver.find_element_by_xpath(
                '//input[@name="commit"]'
            )
        except NoSuchElementException as e:
            source = self._driver.page_source
            logger.error(
                f"Could not find this element: \n"
                f"//input[@id='session[username_or_email]']\n"
                f"Error: {e}\n"
                f"Page source: {source}"
            )
        username_input.send_keys(self._username)
        sleep()
        password_input.send_keys(self._password)
        sleep()
        login_button.click()
        self.ok_email()
        self.ok_phone()
        self.get_authenticity_token()

    def login_twitter(self):
        driver = getOrCreateWebdriver(js=True)
        driver.get("https://www.twitter.com/login")
        sleep()
        focused_elem = driver.switch_to.active_element
        focused_elem.send_keys(self.username)
        sleep()
        focused_elem.send_keys(keys.Keys.TAB)
        focused_elem = driver.switch_to.active_element
        focused_elem.send_keys(self.password)
        sleep()
        focused_elem.send_keys(keys.Keys.TAB)
        focused_elem = driver.switch_to.active_element
        focused_elem.click()
    
    def logout(self):
        auth_token = self.get_authenticity_token()
        if not auth_token:
            return
        url = "https://mobile.twitter.com/session/destroy"
        commit = "Log+out"
        referer = "https://mobile.twitter.com/account"
        send_post_request(
            auth_token=auth_token,
            url=url,
            commit=commit,
            referer=referer,
        )

def close():
    driver = getOrCreateWebdriver()
    driver.close()

def tweet(txt):
    new_tweet_id = "global-new-tweet-button"
    driver = getOrCreateWebdriver()
    sleep()
    try:
        driver.find_element_by_id(new_tweet_id).click()
    except NoSuchElementException:
        driver.get("https://www.twitter.com/compose/tweet")
    focused_elem = driver.switch_to.active_element
    focused_elem.send_keys(txt)
    sleep()
    #driver.find_element_by_class_name('SendTweetsButton').click()
    driver.find_element_by_xpath("//div[@data-testid='tweetButton']").click()

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Authorize this Twitter userid.')
    parser.add_argument('integers', metavar='N', type=int, nargs='+',
                   help='a bigint user id')
    args = parser.parse_args()
    print(f"args.integers: {args.integers}")
@@ -4426,32 +4426,51 @@
 %22%0A            )%0A
+            return%0A
         try:%0A   
@@ -4458,32 +4458,32 @@
 rn%0A        try:%0A
-
             pass
@@ -4905,32 +4905,51 @@
 %22%0A            )%0A
+            return%0A
         try:%0A   
@@ -5359,32 +5359,32 @@
 urce: %7Bsource%7D%22%0A
-
             )%0A  
@@ -5373,32 +5373,51 @@
 %22%0A            )%0A
+            return%0A
         username

fix login