#!/usr/bin/python -B

from string import Template, upper, replace

from ApiUtil import outputCode
from ApiUtil import typeIsVoid

from ApiCodeGen import *

from RegalDispatchLog import apiDispatchFuncInitCode
from RegalDispatchEmu import dispatchSourceTemplate
from RegalContextInfo import cond

##############################################################################################

# CodeGen for NaCL dispatch functions

def apiNaclFuncDefineCode(apis, args):

  code = ''

  for api in apis:

    if api.name=='gl':

      for function in api.functions:
        if not function.needsContext:
          continue
        if getattr(function,'esVersions',None)==None or 2.0 not in function.esVersions:
          continue
        if getattr(function,'regalOnly',False)==True:
          continue

        name   = function.name
        params = paramsDefaultCode(function.parameters, True)
        callParams = paramsNameCode(function.parameters)
        rType  = typeCode(function.ret.type)
        naclName = name
        if naclName.startswith('gl'):
          naclName = naclName[2:]

        code += 'static %sREGAL_CALL %s%s(%s) \n{\n' % (rType, 'nacl_', name, params)
        code += '  Internal("nacl_%s","()");\n' % name
        code += '  RegalContext * rCtx = GET_REGAL_CONTEXT();\n'
        code += '  RegalAssert(rCtx)\n'
        code += '  RegalAssert(rCtx->naclES2)\n'
        code += '  RegalAssert(rCtx->naclES2->%s)\n'%(naclName)
        code += '  RegalAssert(rCtx->naclResource)\n'
        if not typeIsVoid(rType):
          code += '  %s ret = ' % rType
        else:
          code += '  '
        if len(callParams):
          callParams = 'rCtx->naclResource, %s'%callParams
        else:
          callParams = 'rCtx->naclResource'
        code += 'rCtx->naclES2->%s(%s);\n' % ( naclName, callParams )
        if not typeIsVoid(rType):
          code += '  return ret;\n'
        code += '}\n\n'

  return code

def apiNaclFuncInitCode(apis, args):

  code = '// OpenGL ES 2.0 only\n'

  for api in apis:

    if api.name=='gl':

      for function in api.functions:
        if not function.needsContext:
          continue
        if getattr(function,'esVersions',None)==None or 2.0 not in function.esVersions:
          continue

        name   = function.name
        params = paramsDefaultCode(function.parameters, True)
        callParams = paramsNameCode(function.parameters)
        rType  = typeCode(function.ret.type)

        code += '  tbl.%s = %s_%s;\n' % ( name, 'nacl', name )

  return code

def generateNaclSource(apis, args):

  funcDefine = apiNaclFuncDefineCode( apis, args )
  funcInit   = apiNaclFuncInitCode  ( apis, args )

  # Output

  substitute = {}

  substitute['LICENSE']         = args.license
  substitute['AUTOGENERATED']   = args.generated
  substitute['COPYRIGHT']       = args.copyright
  substitute['DISPATCH_NAME'] = 'Nacl'
  substitute['LOCAL_INCLUDE'] = '#include <ppapi/c/ppb_opengles2.h>'
  substitute['LOCAL_CODE']    = ''
  substitute['API_DISPATCH_FUNC_DEFINE'] = funcDefine
  substitute['API_DISPATCH_FUNC_INIT'] = funcInit
  substitute['IFDEF'] = '#if REGAL_DRIVER && defined(__native_client__)\n'
  substitute['ENDIF'] = '#endif\n'

  outputCode( '%s/RegalDispatchNacl.cpp' % args.outdir, dispatchSourceTemplate.substitute(substitute))

