<commit_before>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * 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 iht.controllers.application.pdf

import iht.config.FrontendAuthConnector
import iht.connector.{CachingConnector, IhtConnector}
import iht.constants.Constants
import iht.controllers.application.ApplicationController
import iht.controllers.auth.IhtActions
import iht.utils.pdf._
import iht.utils.{CommonHelper, DeclarationHelper}
import models.des.iht_return.IHTReturn
import play.api.Logger
import play.api.i18n.Messages
import uk.gov.hmrc.play.frontend.auth.connectors.AuthConnector
import uk.gov.hmrc.play.http.HeaderCarrier

import scala.concurrent.Future

/**
  * Created by dbeer on 14/08/15.
  */
object PDFController extends PDFController {
  lazy val cachingConnector = CachingConnector
  lazy val authConnector: AuthConnector = FrontendAuthConnector
  lazy val ihtConnector = IhtConnector
  lazy val xmlFoToPDF = XmlFoToPDF
}

trait PDFController extends ApplicationController with IhtActions {

  def cachingConnector: CachingConnector

  def ihtConnector: IhtConnector

  def xmlFoToPDF: XmlFoToPDF

  private val staticHeaders: Seq[(String, String)] = Seq(Tuple2("Content-type", "application/pdf"))

  private def pdfHeaders(fileName: String): Seq[(String, String)] =
    staticHeaders :+ Tuple2(CONTENT_DISPOSITION, s"inline; filename=$fileName")

  def onPreSubmissionPDF = authorisedForIht {
    implicit user =>
      implicit request => {
        Logger.info("Generating Summary PDF")
        val fileName = s"${Messages("iht.inheritanceTaxEstateReport")}.pdf"
        withApplicationDetails { regDetails =>
          applicationDetails =>
            val pdfByteArray = xmlFoToPDF.createPreSubmissionPDF(
              regDetails,
              applicationDetails,
              DeclarationHelper.getDeclarationType(applicationDetails)
            )
            Future.successful(Ok(pdfByteArray).withHeaders(pdfHeaders(fileName): _*))
        }
      }
  }

  def onClearancePDF = authorisedForIht {
    implicit user =>
      implicit request => {
        Logger.info("Generating Clearance PDF")

        cachingConnector.getSingleValue(Constants.PDFIHTReference).flatMap { optionIHTReference =>
          val ihtReference = CommonHelper.getOrException(optionIHTReference)
          val fileName = s"${Messages("pdf.clearanceCertificate.title")}.pdf"
          val nino = CommonHelper.getNino(user)
          ihtConnector.getCaseDetails(nino, ihtReference).flatMap(registrationDetails =>
            getSubmittedApplicationDetails(nino,<commit_msg>IHT-4391 Quotes around string as problem with mac<commit_after>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * 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 iht.controllers.application.pdf

import iht.config.FrontendAuthConnector
import iht.connector.{CachingConnector, IhtConnector}
import iht.constants.Constants
import iht.controllers.application.ApplicationController
import iht.controllers.auth.IhtActions
import iht.utils.pdf._
import iht.utils.{CommonHelper, DeclarationHelper}
import models.des.iht_return.IHTReturn
import play.api.Logger
import play.api.i18n.Messages
import uk.gov.hmrc.play.frontend.auth.connectors.AuthConnector
import uk.gov.hmrc.play.http.HeaderCarrier

import scala.concurrent.Future

/**
  * Created by dbeer on 14/08/15.
  */
object PDFController extends PDFController {
  lazy val cachingConnector = CachingConnector
  lazy val authConnector: AuthConnector = FrontendAuthConnector
  lazy val ihtConnector = IhtConnector
  lazy val xmlFoToPDF = XmlFoToPDF
}

trait PDFController extends ApplicationController with IhtActions {

  def cachingConnector: CachingConnector

  def ihtConnector: IhtConnector

  def xmlFoToPDF: XmlFoToPDF

  private val staticHeaders: Seq[(String, String)] = Seq(Tuple2("Content-type", "application/pdf"))

  private def pdfHeaders(fileName: String): Seq[(String, String)] =
    staticHeaders :+ Tuple2(CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\"")

  def onPreSubmissionPDF = authorisedForIht {
    implicit user =>
      implicit request => {
        Logger.info("Generating Summary PDF")
        val fileName = s"${Messages("iht.inheritanceTaxEstateReport")}.pdf"
        withApplicationDetails { regDetails =>
          applicationDetails =>
            val pdfByteArray = xmlFoToPDF.createPreSubmissionPDF(
              regDetails,
              applicationDetails,
              DeclarationHelper.getDeclarationType(applicationDetails)
            )
            Future.successful(Ok(pdfByteArray).withHeaders(pdfHeaders(fileName): _*))
        }
      }
  }

  def onClearancePDF = authorisedForIht {
    implicit user =>
      implicit request => {
        Logger.info("Generating Clearance PDF")

        cachingConnector.getSingleValue(Constants.PDFIHTReference).flatMap { optionIHTReference =>
          val ihtReference = CommonHelper.getOrException(optionIHTReference)
          val fileName = s"${Messages("pdf.clearanceCertificate.title")}.pdf"
          val nino = CommonHelper.getNino(user)
          ihtConnector.getCaseDetails(nino, ihtReference).flatMap(registrationDetails =>
            getSubmittedApplicationDetails(nino,
{'fraction_non_alphanumeric': 0.08306606799230276, 'fraction_numerical': 0.006414368184733804, 'mean_word_length': 3.891764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5103649', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1724, 'n_words': 445}