import time
from flask.ext.testing import LiveServerTestCase
import mockredis
from splinter import Browser
from helix import main
from helix.constants import sql_constant
from helix.constants.module_type import ModuleType
from helix.constants.system_type import SystemType
from helix.constants.anchor_type import AnchorType
from helix.constants import redis_constant
from test.test_helpers import *


class ArraySummaryDynamicTest(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('/array_summary/')

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

    def fill_in_site_characterization_data(self,
                                           system_type=SystemType.singleTilt,
                                           module_type=ModuleType.Cell96):
        self.browser.visit(self.get_server_url() + '/site_characterization/')
        self.browser.fill('project_name', 'Test Project Name')
        self.browser.fill('building_width', '200')
        self.browser.fill('building_height', '30')
        self.browser.fill('building_length', '75.5')
        self.browser.fill('wind_speed', '125')
        self.browser.fill('ballast_block_weight', '13')
        self.browser.fill('building_parapet_height', '0')
        self.browser.fill('max_system_pressure', '10')
        self.browser.select('system_type', system_type.value)
        self.browser.select('anchor_type', AnchorType.EcoFasten.value)
        self.browser.select('module_type', module_type.value)
        self.browser.fill('design_spectral_response', '1.02')
        self.browser.find_by_value('Next').first.click()
        self.browser.visit(self.get_server_url() + '/array_summary/')

    def upload_cad_block_file(self, file):
        self.browser.attach_file('file_upload', file)

    def upload_dxf_file(self, file='test/fixtures/dxf/input_single_tilt_96_cell.dxf'):
        self.browser.attach_file('dxf_upload', file)

    def test_shows_subarray_summary_table_if_user_has_uploaded_data(self):
        self.fill_in_site_characterization_data()
        self.upload_cad_block_file('test/fixtures/input_single_tilt_csv_for_bom.csv')
        time.sleep(3)
        assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
        assert len(self.browser.find_by_css("#seismic_anchor_table")) > 0
        eq_(len(self.browser.find_by_css("#seismic_anchor_table tr")), 4)
        eq_(len(self.browser.find_by_css("#seismic_anchor_table tr:nth-child(2) td")), 13)

    def test_shows_errors_if_user_tries_to_upload_select_invalid_data(self):
        self.fill_in_site_characterization_data()
        self.upload_cad_block_file('test/fixtures/invalid_wind_zone.csv')
        time.sleep(3)
        assert len(self.browser.find_by_id('error_message_txt')) > 0
        assert len(self.browser.find_by_css("#seismic_anchor_table")) == 0

#     def test_shows_subarray_summary_table_after_user_uploads_dxf(self):
#         self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
#         self.upload_dxf_file('test/fixtures/dxf/input_dual_tilt_96_cell.dxf')
#         assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
#         assert len(self.browser.find_by_css("#seismic_anchor_table")) > 0
#         eq_(len(self.browser.find_by_css("#seismic_anchor_table tr")), 4)
#         eq_(len(self.browser.find_by_css("#seismic_anchor_table tr:nth-child(2) td")), 8)

    def test_indicates_that_user_has_already_uploaded_a_csv_file_after_uploading(self):
        self.fill_in_site_characterization_data()
        self.upload_cad_block_file('test/fixtures/input_single_tilt_csv_for_bom.csv')
        time.sleep(3)
        assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
        assert 'input_single_tilt_csv_for_bom.csv' in self.browser.html

    def test_adding_seismic_anchor_updates_subarray_summary(self):
        self.fill_in_site_characterization_data()
        self.upload_cad_block_file('test/fixtures/input_single_tilt_csv_for_bom.csv')
        time.sleep(3)
        assert self.browser.is_element_visible_by_css('#arrayCanvas')
        self.browser.execute_script('arrayVisualization.selectPanel(1631);')
        self.browser.find_by_css('#add_seismic').click()
        assert self.browser.find_by_css('#current_anchors td:nth-child(14)').text == '1'
        self.browser.find_by_css('#add_seismic').click()
        assert self.browser.find_by_css('#current_anchors td:nth-child(14)').text == '2'

    def test_removig_seismic_anchor_updates_subarray_summary(self):
        self.fill_in_site_characterization_data()
        self.upload_cad_block_file('test/fixtures/input_single_tilt_csv_for_bom.csv')
        time.sleep(3)
        assert self.browser.is_element_visible_by_css('#arrayCanvas')
        self.browser.execute_script('arrayVisualization.selectPanel(1631);')
        self.browser.find_by_css('#add_seismic').click()
        assert self.browser.find_by_css('#current_anchors td:nth-child(14)').text == '1'
        self.browser.find_by_css('#remove_seismic').click()
        assert self.browser.find_by_css('#current_anchors td:nth-child(14)').text == '0'

#     def test_indicate_that_user_has_uploaded_a_dxf_file_after_uploading_dxf(self):
#         self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
#         self.upload_dxf_file('test/fixtures/dxf/input_dual_tilt_96_cell.dxf')
#         assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
#         assert 'input_dual_tilt_96_cell.dxf' in self.browser.html
