import unittest
from helix.constants.module_type import ModuleType
import mockredis
from splinter import Browser
from helix import main
from helix.constants import redis_constant, sql_constant
from helix.constants.system_type import SystemType
from helix.constants.anchor_type import AnchorType
from helix.models.panel import PanelWarnings
from test.integration.integration_test_helpers import assert_step_is_active, assert_step_is_completed
from test.test_helpers import *


class SiteSummaryTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.test_db_session = test_db_session()

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

    def setUp(self):
        redis_constant.redis_store = mockredis.mock_redis_client()
        sql_constant.sql_session_maker = lambda: self.test_db_session
        self.app = main.app.test_client()
        self.browser = Browser('flask', app=main.app)
        self.browser.visit('/summary/')

    def tearDown(self):
        reset_db_session(self.test_db_session)

    def fill_in_site_characterization_data(self, system_type=SystemType.singleTilt, module_type=ModuleType.Cell96, ul_warning=False):
        self.browser.visit('/site_characterization/')
        self.browser.fill('project_name', 'Test Project Name')
        self.browser.fill('building_width', '200')
        self.browser.fill('building_height', '30.254')
        self.browser.fill('building_length', '75.5')
        self.browser.fill('wind_speed', '125' if not ul_warning else '160')
        self.browser.fill('exposure_category', 'C')
        self.browser.fill('ballast_block_weight', '13')
        self.browser.fill('building_parapet_height', '0')
        self.browser.fill('max_system_pressure', '10')
        self.browser.fill('system_type', system_type.value)
        self.browser.fill('anchor_type', AnchorType.EcoFasten.value)
        self.browser.fill('module_type', module_type.value)
        self.browser.fill('design_spectral_response', '1.02')
        self.browser.find_by_value('Next').first.click()
        self.browser.visit('/summary/')

    def assert_summary_values_column_data(self, column, label, value):
        eq_(self.browser.find_by_css('#summary_values_table tr:nth-child(1) th:nth-child(%d)' % column).first.html,
            label)
        eq_(self.browser.find_by_css('#summary_values_table tr:nth-child(2) td:nth-child(%d)' % column).first.text,
            value)

    def test_shows_summary_table_if_user_has_uploaded_data(self):
        self.fill_in_site_characterization_data()
        assert len(self.browser.find_by_css("#summary_table")) > 0
        eq_(len(self.browser.find_by_css("#summary_table tr.alternating_row_color")), 13)

    def test_does_not_show_tables_if_user_has_not_uploaded_site_data(self):
        assert len(self.browser.find_by_css("#summary_table")) == 0

    def test_viewing_summary_table_without_site_characteristics_marks_as_incomplete(self):
        assert_step_is_active(self.browser, 2, True)
        assert_step_is_completed(self.browser, 2, False)
        self.fill_in_site_characterization_data()
        assert_step_is_active(self.browser, 2, True)
        assert_step_is_completed(self.browser, 2, True)

    def test_clicking_next_goes_to_array_summary(self):
        self.fill_in_site_characterization_data()

        self.browser.click_link_by_partial_text('Next')
        assert_step_is_active(self.browser, 3)
        assert_step_is_active(self.browser, 2, False)

    def test_shows_summary_values(self):
        self.fill_in_site_characterization_data()
        self.assert_summary_values_column_data(1, 'L<sub>B</sub> - Building Scaling Factor (feet)', '30.25')
        self.assert_summary_values_column_data(2, 'K<sub>Z</sub> - Site Wind Pressure Factor', '0.98')
        self.assert_summary_values_column_data(3, 'q<sub>z</sub> - Site Wind Pressure (psf)', '33.46')

    def test_clicking_back_goes_to_site_characterization(self):
        self.browser.click_link_by_partial_text('Back')

        assert_step_is_active(self.browser, 1)

    def test_shows_minimum_array_size_single_tilt(self):
        self.fill_in_site_characterization_data()
        eq_(len(self.browser.find_by_css("#summary_table tr:last_child td")), 12)
        eq_(self.browser.find_by_css("#summary_table tr:last_child td:first_child").first.text,
            "Minimum Array Size by Zone")

    def test_shows_minimum_array_size_dual_tilt(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        eq_(len(self.browser.find_by_css("#summary_table tr:last_child td")), 6)
        eq_(self.browser.find_by_css("#summary_table tr:last_child td:first_child").first.text,
            "Minimum Array Size by Zone (Tents)")

    def test_shows_dual_tilt_images_for_dual_tilt_system(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        eq_(len(self.browser.find_by_css('.image_container a')), 2)
        assert 'panel_types_dual_tilt' in self.browser.find_by_css('.image_container a:nth-child(1)').first['data-featherlight']
        assert 'wind_zones_dual_tilt' in self.browser.find_by_css('.image_container a:nth-child(2)').first['data-featherlight']

    def test_shows_single_tilt_images_for_single_tilt_system(self):
        self.fill_in_site_characterization_data(system_type=SystemType.singleTilt)
        eq_(len(self.browser.find_by_css('.image_container a')), 3)
        assert 'panel_types_single_tilt' in self.browser.find_by_css('.image_container a:nth-child(1)').first['data-featherlight']
        assert 'wind_zones_single_tilt_north' in self.browser.find_by_css('.image_container a:nth-child(2)').first['data-featherlight']
        assert 'wind_zones_single_tilt_south' in self.browser.find_by_css('.image_container a:nth-child(3)').first['data-featherlight']

    def test_shows_warning_message(self):
        self.fill_in_site_characterization_data(ul_warning=True)
        summary_warning_selector = self.browser.find_by_css('.summary_warning')
        eq_(len(summary_warning_selector), 1)
        summary_warning_message = summary_warning_selector.first.text
        eq_(summary_warning_message, PanelWarnings.MaxPsf.value)

    def test_shows_cells_with_warning_with_different_style(self):
        self.fill_in_site_characterization_data(ul_warning=True)
        eq_(len(self.browser.find_by_css('.table_horizontal_borders.warning')), 3)
        assert 'warning' in self.browser.find_by_css('#summary_table tr:nth-child(3) td:nth-child(3)').first['class']
        assert 'warning' in self.browser.find_by_css('#summary_table tr:nth-child(4) td:nth-child(2)').first['class']
        assert 'warning' in self.browser.find_by_css('#summary_table tr:nth-child(5) td:nth-child(2)').first['class']
