-- doCommandMovieScript
-----------------------------------
-- DESCRIPTION:
--              process the command, which was chosen
--              at this point the command string is stored in the globallist,
--              as we come here in an asynch process -> MUI dialog
--              the command and the paramaters are stored in the global list which we get with:
--              call(#mGetGlobalList, mGetXScript())
--                  the command string is glob.getaprop(#cmd)
--                  the parameters are in glob.getaprop(#theParams)
--
-- REQUIRES:
--              script "aleXtrasMovieScript" as provider for library scripts -> xscr()
--              script "commonMovieScript" -- standard handlers -> here to get the globallist -> mGetGlobalList()
-- USAGE:
-- 
-- NOTES:
--              This handler used to be in "thisMovieScript", but due to a scope bug with calling handlers
--              in other movies -> tell the stage unfortunately this doesn't fix the problem.
--              I have to investigate later WHY... so calling handlers, which are in this movie will fail
-----------------------------------

-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
on _____________________PROCESS_COMMAND
end
-- xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

on mDoCmd dto, dto2
  
  reservedHandlerList = []
  repeat with mscriptName in ["doCommandMovieScript", "thisMoviesScript", "convert_Lingo_2_CSS_Html"]
    li = (script mscriptName).handlers()
    if listP(li) then
      repeat with n = count(li) down to 1
        reservedHandlerList.add(string(li[n]))
      end repeat
    end if
  end repeat
  
  if ilk(dto) = #timeout then dto.forget()
  if ilk(dto2) = #timeout then dto2.forget()
  
  global gRetVal
  glob = call(#mGetGlobalList, mGetXScript())
  
  skipFirstSpace = 0
  
  inst = glob.getaprop(#theTargetInstance)
  if ilk(inst) = #instance then
    theMemberName = glob.getaprop(#theMemberName)
    if glob.getaprop(#copy2ClipBoard) = 2 then
      
      ----------- alex syntax (I have my own "global handler" to create scriptinstances differently):
      --      dostr = "call(#" & symbol(glob.getaprop(#cmd)) & ", mGetXscript(#" & theMemberName & "),"
      if string(theMemberName) = "commonMovieScript" then
        dostr = "xscr()." & glob.getaprop(#cmd) & "("
      else
        dostr = "xscr(#" & theMemberName & ")." & glob.getaprop(#cmd) & "("
      end if
      skipFirstSpace = 1
      del = 0
    else if glob.getaprop(#copy2ClipBoard) = 1 then
      ----------- standard syntax, create new script instance for each call (this is NOT the optimized way !)
      dostr = "call(#" & symbol(glob.getaprop(#cmd)) & ", new(script " & QUOTE & theMemberName & QUOTE & "),"
      
      ----------- better: store the intsance in a variable and reuse it (you will need to delete the new(script foo) lines)
      --      dostr = "theScriptInstance = new(script " & QUOTE & theMemberName & QUOTE & ")" & RETURN & "call(#" & symbol(glob.getaprop(#cmd)) & ", theScriptInstance,"
      del = 1
    else
      dostr = "call(#" & symbol(glob.getaprop(#cmd)) & ", [inst],"
      del = 1
    end if
    
  else
    
    if glob.getaprop(#isBehavior) = 1 then
      theParams = glob.getaprop(#theParams)
      sprnum = theParams.getaprop("spritenum")
      if length(string(sprnum)) < 1 then
        sprnum = 0
      else if not(integerP(integer(sprnum))) then
        sprnum = QUOTE & sprnum & QUOTE
      end if
      theParams.deleteprop("spritenum")
      
      dostr = "sendSprite(" & sprnum & ", #" & glob.getaprop(#cmd) & ","
      del = 1
    else
      dostr = glob.getaprop(#cmd) & "("
      skipFirstSpace = 1
      del = 0
    end if
  end if
  
  theParams = glob.getaprop(#theParams)
  if theParams.count then
    repeat with thisParam in theParams
      
      ------- @@ is reserved to force treatment as string in any case
      ------- sometimes (esp. svn commit messages) we do not want any evaluation of the parameter
      if char 1 to 2 of string(thisParam) = "@@" then
        thisParam = string(thisParam)
        delete char 1 to 2 of thisParam
        theVal = void
      else
        theVal = value(thisParam)
      end if
      
      
      if voidP(theVal) then
        if (string(thisParam)).word.count > 0 then
          if skipFirstSpace = 1 then
            put QUOTE&thisParam&QUOTE&"," after dostr
            skipFirstSpace = 0
          else
            put " "&QUOTE&thisParam&QUOTE&"," after dostr
          end if
        else
          put " void," after dostr
        end if
        
      else
        if length(string(thisParam)) > 0 then
          if skipFirstSpace = 1 then
            put thisParam&"," after dostr
            skipFirstSpace = 0
          else
            put " "&thisParam&"," after dostr
          end if
        else
          put " void," after dostr
        end if
        
      end if
      
      del = 1
      
    end repeat
    
  end if
  if del then delete the last char of dostr
  
  put ")" after dostr
  
  if (glob.getaprop(#copy2ClipBoard) = 1) or (glob.getaprop(#copy2ClipBoard) = 2) then
    put "theResult = " before dostr
    
    member("pastefeld").text = dostr
    copyToClipBoard(member("pastefeld"))
    
  else
    
    if reservedHandlerList.getPos(string(symbol(glob.getaprop(#cmd)))) > 0 then
      alert "Sorry due to a bug in director I can't call handlers, which appear in moviescripts of this tool miaw themselves: " & glob.getaprop(#cmd)
      exit
    end if
    
    tell the stage
      
      res = the result
      do dostr
      if the result <> res then
        gRetVal = the result
        if (string(gRetVal)).length < 100 then put gRetVal
        else put "Too many chars to put in the message window. Get the result from the global variable gRetVal"&RETURN&"put gRetVal"&RETURN
      end if
    end tell
  end if
  
end
