package net.wizardsoflua.lua.classes.eventinterceptor;

import com.google.auto.service.AutoService;

import net.sandius.rembulan.runtime.ExecutionContext;
import net.wizardsoflua.lua.classes.DeclareLuaClass;
import net.wizardsoflua.lua.classes.DelegatorLuaClass;
import net.wizardsoflua.lua.classes.common.LuaInstance;
import net.wizardsoflua.lua.classes.spi.DeclaredLuaClass;
import net.wizardsoflua.lua.function.NamedFunction1;

@AutoService(DeclaredLuaClass.class)
@DeclareLuaClass (name = EventInterceptorClass.NAME)
public class EventInterceptorClass
    extends DelegatorLuaClass<EventInterceptor, EventInterceptorClass.Proxy<EventInterceptor>> {
  public static final String NAME = "EventInterceptor";

  public EventInterceptorClass() {
    add(new StopFunction());
  }

  @Override
  public Proxy<EventInterceptor> toLua(EventInterceptor javaObj) {
    return new Proxy<>(this, javaObj);
  }

  public static class Proxy<D extends EventInterceptor> extends LuaInstance<D> {
    public Proxy(DelegatorLuaClass<?, ?> luaClass, D delegate) {
      super(luaClass, delegate);
    }

    @Override
    public boolean isTransferable() {
      return false;
    }
  }

  private class StopFunction extends NamedFunction1 {
    @Override
    public String getName() {
      return "stop";
    }

    @Override
    public void invoke(ExecutionContext context, Object arg1) {
      EventInterceptor self =
          getConverters().toJava(EventInterceptor.class, arg1, 1, "self", getName());
      self.stop();
      context.getReturnBuffer().setTo();
    }
  }
}

@@ -93,17 +93,16 @@
 ervice;%0A
-%0A
 import n
@@ -125,140 +125,412 @@
 lan.
-runtime.ExecutionContext;%0Aimport net.wizardsoflua.lua.classes.DeclareLuaClass;%0Aimport net.wizardsoflua.lua.classes.DelegatorLuaClass
+Table;%0Aimport net.wizardsoflua.annotation.GenerateLuaClassTable;%0Aimport net.wizardsoflua.annotation.GenerateLuaDoc;%0Aimport net.wizardsoflua.annotation.GenerateLuaInstanceTable;%0Aimport net.wizardsoflua.annotation.LuaFunction;%0Aimport net.wizardsoflua.extension.api.inject.Resource;%0Aimport net.wizardsoflua.extension.spell.api.resource.LuaConverters;%0Aimport net.wizardsoflua.extension.spell.spi.LuaConverter
 ;%0Aim
@@ -567,15 +567,8 @@
 ses.
-common.
 LuaI
@@ -616,173 +616,323 @@
 ses.
-spi.DeclaredLuaClass;%0Aimport net.wizardsoflua.lua.function.NamedFunction1;%0A%0A@AutoService(DeclaredLuaClass.class)%0A@DeclareLuaClass (name = EventInterceptorClass.NAME)
+common.Delegator;%0Aimport net.wizardsoflua.lua.extension.util.BasicLuaClass;%0Aimport net.wizardsoflua.lua.extension.util.LuaClassAttributes;%0A%0A@AutoService(LuaConverter.class)%0A@LuaClassAttributes(name = EventInterceptorClass.NAME)%0A@GenerateLuaClassTable(instance = EventInterceptorClass.Instance.class)%0A@GenerateLuaDoc
 %0Apub
@@ -975,25 +975,21 @@
 extends 
-Delegator
+Basic
 LuaClass
@@ -1029,38 +1029,26 @@
 orClass.
-Proxy%3CEventInterceptor
+Instance%3C?
 %3E%3E %7B%0A  p
@@ -1104,128 +1104,234 @@
 r%22;%0A
-%0A  public EventInterceptorClass() %7B%0A    add(new StopFunction());%0A  %7D%0A%0A  @Override%0A  public Proxy%3CEventInterceptor%3E toLua
+  @Resource%0A  private LuaConverters converters;%0A%0A  @Override%0A  protected Table createRawTable() %7B%0A    return new EventInterceptorClassTable%3C%3E(this, converters);%0A  %7D%0A%0A  @Override%0A  protected Delegator%3CInstance%3C?%3E%3E toLuaInstance
 (Eve
@@ -1348,19 +1348,24 @@
 tor java
-Obj
+Instance
 ) %7B%0A    
@@ -1379,36 +1379,140 @@
 new 
-Proxy%3C%3E(this, javaObj);%0A  %7D%0A
+EventInterceptorClassInstanceTable%3C%3E(new Instance%3C%3E(javaInstance), getTable(),%0A        converters);%0A  %7D%0A%0A  @GenerateLuaInstanceTable
 %0A  p
@@ -1530,21 +1530,24 @@
 c class 
-Proxy
+Instance
 %3CD exten
@@ -1607,48 +1607,17 @@
 lic 
-Proxy(DelegatorLuaClass%3C?, ?%3E luaClass, 
+Instance(
 D de
@@ -1642,18 +1642,8 @@
 per(
-luaClass, 
 dele
@@ -1665,462 +1665,64 @@
    @
-Override%0A    public boolean isTransferable() %7B%0A      return false;%0A    %7D%0A  %7D%0A%0A  private class StopFunction extends NamedFunction1 %7B%0A    @Override%0A    public String getName() %7B%0A      return %22stop%22;%0A    %7D%0A%0A    @Override%0A    public void invoke(ExecutionContext context, Object arg1) %7B%0A      EventInterceptor self =%0A          getConverters().toJava(EventInterceptor.class, arg1, 1, %22self%22, getName());%0A      self.stop();%0A      context.getReturnBuffer().setTo
+LuaFunction%0A    public void stop() %7B%0A      delegate.stop
 ();%0A

Update EventInterceptorClass to architecture 3