import json
import unittest

import flask
import mockredis
from numpy.testing import assert_array_equal
from splinter import Browser

from helix import main
from helix.constants import redis_constant
from helix.constants import sql_constant
from helix.constants.anchor_type import AnchorType
from helix.constants.dxf_validation import INVALID_DUAL_TILT_DESIGN
from helix.constants.file_validation_error import FileValidationMessage
from helix.constants.module_type import ModuleType
from helix.constants.seismic_anchor_validation_error import SeismicAnchorValidationError
from helix.constants.subarray import SUBARRAY_SIZE_BIG
from helix.constants.system_type import SystemType
from helix.session_manager import SessionManager
from test.integration.integration_test_helpers import assert_step_is_active, assert_step_is_completed
from test.test_helpers import *


class ArraySummaryTest(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('/array_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):
        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')
        self.browser.fill('building_length', '75.5')
        self.browser.fill('wind_speed', '125')
        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('/array_summary/')

    def upload_csv_file(self, file='test/fixtures/input_single_tilt.csv'):
        self.browser.visit('/array_summary/')
        self.browser.attach_file('file_upload', file)
        self.browser.find_by_value('Next').first.click()

    def upload_dxf_file(self, file='test/fixtures/dxf/input_dual_tilt_96_cell.dxf'):
        self.browser.visit('/array_summary/')
        self.browser.attach_file('dxf_upload', file)
        self.browser.find_by_value('Next').first.click()

    def assert_summary_values_column_data(self, column, label, value):
        eq_(self.browser.find_by_css('.array_summary .summary_item:nth-child(%d) .item_title' % column).first.html,
            label)
        eq_(self.browser.find_by_css('.array_summary .summary_item:nth-child(%d) .item_body' % column).first.text,
            value)

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

    def test_does_not_show_next_button(self):
        self.fill_in_site_characterization_data()
        next_button = self.browser.find_by_value('Next')
        assert next_button.first._control.attrib['style'] == 'display: none;'

    def test_without_csv_or_dxf_form_submission_requires_csv_attachment(self):
        self.fill_in_site_characterization_data()
        self.browser.find_by_value('Next').first.click()
        assert self.browser.is_text_present("Please provide a .txt file!")

    def test_with_invalid_csv_file(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        self.browser.attach_file('file_upload', 'test/fixtures/input_single_tilt.csv')
        self.browser.find_by_value('Next').first.click()
        eq_(self.browser.find_by_id('error_message_txt').first.value, 'Invalid wind zone for Dual Tilt System on line 1')

    def test_shows_subarray_summary_table_if_user_has_uploaded_data(self):
        self.fill_in_site_characterization_data()
        self.upload_csv_file(file='test/fixtures/input_single_tilt_csv_for_bom.csv')
        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_indicate_that_user_has_already_uploaded_a_csv_file_after_uploading_csv(self):
        self.fill_in_site_characterization_data()
        self.upload_csv_file(file='test/fixtures/input_single_tilt_csv_for_bom.csv')
        assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
        assert 'test/fixtures/input_single_tilt_csv_for_bom.csv' in self.browser.html

#     def test_dxf_as_cad_file_if_dxf_file_uploaded(self):
#         self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
#         self.upload_dxf_file()
#         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_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()
        assert len(self.browser.find_by_css('.upload_field .error_message')) == 0
        assert 'test/fixtures/dxf/input_dual_tilt_96_cell.dxf' in self.browser.html

    def test_shows_errors_if_dxf_fails_validation(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        self.upload_dxf_file(file="test/fixtures/dxf/invalid_dual_tilt_96_cell_has_unpairable_modules.dxf")
        assert len(self.browser.find_by_id('error_message_dxf')) == 1
        msg = INVALID_DUAL_TILT_DESIGN
        eq_(self.browser.find_by_id('error_message_dxf').first.value, msg)

    def test_shows_errors_if_dxf_has_big_subarrays(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        self.upload_dxf_file(file="test/fixtures/dxf/invalid_dual_tilt_96_cell_subarrays_too_large.dxf")
        assert len(self.browser.find_by_id('error_message_dxf')) == 1
        msg = SUBARRAY_SIZE_BIG
        eq_(self.browser.find_by_id('error_message_dxf').first.value, msg)

    def test_viewing_summary_table_after_uploading_data_updates_navigation_header(self):
        self.fill_in_site_characterization_data()
        self.upload_csv_file(file='test/fixtures/input_single_tilt_csv_for_bom.csv')
        assert_step_is_active(self.browser, 3)
        assert_step_is_completed(self.browser, 3)
        self.browser.click_link_by_partial_text("Back")
        assert_step_is_active(self.browser, 3, False)
        assert_step_is_completed(self.browser, 3)

    def test_viewing_summary_table_before_uploading_data_does_not_update_navigation_header(self):
        assert_step_is_active(self.browser, 3)
        assert_step_is_completed(self.browser, 3, False)
        self.browser.click_link_by_partial_text("Back")
        assert_step_is_active(self.browser, 3, False)
        assert_step_is_completed(self.browser, 3, False)

    def test_next_is_not_available_before_uploading_data(self):
        eq_(len(self.browser.find_by_value('Next')), 0)
        self.fill_in_site_characterization_data()
        self.upload_csv_file()
        eq_(len(self.browser.find_by_value('Next')), 1)

    def test_clicking_next_after_uploading_data_goes_to_power_station_configuration(self):
        self.fill_in_site_characterization_data()
        self.upload_csv_file()

        self.browser.find_by_value('Next').first.click()
        assert_step_is_active(self.browser, 4)
        assert_step_is_active(self.browser, 3, False)

    def test_shows_summary_values(self):
        self.fill_in_site_characterization_data()
        self.upload_csv_file(file='test/fixtures/input_single_tilt_csv_for_bom.csv')
        self.assert_summary_values_column_data(1, 'Total System Weight (lbs)', '217,409')
        self.assert_summary_values_column_data(2, 'Max PSF', '9.36')
        self.assert_summary_values_column_data(3, 'Avg PSF', '5.08')
        self.assert_summary_values_column_data(4, 'Total Anchors', '422')
        self.assert_summary_values_column_data(5, 'Total Ballast', '10,448')

    def test_shows_seismic_placement_interval(self):
        self.fill_in_site_characterization_data(system_type=SystemType.dualTilt)
        self.upload_csv_file(file='test/fixtures/input_small_dual_tilt.csv')
        self.assert_summary_values_column_data(8, 'Seismic Anchor Max. Spacing', '14')

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

        assert_step_is_active(self.browser, 2)

    def test_getting_panel_data(self):
        redis = redis_constant.redis_store
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            session_manager.save_form_submission({
                'project_name': 'Test',
                'building_width': 64,
                'building_height': 24,
                'building_length': 86,
                'wind_speed': 110,
                'exposure_category': 'B',
                'ballast_block_weight': 14,
                'building_parapet_height': 1,
                'max_system_pressure': 10,
                'system_type': SystemType.dualTilt.value,
                'module_type': ModuleType.Cell96.value,
                'anchor_type': AnchorType.EcoFasten.value,
                'design_spectral_response': 1,
            })
            with open('test/fixtures/input_dual_subarray.txt', 'r', newline='') as csv_file:
                session_manager.save_uploaded_file(csv_file.read())
        response = self.app.get('/api/panel_data')
        eq_(response.content_type, "application/json")
        data = json.loads(response.data.decode())
        expected = [
            {'data': {'ballast': 7, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 1, 'panel_type': 2, 'psf': 4.91, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 2, 'panel_type': 2, 'psf': 3.1, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 11, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 3, 'panel_type': 1, 'psf': 6.48, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 11, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 4, 'panel_type': 1, 'psf': 6.48, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 6, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 5, 'panel_type': 1, 'psf': 4.57, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 6, 'panel_type': 2, 'psf': 3.1, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 7, 'panel_type': 4, 'psf': 2.26, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 8, 'panel_type': 3, 'psf': 3.0, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 9, 'panel_type': 3, 'psf': 3.0, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 10, 'panel_type': 4, 'psf': 2.26, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 11, 'panel_type': 2, 'psf': 3.1, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 12, 'panel_type': 3, 'psf': 3.0, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 13, 'panel_type': 3, 'psf': 3.0, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 2, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 14, 'panel_type': 3, 'psf': 3.0, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 6, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 15, 'panel_type': 1, 'psf': 4.57, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 6, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 16, 'panel_type': 1, 'psf': 4.57, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 6, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 17, 'panel_type': 1, 'psf': 4.57, 'seismic_anchors': 0, 'subarray': 7, 'wind_anchors': 0, 'wind_zones': 'B'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 11, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 18, 'panel_type': 1, 'psf': 6.48, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 6, 'cross_trays': 0, 'link_trays': 1, 'panel_id': 19, 'panel_type': 3, 'psf': 4.5, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5},
            {'data': {'ballast': 11, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 20, 'panel_type': 1, 'psf': 6.48, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 0, 'wind_zones': 'A'}, 'height': 1, 'width': 1.5}
        ]

        # Removing these keys as they get calculated differently in CircleCI
        entries_to_remove = ('x', 'y')
        for k in entries_to_remove:
            for entry in data['panel_data']:
                entry.pop(k, None)
        assert_array_equal(data['panel_data'], expected)

    def test_posting_user_seismic_anchors(self):
        redis = redis_constant.redis_store
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            session_manager.save_form_submission({
                'project_name': 'Test',
                'building_width': 200,
                'building_height': 30,
                'building_length': 75.5,
                'wind_speed': 125,
                'exposure_category': 'C',
                'ballast_block_weight': 13,
                'building_parapet_height': 0,
                'max_system_pressure': 0,
                'system_type': SystemType.singleTilt.value,
                'anchor_type': AnchorType.EcoFasten.value,
                'design_spectral_response': 1.02,
            })
            with open('test/fixtures/input_single_tilt.csv', 'r', newline='') as csv_file:
                session_manager.save_uploaded_file(csv_file.read())
        result = self.app.post("/api/update_panel_data", content_type='application/json', data=json.dumps([
            {"panel_id": 1, "seismic_anchors": 2},
            {"panel_id": 2, "seismic_anchors": 0},
            {"panel_id": 3, "seismic_anchors": 0},
            {"panel_id": 4, "seismic_anchors": 0},
            {"panel_id": 5, "seismic_anchors": 0},
            {"panel_id": 6, "seismic_anchors": 0},
            {"panel_id": 7, "seismic_anchors": 0},
            {"panel_id": 8, "seismic_anchors": 0},
            {"panel_id": 9, "seismic_anchors": 0},
            {"panel_id": 10, "seismic_anchors": 0},
            {"panel_id": 11, "seismic_anchors": 0},
            {"panel_id": 12, "seismic_anchors": 0},
            {"panel_id": 13, "seismic_anchors": 0},
            {"panel_id": 14, "seismic_anchors": 0},
            {"panel_id": 15, "seismic_anchors": 0},
            {"panel_id": 16, "seismic_anchors": 0},
            {"panel_id": 17, "seismic_anchors": 0},
            {"panel_id": 18, "seismic_anchors": 0},
            {"panel_id": 19, "seismic_anchors": 0},
        ]))
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            eq_(session_manager.user_values().user_override_seismic_anchors(), True)

        expected_result = {
            "status": "success",
            "error": None,
            "panel_data": [
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 1, 'panel_type': 1, 'psf': 1.95, 'seismic_anchors': 2, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 2, 'panel_type': 3, 'psf': 1.88, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 3, 'panel_type': 1, 'psf': 1.95, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 4, 'panel_type': 1, 'psf': 1.95, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 5, 'panel_type': 3, 'psf': 1.88, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 6, 'panel_type': 3, 'psf': 1.88, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 2, 'panel_id': 7, 'panel_type': 3, 'psf': 1.88, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 8, 'panel_type': 1, 'psf': 1.95, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 9, 'panel_type': 2, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 10, 'panel_type': 2, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 11, 'panel_type': 2, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'I'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 12, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 13, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 14, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 15, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 16, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 17, 'panel_type': 4, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 18, 'panel_type': 2, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'H'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
                {'data': {'ballast': 0, 'cross_trays': 0, 'link_trays': 0, 'panel_id': 19, 'panel_type': 2, 'psf': 1.84, 'seismic_anchors': 0, 'subarray': 8, 'wind_anchors': 1, 'wind_zones': 'I'}, 'height': 1, 'width': 1, 'x': 0.0, 'y': 0.0},
            ],
            "subarray_data": [
                {"subarray": 8, "weight": 932, "required_seismic_anchors": 0},
            ],
        }
        received_result = flask.json.loads(result.data)
        eq_(received_result, expected_result)
        eq_(result.content_type, "application/json")

    def test_posting_user_seismic_anchors_with_required_ones(self):
        redis = redis_constant.redis_store
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            session_manager.save_form_submission({
                'project_name': 'Test',
                'building_width': 64,
                'building_height': 24,
                'building_length': 86,
                'wind_speed': 110,
                'exposure_category': 'B',
                'ballast_block_weight': 14,
                'building_parapet_height': 1,
                'max_system_pressure': 10,
                'system_type': SystemType.dualTilt.value,
                'module_type': ModuleType.Cell96.value,
                'anchor_type': AnchorType.EcoFasten.value,
                'design_spectral_response': 1,
            })
            with open('test/fixtures/input_dual_subarray.txt', 'r', newline='') as csv_file:
                session_manager.save_uploaded_file(csv_file.read())
        result = self.app.post("/api/update_panel_data", content_type='application/json', data=json.dumps([
            {"panel_id": 1, "seismic_anchors": 2},
            {"panel_id": 2, "seismic_anchors": 0},
            {"panel_id": 3, "seismic_anchors": 0},
            {"panel_id": 4, "seismic_anchors": 2},
            {"panel_id": 5, "seismic_anchors": 0},
            {"panel_id": 6, "seismic_anchors": 0},
            {"panel_id": 7, "seismic_anchors": 0},
            {"panel_id": 8, "seismic_anchors": 0},
            {"panel_id": 9, "seismic_anchors": 0},
            {"panel_id": 10, "seismic_anchors": 0},
            {"panel_id": 11, "seismic_anchors": 0},
            {"panel_id": 12, "seismic_anchors": 0},
            {"panel_id": 13, "seismic_anchors": 0},
            {"panel_id": 14, "seismic_anchors": 0},
            {"panel_id": 15, "seismic_anchors": 0},
            {"panel_id": 16, "seismic_anchors": 0},
            {"panel_id": 17, "seismic_anchors": 0},
            {"panel_id": 18, "seismic_anchors": 0},
            {"panel_id": 19, "seismic_anchors": 0},
            {"panel_id": 20, "seismic_anchors": 0},
        ]))
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            eq_(session_manager.user_values().user_override_seismic_anchors(), True)

        expected_result = [
            {"subarray": 7, "weight": 2253, "required_seismic_anchors": 4},
            {"subarray": 8, "weight": 674, "required_seismic_anchors": 0},
        ]
        received_result = flask.json.loads(result.data)

        eq_(received_result['subarray_data'], expected_result)
        eq_(result.content_type, "application/json")

    def test_posting_user_seismic_anchors_below_amount(self):
        redis = redis_constant.redis_store
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            session_manager.save_form_submission({
                'project_name': 'Test',
                'building_width': 200,
                'building_height': 300,
                'building_length': 755,
                'wind_speed': 200,
                'exposure_category': 'B',
                'ballast_block_weight': 20,
                'building_parapet_height': 0,
                'max_system_pressure': 100,
                'system_type': SystemType.dualTilt.value,
                'anchor_type': AnchorType.EcoFasten.value,
                'design_spectral_response': 5.0,
            })
            with open('test/fixtures/input_dual_tilt.csv', 'r', newline='') as csv_file:
                session_manager.save_uploaded_file(csv_file.read())
        result = self.app.post("/api/update_panel_data", content_type='application/json', data=json.dumps([
            {"panel_id": 1, "seismic_anchors": 0},
            {"panel_id": 2, "seismic_anchors": 0},
            {"panel_id": 3, "seismic_anchors": 0},
            {"panel_id": 4, "seismic_anchors": 0},
            {"panel_id": 5, "seismic_anchors": 0},
            {"panel_id": 6, "seismic_anchors": 0},
            {"panel_id": 7, "seismic_anchors": 0},
        ]))
        with self.app.session_transaction() as session:
            session_manager = SessionManager(session, redis, self.test_db_session)
            eq_(session_manager.user_values().user_override_seismic_anchors(), False)
        expected_result = {
            "status": "error",
            "error": SeismicAnchorValidationError.TooFewAnchors.value,
            "panel_data": None,
            "subarray_data": [
                {"subarray": 1, "weight": 2722, "required_seismic_anchors": 6},
            ],
        }
        eq_(flask.json.loads(result.data), expected_result)
        eq_(result.content_type, "application/json")
