from flask.ext.wtf.file import FileField

from helix.constants.anchor_type import AnchorType
from helix.constants.exposure_category import ExposureCategory
from helix.constants.module_type import ModuleType
from helix.constants.system_type import SystemType
from wtforms import SelectField, StringField, BooleanField
from wtforms.fields.html5 import DecimalField, IntegerField
from wtforms.validators import NumberRange, DataRequired

from helix.forms.conditional_validator import ConditionalValidator
from helix.forms.grouped_form import GroupedForm


class InputForm(GroupedForm):
    project_name = StringField('Project Name', validators=[DataRequired(message='Project Name is required.')],
                               render_kw={'group': 'project_info'})
    building_height = DecimalField('Building Height (ft)', places=1, validators=[NumberRange(0, None)],
                                   render_kw={'group': 'site_info'})
    building_width = DecimalField('Building Width (ft)', places=1, validators=[NumberRange(0, None)],
                                  render_kw={'group': 'site_info'})
    building_length = DecimalField('Building Length (ft)', places=1, validators=[NumberRange(0, None)],
                                   render_kw={'group': 'site_info'})
    building_parapet_height = DecimalField('Parapet Height (ft)', places=1, validators=[NumberRange(0, None)],
                                           render_kw={'group': 'site_info'})
    wind_speed = IntegerField('Wind Speed (ASCE 7-10) (mph)',
                              validators=[NumberRange(100, 200)],
                              render_kw={'group': 'site_info',
                                         'link': {'text': 'Look up',
                                                  'href': 'http://windspeed.atcouncil.org/'}})
    exposure_category = SelectField('Exposure Category',
                                    choices=[(ExposureCategory.B.value, ExposureCategory.B.value),
                                             (ExposureCategory.B_C.value, "B to C"),
                                             (ExposureCategory.C_B.value, "C to B"),
                                             (ExposureCategory.C.value, ExposureCategory.C.value),
                                             (ExposureCategory.D.value, ExposureCategory.D.value)],
                                    default=ExposureCategory.default_value(),
                                    render_kw={'group': 'site_info',
                                               'link': {'text': 'More info',
                                                        'href': '/exposure_categories'}
                                               })
    exposure_category_transition_distance = IntegerField('Exposure Transition Distance (ft)',
                                                         default=0,
                                                         validators=[ConditionalValidator('exposure_category',
                                                                                          ['B to C', 'C to B'],
                                                                                          NumberRange(1, None))],
                                                         render_kw={'group': 'site_info'})
    ballast_block_weight = DecimalField('Ballast Block Weight (lbs)',
                                        validators=[NumberRange(12, 20)],
                                        default=14,
                                        places=1,
                                        render_kw={'group': 'site_info'})
    max_system_pressure = DecimalField('Max Allowable System Pressure (psf)',
                                       places=1,
                                       validators=[NumberRange(0, None)],
                                       default=12,
                                       render_kw={'group': 'site_info'})
    system_type = SelectField('System Type',
                              choices=[(SystemType.singleTilt.value, SystemType.singleTilt.display_name()),
                                       (SystemType.dualTilt.value, SystemType.dualTilt.display_name())],
                              default=SystemType.default_value(),
                              render_kw={'group': 'project_info'})
    module_type = SelectField('Module Type',
                              choices=[(ModuleType.Cell128.value, ModuleType.Cell128.value),
                                       (ModuleType.PSeries.value, ModuleType.PSeries.value),
                                       (ModuleType.Cell96.value, ModuleType.Cell96.value)],
                              default=ModuleType.default_value(),
                              render_kw={'group': 'project_info'})
    anchor_type = SelectField('Anchor Type',
                              choices=[(AnchorType.OMG_PowerGrip.value, AnchorType.OMG_PowerGrip.value),
                                       (AnchorType.OMG_PowerGrip_Plus.value, AnchorType.OMG_PowerGrip_Plus.value),
                                       (AnchorType.EcoFasten.value, AnchorType.EcoFasten.value)],
                              default=AnchorType.default_value(),
                              render_kw={'group': 'site_info', 'tooltip': 'OMG anchors are compatible with TPO and PVC roof membranes.<br>EcoFasten anchors are compatible with Built Up Roofing (BUR), Hot Tar, Sips Panels and membrane type roofs.'})
    design_spectral_response = DecimalField('Design Spectral Response Acceleration (S<sub>DS</sub>) (g)',
                                            places=1, validators=[NumberRange(0, 5)],
                                            render_kw={'group': 'site_info',
                                                       'link': {'text': 'Look up',
                                                                'href': 'http://earthquake.usgs.gov/designmaps/us/application.php'
                                                                }
                                                       })
    importance_factor = SelectField('Seismic Importance Factor (I<sub>p</sub>)',
                                    choices=[('1', 1), ('1.5', 1.5)],
                                    default=1,
                                    render_kw={'group': 'site_info', 'tooltip': 'Use 1.5 for essential facilities such as: Hospitals, Police, Fire & Rescue stations & Designated emergency shelters. All other structures should use 1.0.'})


class ArrayForm(GroupedForm):
    file_upload = FileField('System Data (txt)', render_kw={'group': 'array_info', 'class': 'system_upload'})
    dxf_upload = FileField('Cad File (dxf)', render_kw={'group': 'dxf_file', 'class': 'system_upload'})


class TestDXFForm(GroupedForm):
    dxf_upload = FileField('Cad File (dxf)', render_kw={'group': 'array_info', 'class': 'system_upload'})
    show_wind_zones = BooleanField('Show Wind Zones', default=True, render_kw={'group': 'array_info'})
