package controllers

import actors.UserParentActor.GetAllUsers
import actors._
import akka.NotUsed
import akka.actor._
import akka.event.Logging
import akka.pattern.ask
import akka.stream._
import akka.stream.scaladsl._
import akka.util.Timeout
import com.typesafe.config.ConfigFactory

import javax.inject._
import org.reactivestreams.Publisher
import play.api.libs.json._
import play.api.mvc._

import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}

/**
 * This class creates the actions and the websocket needed.
 */
@Singleton
class AdminController @Inject()(cc: ControllerComponents, homeController: HomeController) extends AbstractController(cc) {

  implicit val actorSystem: ActorSystem = ActorSystem()
  implicit val ec: ExecutionContext = defaultExecutionContext
  implicit val timeout = Timeout(100.millis)

  // Use a direct reference to SLF4J
  private val logger = org.slf4j.LoggerFactory.getLogger("controllers.HomeController")

  val userParentActor: ActorRef = homeController.userParentActor

  // Home page that renders template
  def viewAllUsers() = Action { implicit request =>
    logger.info(s"Received request from: ${request.remoteAddress}")

    val allUsers = userParentActor ? GetAllUsers
    val res = Await.result(allUsers, Duration.Inf)
    Ok(res.toString)
  }


  def reloadConfig()= Action { implicit request =>
    ConfigFactory.invalidateCaches()
    logger.info("Config reloaded")
    Ok("Config reloaded")
  }
}
