'''
Name: avi_create_avi_fgdb_feature_class_tool.py

Overview: Creates an AVI 2.1 feature class in a File Geodatabase

Description:
    This script is associated with an ArcTool and is intended to
    create a feature class within a File Geodatabase that meets
    the AVI 2.1 standard.
    
Notes:

Author: Doug Crane
        Sept, 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_schema 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'D:\PythonScripts\avi_class\test_data\avi_fgdb.gdb', \
        r'avi', \
        r'UTM Zone 11', \
        'FALSE', \
        'TRUE']
        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
                    
        gdbPath = arcpy.GetParameterAsText(0)
        aviFCName = arcpy.GetParameterAsText(1)
        projectionType = arcpy.GetParameterAsText(2)
        useAliasCode = arcpy.GetParameterAsText(3)
        assignDomainCode = arcpy.GetParameterAsText(4)
        
        # Do not let user overwrite an existing feature class.
        fcPath = os.path.join(gdbPath, aviFCName)
        if arcpy.Exists(fcPath):
            raise SRD_Exception('Feature Class: %s already exists in %s, cannot overwrite' % (aviFCName, gdbPath))
        
        # Translate some of our arguments into parametr values.
        if projectionType.upper() == 'UTM ZONE 11':
            srType = 11
        elif projectionType.upper() == 'UTM ZONE 12':
            srType = 12
        elif projectionType.upper() == 'TTM':
            srType = 'TTM'
        else:
            raise SRD_Exception('Projection: %s not supported' % projectionType)

        if useAliasCode.upper() == 'TRUE':
            useAlias = True
        elif useAliasCode.upper() == 'FALSE':
            useAlias = False
        else:
            raise SRD_Exception('Could not determine if Field Alias is to be used, value passed to script: %s' % useAliasCode)

        if assignDomainCode.upper() == 'TRUE':
            assignDomain = True
        elif assignDomainCode.upper() == 'FALSE':
            assignDomain = False
        else:
            raise SRD_Exception('Could not determine if Assign Domain is to be used, value passed to script: %s' % assignDomainCode)

        # Build the feature class.
        aviSchemaObj = AVI_Schema()
        aviSchemaObj.createFeatureClass(gdbPath, aviFCName, srType, useAlias)

        # Assign domains to fields if needed.
        if assignDomain:
            aviSchemaObj.assignDomains(gdbPath, aviFCName)            
        
    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()
