import os
import time
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from charts import models


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

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

    def test_shows_home_page_with_create_chart_button(self):
        self.browser.get(self.live_server_url)
        self.assertEqual(self.browser.title, "BTech CNA Charting")

        create_chart_button = self.browser.find_element_by_css_selector("main .action-button")
        self.assertIn(
            create_chart_button.get_attribute("href"),
            self.live_server_url + '/charts/new/'
            )
        self.assertIn(
            'Welcome to\nBTech CNA Charting',
            self.browser.find_element_by_css_selector('main header').text
            )

        create_chart_button.click()

        self.assertEqual(
            self.browser.current_url,
            self.live_server_url + '/charts/new/'
            )

    def test_create_chart(self):
        self.browser.get(f'{self.live_server_url}/charts/new/')
        student_name_input = self.browser.find_element_by_css_selector("input[name=student_name]")
        student_id_input = self.browser.find_element_by_css_selector("input[name=student_id]")
        patient_input = self.browser.find_element_by_css_selector("input[name=patient]")
        assignment_input = self.browser.find_element_by_css_selector("input[name=assignment]")
        submit_button = self.browser.find_element_by_css_selector("input[value=Continue]")

        student_name_input.send_keys('Nicholas Jenkins')
        student_id_input.send_keys('12345')
        patient_input.send_keys('Widmerpool')
        assignment_input.send_keys('#1')
        submit_button.click()

        self.assertEqual(
            self.browser.current_url,
            self.live_server_url + '/charts/1/entry/'
            )
        self.assertIn(
            'Widmerpool',
            self.browser.find_element_by_css_selector(".chart__header").text
            )



