import os, tqdm, sys
import requests
import urllib.request
import re
from bs4 import BeautifulSoup
import multiprocessing
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

#from webdriver_manager.firefox import GeckoDriverManager
from selenium import webdriver
from utillc import *
from selenium.webdriver.firefox.service import Service as ServiceF
from selenium.webdriver.chrome.service import Service as ServiceC
import PIL
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

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
import numpy as np
#wd = browser = webdriver.Firefox(executable_path=    GeckoDriverManager().install())
EKO()
import sys
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.chrome.options import Options as ChromeOptions
import time
import cherrypy
import argparse
import PIL
from PIL import Image
import orange

def tqdmtqdm(x) : return x

class P(orange.RobotBase) :

    def __init__(self) :
        super().__init__()
        self.build(headless=False)
        pass


    def load(self,  passwd, identifiant) :
        self.url = "https://mon.cmb.fr/auth/login"
        bot = self.driver
        browser = self.driver.get(self.url)
        time.sleep(4)
        EKOI(self.screen(), sz=800)
        accept = "//*[contains(text(), 'Tout accepter')]"
        EKOX(bot.find_element(By.XPATH, accept).get_attribute('innerHTML'))
        time.sleep(2)
        self.driver.implicitly_wait(5)            
        accept_button = bot.find_element(By.XPATH, accept) 
        accept_button.click()
        time.sleep(4)
        EKOI(self.screen(), sz=800)

        userlogin = "//*[@id='userLogin']"
        userlogin_txt = bot.find_element(By.XPATH, userlogin) 
        userlogin_txt.send_keys(identifiant)        
        time.sleep(1)
        EKOI(self.screen(), sz=800)
        suivant = "//*[@id='authNextButton']"
        suivant_btn = bot.find_element(By.XPATH, suivant)
        suivant_btn.click()

        EKOI(self.screen(), sz=800)
        pwd = "//*[@id='userPassword']"
        pwd_txt = bot.find_element(By.XPATH, pwd)
        pwd_txt.send_keys(passwd)

        #time.sleep(4444)

        time.sleep(2)
        EKOI(self.screen(), sz=800)
        connect = "//*[@id='btnConnect']"
        connect_btn = bot.find_element(By.XPATH, connect)
        connect_btn.click()
        EKOI(self.screen(), sz=800)
        def get(xpath) :
            return bot.find_element(By.XPATH, xpath)            
        #time.sleep(4444)        
        panneau = get("//*[@id='domi-espace']")
        comptes = panneau.find_elements(By.XPATH, ".//*[@class='content-container']")
        EKOX(len(comptes))
        comptes[0].click()

        EKOI(self.screen(), sz=800)
        time.sleep(4444)
        connect = "//*[contains(text()='Se connecter')]"
        EKOX(bot.find_element(By.XPATH, connect).get_attribute('innerHTML'))
        time.sleep(2)
        connecter_button = bot.find_element(By.XPATH, connecter) 
        connecter_button.click()
        
        


if __name__ == '__main__':

    parser = argparse.ArgumentParser(
        prog='ferrand aspirator',
        description='download ferrand podcast')
    parser.add_argument('--pwd')
    parser.add_argument('--id')
    args = parser.parse_args()
    p = P()
    p.load(args.pwd, args.id)

