from flask.ext.testing import LiveServerTestCase
import mockredis
from splinter import Browser

from helix import main
from helix.constants import redis_constant
from helix.constants import sql_constant
from helix.db.sql_manager import SQLManager
from test.test_helpers import test_db_session, reset_db_session


class SiteCharacterizationDynamicFormTestCase(LiveServerTestCase):
    def create_app(self):
        redis_constant.redis_store = mockredis.mock_redis_client()
        sql_constant.sql_session_maker = lambda: SQLManager.get_sql_session_maker('postgres://pivotal:@localhost/test')

        app = main.app
        app.config['TESTING'] = True
        app.config['LIVESERVER_PORT'] = 8943
        return app

    @classmethod
    def setUpClass(cls):
        cls.test_db_session = test_db_session()
        cls.browser = Browser('phantomjs')

    @classmethod
    def tearDownClass(cls):
        cls.test_db_session.close_all()
        cls.browser.quit()

    def setUp(self):
        self.browser.visit(self.get_server_url() + '/site_characterization/')

    def tearDown(self):
        reset_db_session(self.test_db_session)
        self.browser.cookies.delete()

    def test_selecting_exposure_B_to_C_shows_transition_distance(self):
        assert not self.browser.is_text_present("Exposure Transition Distance")
        self.browser.select('exposure_category', 'B to C')
        assert self.browser.is_text_present("Exposure Transition Distance")
        self.browser.select('exposure_category', 'D')
        assert not self.browser.is_text_present("Exposure Transition Distance")

    def test_showing_and_hiding_tooltip_for_anchor_type(self):
        assert not self.browser.is_text_present('OMG anchors are compatible with TPO and PVC roof membranes.')
        self.browser.find_by_css('#anchor_type_row i.icon-info-circled').first.click()
        assert self.browser.is_text_present('OMG anchors are compatible with TPO and PVC roof membranes.')
        self.browser.find_by_css('body').first.click()
        assert not self.browser.is_text_present('OMG anchors are compatible with TPO and PVC roof membranes.')

    def test_showing_and_hiding_tooltip_for_importance_factor(self):
        assert not self.browser.is_text_present('Use 1.5 for essential facilities such as: Hospitals, Police, Fire & Rescue stations & Designated emergency shelters. All other structures should use 1.0.')
        self.browser.find_by_css('#importance_factor_row i.icon-info-circled').first.click()
        assert self.browser.is_text_present('Use 1.5 for essential facilities such as: Hospitals, Police, Fire & Rescue stations & Designated emergency shelters. All other structures should use 1.0.')
        self.browser.find_by_css('body').first.click()
        assert not self.browser.is_text_present('Use 1.5 for essential facilities such as: Hospitals, Police, Fire & Rescue stations & Designated emergency shelters. All other structures should use 1.0.')
