from . import base
from . import mixins

import string

from datetime import date

class TransformedRecord(
    mixins.GenericCompensationMixin,
    mixins.GenericIdentifierMixin,
    mixins.GenericPersonMixin,
    mixins.MembershipMixin, mixins.OrganizationMixin, mixins.PostMixin,
    mixins.RaceMixin, mixins.LinkMixin, base.BaseTransformedRecord):

    MAP = {
        'full_name': 'EMPL_NAME',
        'department': 'DEPT_NAME',
        'job_title': 'JOB_TITLE',
        'hire_date': 'LAST_HIRE_DATE',
        'compensation': 'ANNUAL_RATE',
        'gender': 'GENDER',
        'given_race': 'RACE',
        'status': 'FULL-TIME/PART-TIME'
    }

     # The order of the name fields to build a full name.
    # If `full_name` is in MAP, you don't need this at all.
    NAME_FIELDS = ('full_name', )

    # The name of the organization this WILL SHOW UP ON THE SITE,
    #  so double check it!
    ORGANIZATION_NAME = 'UT Southwestern Medical Center'

    # What type of organization is this? This MUST match what we use on the
    # site, double check against salaries.texastribune.org
    ORGANIZATION_CLASSIFICATION = 'University Hospital'

    # When did you receive the data? NOT when we added it to the site.
    DATE_PROVIDED = date(2017, 5, 26)

    # How do they track gender? We need to map what they use to `F` and `M`.
    gender_map = {'Female': 'F', 'Male': 'M', 'Unknown': 'F'}

    # The URL to find the raw data in our S3 bucket.
    URL = ('http://raw.texastribune.org.s3.amazonaws.com/'
           'ut_southwestern_medical/salaries/2017-05/12295_PIR_UTSW_Employees.xlsx')

    # This is how the loader checks for valid people. Defaults to checking to
    # see if `full_name` is empty.
    @property
    def is_valid(self):
        # Adjust to return False on invalid fields. For example:
        return self.full_name.strip() != ''

    @property
    def compensation_type(self):
        if self.status == 'Full-Time':
            return 'FT'
        else:
            return 'PT'

    @property
    def compensation(self):
        comp = self.get_mapped_value('compensation')

        if not comp:
            return 0
        else:
            return comp

    @property
    def person(self):
        name = self.get_name()

        r = {
            'family_name': name.last,
            'given_name': name.first,
            'additional_name': name.middle,
            'name': unicode(name),
            'gender': self.gender_map[self.gender.strip()]
        }

        return r

    @property
    def job_title(self):
        # don't title case roman numerals
        title = self.get_mapped_value('job_title')
        split = title.split('II')
        split_two = title.split('IV')
        split_three = title.split(' V')

        if len(split) == 1 and len(split_two) and len(split_three):
            return title.title()
        elif len(split) > 1:
            return split[0].title() + ' II' + split[1]
        elif len(split_two) > 1:
            return split_two[0].title() + ' IV'
        elif len(split_three) > 1:
            return split_three[0].title() + ' V'

    @property
    def race(self):
        return {
            'name': self.get_mapped_value('given_race').strip().title()
        }

    @property
    def description(self):
        if self.status == 'PRN':
            return "Annual compensation (on-call employee)"
        elif self.status == 'Full-Time':
            return "Annual compensation"
        else:
            return "Part-time annual compensation"


transform = base.transform_factory(TransformedRecord)

@@ -346,16 +346,67 @@
 cord):%0A%0A
+    # REJECT_ALL_IF_INVALID_RECORD_EXISTS = False%0A%0A
     MAP 
@@ -405,24 +405,24 @@
     MAP = %7B%0A
-
         'ful
@@ -1301,9 +1301,9 @@
 5, 2
-6
+7
 )%0A%0A 
@@ -1436,17 +1436,23 @@
 nown': '
-F
+Unknown
 '%7D%0A%0A    
@@ -2528,16 +2528,16 @@
 trip()%5D%0A
-
         
@@ -2535,24 +2535,480 @@
 %0A        %7D%0A%0A
+        # If we don't have a name that looks like so:%0A        # Jones,Mike%0A        # The name isn't parsed into first, last properly%0A        # So we'll parse it out manually by splitting it by the comma%0A        # And reversing order so we end up with:%0A        # Mike Jones%0A        if ',' in r%5B'name'%5D:%0A            if ', ' not in r%5B'name'%5D:%0A                name_split = r%5B'name'%5D.split(',')%0A%0A                r%5B'name'%5D = name_split%5B1%5D + ' ' + name_split%5B0%5D%0A%0A
         retu

fix name