/*
 * Copyright 2018 Comcast Cable Communications Management, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controllers

import actions.SecuritySupport
import javax.inject.{Inject, Singleton}
import models.{CustomLinks, DnsChangeNotices, Meta}
import org.slf4j.LoggerFactory
import play.api.Configuration
import play.api.mvc._

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

/*
 * Controller for specific pages - sends requests along to views
 */
@Singleton
class FrontendController @Inject() (
    components: ControllerComponents,
    configuration: Configuration,
    securitySupport: SecuritySupport
) extends AbstractController(components) {

  implicit lazy val customLinks: CustomLinks = CustomLinks(configuration)
  implicit lazy val meta: Meta = Meta(configuration)
  private val logger = LoggerFactory.getLogger(classOf[FrontendController])
  private val userAction = securitySupport.frontendAction

  def loginPage(): Action[AnyContent] = securitySupport.loginPage()

  def noAccess(): Action[AnyContent] = securitySupport.noAccess()

  def logout(): Action[AnyContent] = securitySupport.logout()

  def index(): Action[AnyContent] = userAction.async { implicit request =>
    val canReview = request.user.isSuper || request.user.isSupport
    Future(
      Ok(
        views.html.dnsChanges
          .dnsChanges(request.user.userName, canReview)
      )
    )
  }

  def viewAllGroups(): Action[AnyContent] = userAction.async { implicit request =>
    Future(Ok(views.html.groups.groups(request.user.userName)))
  }

  def viewGroup(groupId: String): Action[AnyContent] = userAction.async { implicit request =>
    logger.info(s"View group for $groupId")
    Future(Ok(views.html.groups.groupDetail(request.user.userName)))
  }

  def viewAllZones(): Action[AnyContent] = userAction.async { implicit request =>
    Future(Ok(views.html.zones.zones(request.user.userName)))
  }

  def viewZone(zoneId: String): Action[AnyContent] = userAction.async { implicit request =>
    val canReview = request.user.isSuper || request.user.isSupport
    Future(Ok(views.html.zones.zoneDetail(request.user.userName, canReview, zoneId)))
  }

  def viewRecordSets(): Action[AnyContent] = userAction.async { implicit request =>
    val canReview = request.user.isSuper || request.user.isSupport
    Future(Ok(views.html.recordsets.recordSets(request.user.userName, canReview)))
  }

  def viewAllBatchChanges(): Action[AnyContent] = userAction.async { implicit request =>
    val canReview = request.user.isSuper || request.user.isSupport
    Future(
      Ok(
        views.html.dnsChanges
          .dnsChanges(request.user.userName, canReview)
      )
    )
  }

  def viewBatchChange(batchId: String): Action[AnyContent] = userAction.async { implicit request =>
    logger.info(s"View Batch Change for $batchId")
    val canReview = request.user.isSuper || request.user.isSupport
    val dnsChangeNotices = configuration.get[DnsChangeNotices]("dns-change-notices")
    Future(
      Ok(
        views.html.dnsChanges
          .dnsChangeDetail(request.user.userName, canReview, dnsChangeNotices)
      )
    )
  }

  def viewNewBatchChange(): Action[AnyContent] = userAction.async { implicit request =>
    Future(Ok(views.html.dnsChanges.dnsChangeNew(request.user.userName)))
  }
}
