import time
import json
import pdb
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as EC

def main():
    
    # Generated by Selenium IDE
    driver = webdriver.Firefox()
    vars = {}
    
    driver.get("https://www.4devs.com.br/gerador_de_veiculos")
    driver.set_window_size(1047, 708)
    driver.find_element(By.ID, "estado").click()
    dropdown = driver.find_element(By.ID, "estado")
    dropdown.find_element(By.XPATH, "//option[. = 'PI']").click()
    driver.find_element(By.ID, "estado").click()
    driver.find_element(By.ID, "pontuacao_nao").click()
    driver.find_element(By.ID, "fipe_codigo_marca").click()
    dropdown = driver.find_element(By.ID, "fipe_codigo_marca")
    dropdown.find_element(By.XPATH, "//option[. = 'Mitsubishi']").click()
    driver.find_element(By.ID, "fipe_codigo_marca").click()
    
    cars = []

    for i in range(50):
        driver.find_element(By.ID, "bt_gerar").click()
        element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "marca"))
        )
        marca = driver.find_element(By.ID, 'marca').get_attribute("value")
        modelo = driver.find_element(By.ID, 'modelo').get_attribute("value")
        ano = '2020' # driver.find_element(By.ID, 'ano').get_attribute("value")
        cor = driver.find_element(By.ID, 'cor').get_attribute("value")
        cars.append([marca, modelo, ano, cor])

    with open('cars.sql', 'w') as file:
        file.write('INSERT INTO cars (brand, manufacture_year, model, color, used) VALUES ')
        for car in cars:
            for i in range(len(car)):
                car[i] = '\"' + car[i] + '\"'
            used = ', 1' if car[2] == '\"2020\"' or car[2] == '\"2019\"' or car[2] == '\"2018\"' else ', 0'
            strcar = ','.join(car) + used
            file.write(f'   ({strcar}),\n')

if __name__ == '__main__':
    main()