import os
import time

from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from django.contrib.staticfiles.testing import StaticLiveServerTestCase


class AddressTests(StaticLiveServerTestCase):
    fixtures = ['products.json']

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.browser = WebDriver()

    @classmethod
    def tearDownClass(cls):
        cls.browser.quit()
        super().tearDownClass()

    def test_invalid_address_returns_errorlist(self):
        self.browser.get(self.live_server_url + '/checkout/address/')
        self.assertEqual(
            self.browser.title,
            'Checkout | Port Townsend Roasting Co.'
        )

        full_name_input = self.browser.find_element_by_name("full_name")
        full_name_input.send_keys('John Doe')
        email_input = self.browser.find_element_by_id('id_email')
        email_input.send_keys('john@example.com')
        street_address_1_input = self.browser.find_element_by_name('street_address_1')
        street_address_1_input.send_keys('1579')
        city_input = self.browser.find_element_by_name('city')
        city_input.send_keys('Logan')
        state_select = select = Select(self.browser.find_element_by_name('state'))
        state_select.select_by_value('UT')
        postal_code_input = self.browser.find_element_by_name('postal_code')
        postal_code_input.send_keys('37461')
        self.browser.find_element_by_xpath('//input[@value="Continue"]').click()
        # try:
        #     WebDriverWait(self.browser, 4).until(
        #         EC.presence_of_element_located((By.CLASS_NAME, 'errorlist'))
        #     )
        # finally:
        #     self.browser.quit()

        self.assertEqual(
            self.browser.find_element_by_css_selector(
                '.errorlist li'
            ).text,
            'USPS: Address Not Found.'
        )

    def test_address_is_url_safe(self):
        self.browser.get(self.live_server_url + '/checkout/address/')
        self.assertEqual(
            self.browser.title,
            'Checkout | Port Townsend Roasting Co.'
        )

        full_name_input = self.browser.find_element_by_name("full_name")
        full_name_input.send_keys('John Doe')
        email_input = self.browser.find_element_by_id('id_email')
        email_input.send_keys('john@example.com')
        street_address_1_input = self.browser.find_element_by_name('street_address_1')
        street_address_1_input.send_keys('4031 N Douglas HWY #B')
        city_input = self.browser.find_element_by_name('city')
        city_input.send_keys('Juneau')
        state_select = select = Select(self.browser.find_element_by_name('state'))
        state_select.select_by_value('AK')
        postal_code_input = self.browser.find_element_by_name('postal_code')
        postal_code_input.send_keys('99801')
        self.browser.find_element_by_xpath('//input[@value="Continue"]').click()
        # try:
        #     WebDriverWait(self.browser, 4).until(
        #         EC.presence_of_element_located((By.CLASS_NAME, 'errorlist'))
        #     )
        # finally:
        #     self.browser.quit()

        self.assertRegex(
            self.browser.current_url,
            '^http://localhost:[0-9]*/checkout/$'
        )
