# -*- coding: utf-8 -*-

""" Sahana Eden Assessments Model

    @copyright: 2012-2015 (c) Sahana Software Foundation
    @license: MIT

    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
"""

__all__ = ("S3Assess24HModel",
           "S3AssessBuildingModel",
           "S3AssessCanvassModel",
           )

from gluon import *
from gluon.storage import Storage
from gluon.tools import callback
from ..s3 import *

T = current.T

# Common to both Building & Canvass
assess_property_type_opts = {
    1 : T("Single Family"),
    2 : T("Multi-Family/Apts"),
    3 : T("Residence is Vacation Home"),
    4 : T("Business"),
    }

# =============================================================================
class S3Assess24HModel(S3Model):
    """
        IFRC 24H Assessment form
    """

    names = ("assess_24h",
             )

    def model(self):

        T = current.T
        s3 = current.response.s3

        if s3.bulk:
            # Don't default the Team leader name for Bulk Imports
            default_person = None
        else:
            default_person = current.auth.s3_logged_in_person()

        # ---------------------------------------------------------------------
        # IFRC 24H Assessment
        #
        tablename = "assess_24h"
        self.define_table(tablename,
                          self.pr_person_id(
                            default = default_person,
                            label = ("Name of Assessment Team Leader"),
                            ),
                          s3_date(default = "now"),
                          self.gis_location_id(
                            widget = S3LocationSelector(show_map = False),
                            ),
                          Field("inhabitants", "integer",
                                label = T("Approximate number of inhabitants"),
                                ),
                          Field("inhabitants", "integer",
                                label = T("Approximate number of inhabitants"),
                                ),
                          self.pr_person_id("contact_id",
                            comment = None,
                            label = ("Name of contact person in the community"),
                            requires = IS_ADD_PERSON_WIDGET2(),
                            widget = S3AddPersonWidget2(),
                            ),
                          Field("injured", "integer",
                                label = T("# Injured"),
                                ),
                          Field("dead", "integer",
                                label = T("# Dead"),
                                ),
                          Field("missing", "integer",
                                label = T("# Missing"),
                                ),
                          Field("minor_damage", "integer",
                                label = T("# Minor Damage"),
                                ),
                          Field("moderate_damage", "integer",
                                label = T("# Moderate Damage"),
                                ),
                          Field("destroyed", "integer",
                                label = T("# Destroyed"),
                                ),
                          # tbc if-useful
                          *s3_meta_fields())

        # CRUD Strings
        s3.crud_strings[tablename] = Storage(
            label_create = T("Create Assessment"),
            title_display = T("Assessment Details"),
            title_list = T("Assessments"),
            title_update = T("Edit Assessment"),
            label_list_button = T("List Assessments"),
            label_delete_button = T("Delete Assessment"),
            msg_record_created = T("Assessment added"),
            msg_record_modified = T("Assessment updated"),
            msg_record_deleted = T("Assessment deleted"),
            msg_list_empty = T("No Assessments found")
        )

        # ---------------------------------------------------------------------
        # Pass names back to global scope (s3.*)
        #
        return dict()

# =============================================================================
class S3AssessBuildingModel(S3Model):
    """
        Building Damage Assessment form
    """

    names = ("assess_building",
             "assess_building_rheader",
             )

    def model(self):

        T = current.T
        messages = current.messages
        NONE = messages["NONE"]
        UNKNOWN_OPT = messages.UNKNOWN_OPT

        # ---------------------------------------------------------------------
        # Building Assessment
        #
        approved_opts = {
            1 : T("Approved"),
            2 : T("Rejected"),
            }
        assess_status_opts = {
            1 : T("Assessed"),
            2 : T("Ready to Start"),
            3 : T("In-Progress"),
            4 : T("Finished"),
            }
        assess_status2_opts = {
            1 : T("N/A"),
            2 : T("To be done"),
            3 : T("In-Progress"),
            4 : T("Finished"),
            }
        assess_priority_opts = {
            1 : T("High"),
            2 : T("Medium"),
            3 : T("Low"),
            }
        building_status_opts = {
            1 : T("Green"),
            2 : T("Yellow"),
            3 : T("Red (unsafe)"),
            }
        on_off_opts = {
            1 : T("On"),
            2 : T("Off"),
            }
        ownership_opts = {
            1: T("Rent"),
            2: T("Own"),
            }
        yes_no_opts = {
            1: T("Yes"),
            2: T("No"),
            }

        assess_additional_needs_opts = {
            1 : T("Shelter"),
            2 : T("Information -Mold -FEMA -Legal"),
            3 : T("Other"),
            4 : T("Food"),
            }

        assess_construction_type_opts = {
            1 : T("Concrete"),
            2 : T("Brick"),
            3 : T("Wood Frame"),
            4 : T("Metal Stud"),
            4 : T("Other"),
            }

        assess_damage_opts = {
            1 : T("Primarily Flood"),
            2 : T("Wind/Wind driven rain"),
            3 : T("Other"),
            }

        assess_insurance_opts = {
            1 : T("Property"),
            2 : T("Flood (Structure)"),
            3 : T("Wind/Hurricane"),
            4 : T("Sewer Back-up"),
            5 : T("Flood (Contents)"),
            6 : T("NONE"),
            }

        #assess_tools_opts = {
        #    1 : T("Pump/Hoses"),
        #    2 : T("Flat bar"),
        #    3 : T("Dust pan"),
        #    4 : T("Large pails"),
        #    5 : T("Long pry bar"),
        #    6 : T("Brooms"),
        #    7 : T("Shovels"),
        #    8 : T("Pick axe"),
        #    9 : T("Trash bags"),
        #    10 : T("Wheelbarrows"),
        #    11 : T("Hammers"),
        #    12 : T("Cleaning Supplies"),
        #    13 : T("Crowbar"),
        #    14 : T("Sledgehammer"),
        #    15 : "",
        #    16 : T("Nail pullers/cat's claws"),
        #    17 : T("Generator"),
        #    18 : T("Screwdrivers"),
        #    19 : T("Chalk line"),
        #    20 : T("Portable lights"),
        #    21 : T("Wrench"),
        #    22 : T("Sawzall"),
        #    23 : T("Extension cords"),
        #    24 : "",
        #    25 : T("Utility knives"),
        #    26 : T("Headlamps"),
        #    21 : "",
        #    }

        #assess_mold_removal_opts = {
        #    1 : T("Wire brush"),
        #    2 : T("Demolding solution"),
        #    3 : T("Tyvek suits"),
        #    4 : T("Grinder"),
        #    5 : T("Paint brushes"),
        #    6 : T("Goggles"),
        #    7 : T("Shop vac + HEPA filter"),
        #    8 : T("Cloth rags"),
        #    9 : T("Rubber gloves"),
        #    10 : T("Ladder/step stool"),
        #    11 : T("Kllz"),
        #    #12 : T("Ladder/step stool""),
        #    }

        #assess_personal_protection_opts = {
        #    1 : T("N95 Dust masks"),
        #    2 : T("Rubber Boots"),
        #    4 : T("Respirators"),
        #    5 : T("Work gloves"),
        #    7 : T("Safety glasses"),
        #    8 : T("Hard hats"),
        #    10 : T("Boots"),
        #    11 : T("Ear plugs"),
        #    }

        #assess_skills_required_opts = {
        #    1 : T("Pump equipment"),
        #    2 : T("Mold removal"),
        #    3 : T("Demolition/gutting"),
        #    4 : T("Construction"),
        #    }

        #assess_special_skills_opts = {
        #    1 : T("Plumber"),
        #    2 : T("Engineer"),
        #    3 : T("Electrician"),
        #    4 : T("Other"),
        #    }

        assess_vulnerability_opts = {
            1 : T("Elderly"),
            2 : T("Disabled"),
            3 : T("Small Children"),
            4 : T("Single Female Head of Household"),
            }

        assess_work_requested_opts = {
            1 : T("Pump out water"),
            2 : T("Mud/sand removal"),
            3 : T("Demolition/Gutting"),
            4 : T("Clean up debris"),
            5 : T("Mold removal"),
            6 : T("Sanitization"),
            }

        tablename = "assess_building"
        self.define_table(tablename,
                          Field("database_id", "integer",
                                label=T("Database ID")),
                          Field("status", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_status_opts)
                                        ),
                                represent = lambda opt:
                                    assess_status_opts.get(opt,
                                                            UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=4, **attr),
                                label=T("Status")),
                          Field("status_gutting", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_status2_opts)
                                        ),
                                represent = lambda opt:
                                    assess_status2_opts.get(opt,
                                                            UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=4, **attr),
                                label=T("Gutting Status")),
                          Field("status_mold", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_status2_opts)
                                        ),
                                represent = lambda opt:
                                    assess_status2_opts.get(opt,
                                                            UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=4, **attr),
                                label=T("Mold Status")),
                          s3_comments("mold_notes",
                                      comment=None,
                                      label=T("Mold Notes")),
                          Field("priority", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_priority_opts)
                                        ),
                                represent = lambda opt:
                                    assess_priority_opts.get(opt,
                                                            UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=3, **attr),
                                label=T("Priority")),
                          s3_date(label=T("Intake Date")),
                          Field("assessor1",
                                represent = lambda v: v or NONE,
                                label=T("Assessor 1")),
                          Field("assessor2",
                                represent = lambda v: v or NONE,
                                label=T("Assessor 2")),
                          Field("name",
                                represent = lambda v: v or NONE,
                                label=T("Name")),
                          Field("phone",
                                requires=IS_EMPTY_OR(s3_phone_requires),
                                represent = lambda v: v or NONE,
                                label=T("Phone Number")),
                          Field("contact_other",
                                represent = lambda v: v or NONE,
                                label=T("Other Contact Information")),
                          self.gis_location_id(),
                          Field("homeowner_availability",
                                represent = lambda v: v or NONE,
                                label=T("Homeowner Availability")),
                          Field("type_of_property", "list:integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_property_type_opts,
                                                        multiple=True)
                                        ),
                                represent = lambda ids: \
                                    assess_multi_type_represent(ids,
                                                                assess_property_type_opts),
                                widget = lambda f, v, **attr: \
                                    CheckboxesWidgetS3.widget(f, v, cols=4, **attr),
                                label=T("Type of Property")),
                          Field("inhabitants", "integer",
                                represent = lambda v: v or NONE,
                                label=T("# of Inhabitants")),
                          Field("year_built", "integer",
                                requires = IS_EMPTY_OR(
                                            IS_INT_IN_RANGE(1800, 2012)
                                            ),
                                represent = lambda v: v or NONE,
                                label=T("Year Built")),
                          Field("current_residence", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(yes_no_opts)
                                        ),
                                represent = lambda opt: \
                                    yes_no_opts.get(opt,
                                                    UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=2, **attr),
                                label=T("Current Residence")),
                          Field("ownership", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(ownership_opts)
                                        ),
                                represent = lambda opt: \
                                    ownership_opts.get(opt,
                                                        UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=2, **attr),
                                label=T("Ownership")),
                          Field("intention",
                                represent = lambda v: v or NONE,
                                label=T("Intention to Stay Home")),
                          Field("vulnerability", "list:integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_vulnerability_opts,
                                                        multiple=True)
                                        ),
                                represent = lambda ids: \
                                    assess_multi_type_represent(ids,
                                                                assess_vulnerability_opts),
                                widget = lambda f, v, **attr: \
                                    CheckboxesWidgetS3.widget(f, v, cols=4, **attr),
                                label=T("Vulnerabilities")),
                          Field("building_status", "integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(building_status_opts)
                                        ),
                                represent = lambda opt:
                                    building_status_opts.get(opt,
                                                                UNKNOWN_OPT),
                                widget = lambda f, v, **attr: \
                                    SQLFORM.widgets.radio.widget(f, v, cols=3, **attr),
                                label=T("Based on the DOB/FEMA sticker, the property is")),
                          Field("insurance", "list:integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_insurance_opts,
                                                        multiple=True)
                                        ),
                                represent = lambda ids: \
                                    assess_multi_type_represent(ids,
                                                                assess_insurance_opts),
                                widget = lambda f, v, **attr: \
                                    CheckboxesWidgetS3.widget(f, v, cols=3, **attr),
                                label=T("Type of Insurance")),
                          Field("work_requested", "list:integer",
                                requires=IS_EMPTY_OR(
                                            IS_IN_SET(assess_work_requested_opts,
                                                        multiple=True)
                                        ),