import time
from selenium import webdriver
from selenium.webdriver.common.by import By
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.chrome.options import Options
from bs4 import BeautifulSoup


# Create ChromeOptions object to configure headless mode
chrome_options = Options()
# chrome_options.add_argument('--headless')
# Path to the Chrome WebDriver executable
chromedriver_path = 'chromedriver-win64\chromedriver.exe'
# Initialize a Selenium WebDriver (you need to have a compatible driver installed)
# Download the driver from https://sites.google.com/chromium.org/driver/
driver = webdriver.Chrome(options=chrome_options)

# Navigate to the website
driver.get("https://eimei24.com")

def fetchRate(num):
    num1=str(num)
    try:
        # Wait for the input field to become available
        imei_input = WebDriverWait(driver, 20).until(
            EC.presence_of_element_located((By.CLASS_NAME, "btn-group1"))
        )
        

        # Input the IMEI number (replace 'your_imei' with the actual IMEI)
        imei_input.send_keys(num1)
        

        # Submit the form (including handling reCAPTCHA)
        submit_button = driver.find_element(By.CLASS_NAME, "btn-form")
        submit_button.click()

        # Wait for the results popup to appear
        WebDriverWait(driver, 30).until(
            EC.presence_of_element_located((By.ID, "myModal"))
        )


        # Wait for the results to load (adjust the sleep time as needed)
        time.sleep(3)

        # Get the content of the results popup
        content = driver.find_element(By.CLASS_NAME, "row").get_attribute("innerHTML")
        
        # Parse the HTML content
        soup = BeautifulSoup(content, 'html.parser')

        # Extract table elements and create a dictionary
        tables = soup.find_all('table')
        data_dict = {}

        for table in tables:
            rows = table.find_all('tr')
            for row in rows:
                columns = row.find_all(['th', 'td'])
                if len(columns) == 2:
                    key = columns[0].get_text(strip=True)
                    value = columns[1].get_text(strip=True)
                    data_dict[key] = value

        # print(data_dict)
            
        # Get the content of the results popup
        content2 = driver.find_element(By.CLASS_NAME, "rating").get_attribute("innerHTML")
        soup = BeautifulSoup(content2, 'html.parser')

        # Extract the values of the hidden fields
        count_value = soup.find('input', id='count')['value']
        count1_value = soup.find('input', id='count1')['value']
        count2_value = soup.find('input', id='count2')['value']
        count3_value = soup.find('input', id='count3')['value']
        
        # Add the variables to the dictionary
        data_dict['batteryScore'] = count_value
        data_dict['display_screenScore'] = count1_value
        data_dict['speedScore'] = count2_value
        data_dict['overallScore'] = count3_value
        
        return data_dict
        
    except Exception as e:
        return 1

    finally:
        # Close the popup and the main browser window
        driver.quit()

        

