import profile
from selenium import webdriver
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException, ElementNotInteractableException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import mariadb

address = 'https://192.168.2.71'
number = '956956956'
password = '123456789'


class AuthException(Exception):
    pass


def login(driver: webdriver.Firefox, address: str, password: str):
    driver.get(address)

    WebDriverWait(driver=driver, timeout=5).until(EC.element_to_be_clickable(
        (By.XPATH, '//input[@name="input_user"]')
    )).send_keys('admin')

    WebDriverWait(driver=driver, timeout=5).until(EC.element_to_be_clickable(
        (By.XPATH, '//input[@name="input_pass"]')
    )).send_keys(password, Keys.ENTER)

    try:
        WebDriverWait(driver=driver, timeout=5).until(EC.element_to_be_clickable(
            (By.XPATH, '//*[text()="System"]')
        ))
    except TimeoutException:
        raise AuthException()


if __name__ == '__main__':
    options = FirefoxOptions()
    options.accept_insecure_certs = True
    options.capabilities['acceptSslCerts'] = True

    # options.add_argument("--headless")
    driver = webdriver.Firefox(options=options)

    # Inicio de Sesión
    try:
        login(driver, address, 'Mur5cKUBatdw')
    except AuthException:
        login(driver, address, '_RW!2k14')

    driver.get(f'{address}/?menu=pbxconfig&type=setup&display=trunks')

    WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
        (By.XPATH, '//span[contains(text(),"Add SIP Trunk")]')
    )).click()

    try:
        WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
            (By.XPATH, '//input[@name="trunk_name"]')
        )).send_keys(number)
    except TimeoutException:
        driver.get(f'{address}/?menu=pbxconfig&type=setup&display=trunks')

        WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
            (By.XPATH, '//span[contains(text(),"Add SIP Trunk")]')
        )).click()
        WebDriverWait(driver, 5).until(EC.element_to_be_clickable(
            (By.XPATH, '//input[@name="trunk_name"]')
        )).send_keys(number)

    driver.find_element(By.XPATH, '//input[@name="outcid"]').send_keys(number)
    driver.find_element(
        By.XPATH, '//input[@name="pattern_pass[0]"]').send_keys('.')
    driver.find_element(
        By.XPATH, '//input[@name="prepend_digit[0]"]').send_keys('34')
    driver.find_element(
        By.XPATH, '//input[@name="channelid"]').send_keys(number)

    peer_details = driver.find_element(
        By.XPATH, '//textarea[@name="peerdetails"]'
    )
    peer_details.clear()
    peer_details.send_keys(
        f'type = friend\nusername = {number}\nhost = 192.168.2.84\nnat = force_rport, comedia\nqualify = yes\ninsecure = invite, port'
    )

    driver.find_element(
        By.XPATH, '//input[@name="usercontext"]').send_keys('from-trunk')

    user_config = driver.find_element(
        By.XPATH, '//textarea[@name="userconfig"]'
    )
    user_config.clear()
    user_config.send_keys(
        'type = friend\nhost = 192.168.2.84\nnat = force_rport, comedia\nqualify = yes\ninsecure = invite, port'
    )


    driver.find_element(By.XPATH, '//input[@name="Submit"]').click()
    WebDriverWait(driver, 5).until(EC.element_to_be_clickable((
        By.XPATH, '//a[@id="button_reload"]'
    ))).click()
