# Generated by Selenium IDE
import pytest
import time
import json
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.chrome.options import Options
from datetime import datetime

class TestTest():
  def setup_method(self):
    # instantiate a chrome options object so you can set the size and headless preference
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    
    self.driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options)
    self.vars = {}
    self.data = {}
  
  def teardown_method(self):
    self.driver.quit()
  
  def test_test(self, seconds):
    first_time = True
    emails = self.data["emails"]
    codes = self.data["codes"]
    print(emails)
    print(codes)
    for email in emails:
      for code in codes:
        if not first_time:
          time.sleep(seconds)

        self.vars["email"] = email
        self.vars["code"] = code

        self.driver.get("https://winxbox.com/")
        self.driver.set_window_size(850, 850)
        WebDriverWait(self.driver, 180).until(expected_conditions.presence_of_element_located((By.ID, "Email_Identifier")))
        self.driver.find_element(By.ID, "Email_Identifier").click()
        self.driver.find_element(By.ID, "Email_Identifier").send_keys(self.vars["email"])
        self.driver.find_element(By.CSS_SELECTOR, "#emailFormLogin > .grid-x").click()
        self.driver.find_element(By.ID, "emailLogin-Submit-Submit").click()
        

        WebDriverWait(self.driver, 180).until(expected_conditions.presence_of_element_located((By.ID, "CodeEntry.Code")))

        #log beginning of entry
        with open('log.txt', 'a') as log:
          now = datetime.now()
          now_str = now.strftime("%d/%m/%Y %H:%M:%S")
          log_str = f'Attempting submission for {email} with code {code} at {now_str}...'
          log.write(log_str)
          print(log_str)

        self.driver.find_element(By.ID, "CodeEntry.Code").clear()
        self.driver.find_element(By.ID, "CodeEntry.Code").send_keys(self.vars["code"])
        self.driver.find_element(By.NAME, "submit").click()

        #log entry completion
        with open('log.txt', 'a') as log:
          log_str = 'complete\n'
          log.write(log_str)
          print(log_str)

        self.driver.save_screenshot(f'proof/{email}{code}.png')
          
        first_time = False

  def test_verify(self):
    #log beginning of verificiation
    with open('log.txt', 'a') as log:
      now = datetime.now()
      now_str = now.strftime("%d/%m/%Y %H:%M:%S")
      log_str = f'Beginning entry verificiation at {now_str}:\n'
      log.write(log_str)
      print(log_str)

    emails = self.data["emails"]
    print(emails)
    for email in emails:
      self.vars["email"] = email

      self.driver.get("https://winxbox.com/")
      self.driver.set_window_size(850, 850)
      self.driver.find_element(By.ID, "Email_Identifier").click()
      self.driver.find_element(By.ID, "Email_Identifier").send_keys(self.vars["email"])
      self.driver.find_element(By.CSS_SELECTOR, "#emailFormLogin > .grid-x").click()
      self.driver.find_element(By.ID, "emailLogin-Submit-Submit").click()

      self.driver.save_screenshot(f'proof/{email}.png')
      #log beginning of entry
      with open('log.txt', 'a') as log:
        now = datetime.now()
        now_str = now.strftime("%d/%m/%Y")
        log_str = str()
        try:
          page = self.driver.find_element(By.ID, "played")
          log_str = f'Daily submissions for {email} complete on {now_str}\n'
        except:
          log_str = f'Daily submissions for {email} incomplete on {now_str}\n'
        log.write(log_str)
        print(log_str)
        

  def load_vars(self, path):
    with open(path, 'r') as file:
      data = json.load(file)
      for key in data:
        self.data[key] = data[key]
  
