/*
 * Copyright (C) 2020  Prasoon Joshi
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import _root_.controllers.AssetsComponents
import com.softwaremill.macwire._
import play.api.ApplicationLoader.Context
import play.api._
import play.api.i18n._
import play.api.mvc._
import play.api.routing.Router
import router.Routes
import play.filters.csrf.CSRFFilter

import configuration.NiosxModule
import repository._

class NiosxApplicationLoader extends ApplicationLoader {
  def load(context: Context): Application = new NiosxComponents(context).application
}

class NiosxComponents(context: Context) extends BuiltInComponentsFromContext(context)
with NiosxModule
with I18nComponents 
with AssetsComponents
with play.filters.cors.CORSComponents
with play.filters.HttpFiltersComponents {

  LoggerConfigurator(context.environment.classLoader).foreach {
    _.configure(context.environment, context.initialConfiguration, Map.empty)
  }

  /**
   * If you are using compile-time dependency injection, 
   * the runtime configuration for filters is ignored. 
   * Instead, you need to put code into your ApplicationLoader:
   */
  override def httpFilters: Seq[EssentialFilter] = {
    super.httpFilters.filterNot(_.getClass == classOf[CSRFFilter]) :+ corsFilter
  }

  lazy val router: Router = {
    //adding prefix string in local scope for the Router constructor
    val prefix: String = "/"
    wire[Routes]
  }

  lazy val ec: NiosxExecutionContext = wire[NiosxExecutionCtxImpl]
}
