# Path: utils.py
# def convert_to_int(possible_number, use_if_not_int):
#     """
#     Save conversion of string to int with ability to specify default if string is not a number
#     """
#     try:
#         result = int(possible_number)
#     except ValueError:
#         result = use_if_not_int
#     return result
# 
# def join_with_space_and_convert_spaces(segments, replace_with='-'):
#     """
#     Helper function joins array pieces together and then replaces any spaces
#     with specified value.
#     """
#     return " ".join(segments).replace(" ", replace_with)
# 
# def yesterday():
#     return date.today() - ONE_DAY


from django.db.utils import DatabaseError
from utils import convert_to_int, join_with_space_and_convert_spaces, yesterday
from models import HousingLocation

class HousingLocationInfo:

    def __init__(self, inmate, inmate_details, monitor):
        self._inmate = inmate
        self._inmate_details = inmate_details
        self._monitor = monitor
        self._housing_location = None
        self._location_segments = None

    def _debug(self, msg):
        self._monitor.debug('HousingLocationInfo: %s' % msg)

    def _process_housing_location(self):
        """
        Receives a housing location from the HousingLocation table and parses it editing the different fields
        """
        self._location_segments = self._housing_location.housing_location.replace("-", " ").split()
        if self._location_segments == [] or convert_to_int(self._location_segments[0], None) is None:
            # Location did not start with a number so no further parsing
            if self._housing_location.housing_location == "":
                self._housing_location.housing_location = "UNKNOWN"
            return

        self._housing_location.division = self._location_segments[0]
        if len(self._location_segments) == 1:  # Execute only if the housing information is a single division number
            return                             # ex: '01-'

        self._set_day_release()


# Path: utils.py
# def convert_to_int(possible_number, use_if_not_int):
#     """
#     Save conversion of string to int with ability to specify default if string is not a number
#     """
#     try:
#         result = int(possible_number)
#     except ValueError:
#         result = use_if_not_int
#     return result
# 
# def join_with_space_and_convert_spaces(segments, replace_with='-'):
#     """
#     Helper function joins array pieces together and then replaces any spaces
#     with specified value.
#     """
#     return " ".join(segments).replace(" ", replace_with)
# 
# def yesterday():
#     return date.today() - ONE_DAY


# Path: countyapi/housing_location_info.py
from django.db.utils import DatabaseError
from utils import convert_to_int, join_with_space_and_convert_spaces, yesterday
from models import HousingLocation


class HousingLocationInfo:

    def __init__(self, inmate, inmate_details, monitor):
        self._inmate = inmate
        self._inmate_details = inmate_details
        self._monitor = monitor
        self._housing_location = None
        self._location_segments = None

    def _debug(self, msg):
        self._monitor.debug('HousingLocationInfo: %s' % msg)

    def _process_housing_location(self):
        """
        Receives a housing location from the HousingLocation table and parses it editing the different fields
        """
        self._location_segments = self._housing_location.housing_location.replace("-", " ").split()
        if self._location_segments == [] or convert_to_int(self._location_segments[0], None) is None:
            # Location did not start with a number so no further parsing
            if self._housing_location.housing_location == "":
                self._housing_location.housing_location = "UNKNOWN"
            return

        self._housing_location.division = self._location_segments[0]
        if len(self._location_segments) == 1:  # Execute only if the housing information is a single division number
            return                             # ex: '01-'

        self._set_day_release()


        location_start = convert_to_int(self._location_segments[0], -1)