'''
Name: avi_remove_township_tool.py

Overview: 

Description:
    
Notes:

Author: Doug Crane
        Oct, 2009

Modifications:

'''

logChn = None

import pdb
import os
import sys

import arcpy

from srd.srd_exception import *
import srd.srd_logging as srd_logging
import srd.srd_misc as srd_misc

from avi_remove_township import *

DEBUG = 0
#--------------------------------------------------------------------------
if __name__ == '__main__':

    arcpy.env.overwriteOutput = True
    # If using geographic features then may be best to modify these.
    arcpy.env.XYTolerance = .001
    arcpy.env.XYResolution = .0001
    arcpy.gp.logHistory = False

    if DEBUG:
        # Modify arguments to simulate those passed from toolbox.
        # You will have to modify these based on your scripts requirements.
        sys.argv = [sys.argv[0], \
        r'X:\prjs\blueridgeConversionFeb2011\township_test.gdb\BRL_AVI', \
        r'X:\prjs\blueridgeConversionFeb2011\township_test.gdb\BRL_AVI_notwp',
        'FALSE',
        '5']
        print('\n***\n*** IN DEBUG MODE\n***')
        arcpy.AddMessage('\n***\n*** IN DEBUG MODE\n***')
        
    try:

        logFilePath = srd_misc.getLogFilePath(sys.argv[0])
        logChn = srd_logging.SRD_Log(logFilePath)

        #-------------------------------------------------------------
        # Set the log file.
        if logFilePath:
            try:
                # Parameters to tool will be written to log file in case of problems.
                params = arcpy.GetParameterInfo()
                hdrList = ['Arguments:']
                for param in params:
                    hdrList.append('Name: %s, Type: %s, Value: %s' % (param.name, param.parameterType, param.value))
                logChn.stampHeader(hdrList)
                arcpy.AddMessage('\n*** Log information will be written to: %s\n' % logFilePath)
                logChn.stampHeader(hdrList)
                arcpy.AddMessage('\n*** Log information will be written to: %s\n' % logFilePath)
            except:
                pass

        aviFCArg = arcpy.GetParameterAsText(0)
        notwpFCArg = arcpy.GetParameterAsText(1)

        ignoreTPRArg = arcpy.GetParameterAsText(2)
        if ignoreTPRArg.upper() == 'TRUE':
            ignoreTPRArg = True
        else:
            ignoreTPRArg = False

        excludeCCArg = arcpy.GetParameterAsText(3)
        if excludeCCArg.upper() == 'TRUE':
            excludeCCArg = True
        else:
            excludeCCArg = False

        twpSelDistArg = arcpy.GetParameterAsText(4)

        twpObj = AVI_RemoveTownship()
        twpObj.removeTownshipLine(aviFCArg, notwpFCArg, int(twpSelDistArg), ignoreTPRArg, excludeCCArg)

        # Required to add to ArcMap as layer.
        exeContext = os.path.basename(sys.executable)
        if exeContext.upper().startswith('ARCMAP'):
            logChn.logMsg('Context: %s' % exeContext)        
            arcpy.SetParameterAsText(5, notwpFCArg)
        
    except SRD_Exception as e:
        if logChn:
            logChn.logError(e)
            logChn.logError(srd_misc.exceptionDetails(sys.exc_info()[2]))
        else:
            arcpy.AddMessage(srd_misc.exceptionDetails(sys.exc_info()[2]))
    except Exception as e:
        if logChn:
            logChn.logError(e)
            logChn.logError(srd_misc.exceptionDetails(sys.exc_info()[2]))
        else:
            arcpy.AddMessage(srd_misc.exceptionDetails(sys.exc_info()[2]))
    else:
        logChn.logMsg('\n*** %s completed normally\n' % sys.argv[0])
    finally:
        arcpy.ResetEnvironments()
        if logChn:
            logChn.closeLog()
