from django.test import LiveServerTestCase

import sys, os
from datetime import datetime

from selenium import webdriver
from pyvirtualdisplay import Display

from django.core.urlresolvers import reverse

SCREEN_DUMP_LOCATION = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), 'screendumps'
)


class FunctionalTest(LiveServerTestCase):
    @classmethod
    def setUpClass(cls):
        for arg in sys.argv:
            if 'liveserver' in arg:
                cls.server_url = 'http://' + arg.split('=')[1]
                return
        super().setUpClass()
        cls.server_url = cls.live_server_url

    @classmethod
    def tearDownClass(cls):
        if cls.server_url == cls.live_server_url:
            super().tearDownClass()
            
    def disable_implicit_wait(self):
        self.browser.implicitly_wait(0)
        
    def enable_implicit_wait(self):
        self.browser.implicitly_wait(10)

    def setUp(self):
        self.display = Display()
        self.display.start()

        self.browser = webdriver.Firefox()
        self.enable_implicit_wait()
        
        self.home_url = ''.join([self.server_url, reverse("home")])

    def tearDown(self):
        if not self._outcomeForDoCleanups.success:
            if not os.path.exists(SCREEN_DUMP_LOCATION):
                os.makedirs(SCREEN_DUMP_LOCATION)
            for ix, handle in enumerate(self.browser.window_handles):
                self._windowid = ix
                self.browser.switch_to_window(handle)
                self.take_screenshot()
                self.dump_html()
        self.browser.quit()
        self.display.stop()
        
    def take_screenshot(self):
        filename = self._get_filename() + '.png'
        print('screenshotting to', filename)
        self.browser.get_screenshot_as_file(filename)

    def dump_html(self):
        filename = self._get_filename() + '.html'
        print('dumping page HTML to', filename)
        with open(filename, 'w') as f:
            output = self.browser.page_source.encode(
                                                'ascii', 'ignore'
                                            ).decode(
                                                'ascii'
                                            )
            f.write(output)

    def _get_filename(self):
        timestamp = datetime.now().isoformat().replace(':', '.')[:19]
        return '{folder}/{classname}.{method}-window{windowid}-{timestamp}'.format(
            folder = SCREEN_DUMP_LOCATION,
            classname = self.__class__.__name__,
            method = self._testMethodName,
            windowid = self._windowid,
            timestamp=timestamp
        )