package wow.doge.chatto import javax.enterprise.context.ApplicationScoped import javax.enterprise.inject.Produces import javax.inject.Named import com.typesafe.config.ConfigFactory import com.sfxcode.sapphire.core.controller.DefaultWindowController // import org.asynchttpclient.Dsl._ import wow.doge.chatto.controller.MainViewController import sttp.client.asynchttpclient.future.AsyncHttpClientFutureBackend import sttp.client._ import scala.concurrent.ExecutionContext.Implicits.global import scala.async.Async.{async, await} import sttp.client.json4s._ import org.json4s._ import org.json4s.native.JsonMethods._ import org.json4s.JsonDSL._ import scala.util.Success import scala.util.Failure import com.softwaremill.quicklens._ @Named @ApplicationScoped class ApplicationController extends DefaultWindowController { // import ApplicationController._ lazy val mainViewController = getController[MainViewController]() private implicit lazy val serialization = org.json4s.native.Serialization private implicit lazy val backend = AsyncHttpClientFutureBackend() // override def width: Int = 400 def applicationDidLaunch() = { logger.info("start " + this) applicationEnvironment.loadResourceBundle("bundles/application") replaceSceneContent(mainViewController) } @Produces def applicationName: ApplicationName = { ApplicationName(configStringValue("application.name")) } @Produces var appData: AppData = synchronized { AppData(User.empty, "") } @Produces def httpBackend = backend def replacePrimarySceneContent(): Unit = { // Styling reloadStyles() // Resources applicationEnvironment.clearResourceBundleCache() applicationEnvironment.loadResourceBundle("bundles/application") // FXML val newMainViewController = getController[MainViewController]() replaceSceneContent(newMainViewController) } override def applicationWillStop(): Unit = async { super.applicationWillStop() println("stopping") await(httpBackend.close()) System.exit(0) } def showLoginPane() = { appData = appData.copy(user = User.empty) appData = appData.modify(_.user).using(_ => User.empty) appData = appData.modify(_.user.username).using(_ => "") replaceSceneContent(mainViewController.loginController) } def showChatPane(): Unit = { // import org.scalafx.extras._ replaceSceneContent(mainViewController.chatController, true) // httpBackend.send(basicRequest.get(uri"")) // val willBeResponse = basicRequest // .get(uri"https://httpbin.org/get") // .response(asJson[HttpBinResponse]) // .send() // async { // val r = await { willBeResponse } // r.body.map(println) // } // willBeResponse onComplete { // case Success(x) => { x.body } // case Failure(x) => {} // } // val body = for { // r <- willBeResponse // } yield (r.body) } } final case class ApplicationName(name: String) final case class User(username: String, password: String, token: String) object User { def empty = User("", "", "") } final case class AppData(user: User, sumth: String)