package org.infinispan.cli.interpreter;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.infinispan.cli.interpreter.codec.CodecRegistry;
import org.infinispan.cli.interpreter.logging.Log;
import org.infinispan.cli.interpreter.result.EmptyResult;
import org.infinispan.cli.interpreter.result.Result;
import org.infinispan.cli.interpreter.result.ResultKeys;
import org.infinispan.cli.interpreter.session.Session;
import org.infinispan.cli.interpreter.session.SessionImpl;
import org.infinispan.cli.interpreter.statement.Statement;
import org.infinispan.commons.time.TimeService;
import org.infinispan.configuration.ConfigurationManager;
import org.infinispan.factories.annotations.Inject;
import org.infinispan.factories.annotations.Start;
import org.infinispan.factories.annotations.Stop;
import org.infinispan.factories.scopes.Scope;
import org.infinispan.factories.scopes.Scopes;
import org.infinispan.jmx.annotations.MBean;
import org.infinispan.jmx.annotations.ManagedAttribute;
import org.infinispan.jmx.annotations.ManagedOperation;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.util.logging.LogFactory;

@Scope(Scopes.GLOBAL)
@MBean(objectName = "Interpreter", description = "Interpreter component which executes CLI operations")
public class Interpreter {
   private static final Log log = LogFactory.getLog(Interpreter.class, Log.class);
   private static final long DEFAULT_SESSION_REAPER_WAKEUP_INTERVAL = 60000l; // in millis
   private static final long DEFAULT_SESSION_TIMEOUT = 360000l; // in millis

   @Inject EmbeddedCacheManager cacheManager;
   @Inject TimeService timeService;
   @Inject ConfigurationManager configurationManager;

   private ScheduledExecutorService executor;
   private long sessionReaperWakeupInterval = DEFAULT_SESSION_REAPER_WAKEUP_INTERVAL;
   private long sessionTimeout = DEFAULT_SESSION_TIMEOUT;
   private CodecRegistry codecRegistry;

   private final Map<String, Session> sessions = new ConcurrentHashMap<>();
   private ScheduledFuture<?> sessionReaperTask;

   public Interpreter() {
   }

   @Start
   public void start() {
      this.codecRegistry = new CodecRegistry(cacheManager);
      this.executor = Executors.newSingleThreadScheduledExecutor(r -> new Thread(r, "Interpreter"));
      sessionReaperTask = executor.scheduleWithFixedDelay(new ScheduledTask(), sessionReaperWakeupInterval, sessionReaperWakeupInterval, TimeUnit.MILLISECONDS);
   }

   @Stop
   public void stop() {
      if (sessionReaperTask != null) {
         sessionReaperTask.cancel(true);
      }
      executor.shutdownNow();
   }

   @ManagedOperation(description = "Creates a new interpreter session")
   public String createSessionId(String cacheName) {
      String sessionId = UUID.randomUUID().toString();
      SessionImpl session = new SessionImpl(codecRegistry, cacheManager, sessionId, timeService, configurationManager);
      sessions.put(sessionId, session);
      if (cacheName != null) {
         session.setCurrentCache(cacheName);
      }
      return sessionId;
   }

   public long getSessionReaperWakeupInterval() {
      return sessionReaperWakeupInterval;
   }

   public void setSessionReaperWakeupInterval(final long sessionReaperWakeupInterval) {
      this.sessionReaperWakeupInterval = sessionReaperWakeupInterval;
   }

   public long getSessionTimeout() {
      return sessionTimeout;
   }

   public void setSessionTimeout(final long sessionTimeout) {
      this.sessionTimeout = sessionTimeout;
   }

   void expireSessions() {
      long timeBoundary = timeService.time() - sessionTimeout * 1000000l;
      for (Iterator<Session> i = sessions.values().iterator(); i.hasNext(); ) {
         Session session = i.next();
         if (timeBoundary - session.getTimestamp() > 0) {
            i.remove();
            if (log.isDebugEnabled()) {
               log.debugf("Removed expired interpreter session %s", session.getId());
            }
         }
      }
   }

   @ManagedOperation(description = "Parses and executes IspnCliQL statements")
   public Map<String, String> execute(final String sessionId, final String s) throws Exception {
      Session session = null;
      ClassLoader classLoader = configurationManager.getGlobalConfiguration().classLoader();
      ClassLoader oldCL = SecurityActions.setThreadContextClassLoader(classLoader);
      Map<String, String> response = new HashMap<>();
      try {
         session = validateSession(sessionId);

         CharStream stream = new ANTLRStringStream(s);
         IspnCliQLLexer lexer = new IspnCliQLLexer(stream);
         CommonTokenStream tokens = new CommonTokenStream(lexer);
         IspnCliQLParser parser = new IspnCliQLParser(tokens);

         parser.statements();

         if (parser.hasParserErrors()) {
            throw new ParseException(parser.getParserErrors());
         }

         StringBuilder output = new StringBuilder();
         for (Statement stmt : parser.statements) {
            Result result = stmt.execute(session);
            if (result != EmptyResult.RESULT) {
               output.append(result.getResult());
            }
         }
         if (output.length() > 0) {
            response.put(ResultKeys.OUTPUT.toString(), output.toString());
         }
      } catch (Throwable t) {
         log.interpreterError(t);
         response.put(ResultKeys.ERROR.toString(), t.getMessage());
         StringWriter sw = new StringWriter();
         PrintWriter pw = new PrintWriter(sw);
         t.printStackTrace(pw);
         response.put(ResultKeys.STACKTRACE.toString(), sw.toString());
      } finally {
         if (session != null) {
            session.reset();
            response.put(ResultKeys.CACHE.toString(), session.getCurrentCacheName());
         }
         SecurityActions.setThreadContextClassLoader(oldCL);

      }
      return response;
   }

   private Session validateSession(final String sessionId) {
      if (sessionId == null) {
         Session session = new SessionImpl(codecRegistry, cacheManager, null, timeService, configurationManager);
         cacheManager.getCacheManagerConfiguration().defaultCacheName().ifPresent(session::setCurrentCache);
         return session;
      }
      if (!sessions.containsKey(sessionId)) {
         throw log.invalidSession(sessionId);
      }
      return sessions.get(sessionId);
   }

   @ManagedAttribute(description = "Retrieves a list of caches for the cache manager")
   public String[] getCacheNames() {
      Set<String> cacheNames = new HashSet<>(cacheManager.getCacheNames());
      return cacheNames.toArray(new String[0]);
   }

   class ScheduledTask implements Runnable {
      @Override
      public void run() {
         expireSessions();
      }
   }

}
