import arcpy
class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""

    self.params[8].category = "Biodiversity Weights"
    self.params[9].category = "Biodiversity Weights"
    self.params[10].category = "Biodiversity Weights"
    self.params[11].category = "Biodiversity Weights"

    self.params[12].category = "Environmental Services Weights"
    self.params[13].category = "Environmental Services Weights"
    self.params[14].category = "Environmental Services Weights"
    self.params[15].category = "Environmental Services Weights"

    self.params[16].category = "Landscape Parameter Weights"
    self.params[17].category = "Landscape Parameter Weights"
    self.params[18].category = "Landscape Parameter Weights"
    self.params[19].category = "Landscape Parameter Weights"
    self.params[20].category = "Landscape Parameter Weights"
    self.params[21].category = "Landscape Parameter Weights"
    self.params[22].category = "Landscape Parameter Weights"
    self.params[23].category = "Landscape Parameter Weights"

    self.params[24].category = "Legal and Conservation Land Designation Weights"
    self.params[25].category = "Legal and Conservation Land Designation Weights"
    self.params[26].category = "Legal and Conservation Land Designation Weights"
    self.params[27].category = "Legal and Conservation Land Designation Weights"
    self.params[28].category = "Legal and Conservation Land Designation Weights"

    self.params[29].category = "Threat Weights"
    self.params[30].category = "Threat Weights"

    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""


    

    if self.params[0].value and not self.params[0].hasBeenValidated:
     
     ws=self.params[0].value
     dws = arcpy.Describe(ws)
     theInputWSgdb = dws.catalogPath+"\\lg_output.gdb"
     theInputWS = dws.catalogPath+"\\lg_output"
     
     if arcpy.Exists(theInputWSgdb):
      imgExt = ""
      setVal = "yes"
      thePath = theInputWSgdb
     elif arcpy.Exists(theInputWS):
      imgExt = ".img"
      setVal = "yes"
      thePath = theInputWS
     else:
      setVal = "no"

     if setVal == "yes":
      
      if arcpy.Exists(thePath+"\\lgBioAll_ConvRank"+imgExt):
       
       self.params[3].enabled = True
       self.params[8].enabled = True
       self.params[9].enabled = arcpy.Exists(thePath+"\\lgBio_VegRank"+imgExt)
       self.params[10].enabled = True
       self.params[11].enabled = arcpy.Exists(thePath+"\\lgBio_OtherRank"+imgExt)
      else:
       
       self.params[3].enabled = False
       self.params[8].enabled = False
       self.params[9].enabled = False
       self.params[10].enabled = False
       self.params[11].enabled = False


      if arcpy.Exists(thePath+"\\lgESAll_ConvRank"+imgExt):
       self.params[4].enabled = True
       self.params[12].enabled = arcpy.Exists(thePath+"\\lgES_ARA_Rank"+imgExt)
       self.params[13].enabled = arcpy.Exists(thePath+"\\lgES_FA_Rank"+imgExt)
       self.params[14].enabled = arcpy.Exists(thePath+"\\lgES_SD_Rank"+imgExt)
       self.params[15].enabled = arcpy.Exists(thePath+"\\lgES_Other_Rank"+imgExt)
      else:
       self.params[4].enabled = False
       self.params[12].enabled = False
       self.params[13].enabled = False
       self.params[14].enabled = False
       self.params[15].enabled = False

      self.params[23].enabled = arcpy.Exists(thePath+"\\lgLand_OtherRank"+imgExt)

      if arcpy.Exists(thePath+"\\lgLegalAll_ConvRank"+imgExt):
       self.params[6].enabled = True
       self.params[24].enabled = arcpy.Exists(thePath+"\\lgLegal_LRsRank"+imgExt)
       self.params[25].enabled = arcpy.Exists(thePath+"\\lgLegal_PPAsRank"+imgExt)
       self.params[26].enabled = arcpy.Exists(thePath+"\\lgLegal_PAsRank"+imgExt)
       self.params[27].enabled = arcpy.Exists(thePath+"\\lgLegal_CPAsRank"+imgExt)
       self.params[28].enabled = arcpy.Exists(thePath+"\\lgLegal_OtherRank"+imgExt)
      else:
       self.params[6].enabled = False
       self.params[24].enabled = False
       self.params[25].enabled = False
       self.params[26].enabled = False
       self.params[27].enabled = False
       self.params[28].enabled = False

      if arcpy.Exists(thePath+"\\lgThreatAll_ConvRank"+imgExt):
       self.params[7].enabled = True
       self.params[29].enabled = arcpy.Exists(thePath+"\\lgThreat_AccessRank"+imgExt)
       self.params[30].enabled = arcpy.Exists(thePath+"\\lgThreat_OtherRank"+imgExt)
      else:
       self.params[7].enabled = False
       self.params[29].enabled = False
       self.params[30].enabled = False


    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""


    if self.params[2].altered:
      outFCname = str(self.params[2].value)
      self.params[2].clearMessage
      if self.params[0].altered:
        ws=self.params[0].value
        dws = arcpy.Describe(ws)
        theInputWSgdb = dws.catalogPath+"\\lg_output.gdb"
        theInputWS = dws.catalogPath+"\\lg_output"
        if arcpy.Exists(theInputWS):
         fname = theInputWS+"\\"+outFCname+".img"
        else:
         fname = theInputWSgdb+"\\"+outFCname
        if arcpy.Exists(fname):
          self.params[2].setErrorMessage("Filename already exists. Please change name.")
      import string
      allVals = string.ascii_letters+string.digits+"_"
      for chr in outFCname:
        if chr not in allVals:
         self.params[2].setErrorMessage("Output name must not have spaces, periods or special characters.  Please modify the name.")

    if self.params[1].altered:
      outFCname = str(self.params[1].value)
      self.params[1].clearMessage
      if self.params[0].altered:
        ws=self.params[0].value
        dws = arcpy.Describe(ws)
        theInputWSgdb = dws.catalogPath+"\\lg_output.gdb"
        theInputWS = dws.catalogPath+"\\lg_output"
        if arcpy.Exists(theInputWS):
         fname = theInputWS+"\\"+outFCname+".img"
        else:
         fname = theInputWSgdb+"\\"+outFCname
        if arcpy.Exists(fname):
          self.params[1].setErrorMessage("Filename already exists. Please change name.")
      import string
      allVals = string.ascii_letters+string.digits+"_"
      for chr in outFCname:
        if chr not in allVals:
         self.params[1].setErrorMessage("Output name must not have spaces, periods or special characters.  Please modify the name.")

    if self.params[0].altered:
      ws=self.params[0].value
      dws = arcpy.Describe(ws)
      theInputWSgdb = dws.catalogPath+"\\lg_output.gdb"
      theInputWS = dws.catalogPath+"\\lg_output"
      if not((arcpy.Exists(theInputWSgdb)) or (arcpy.Exists(theInputWS))):
	   self.params[0].setErrorMessage("Folder selected was not created by LegalGeo.  Please select a different folder.")


    return