package com.avitam.bankloanapplication.web.controllers;

import com.avitam.bankloanapplication.pdfGenerator.PdfGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;

@RestController
@RequestMapping("/loans/pdfDownload")
public class PdfDownloadController {

    @Autowired
    private PdfGenerator pdfGenerator;

    @GetMapping("/getSanctionLetter")
    public ResponseEntity<byte[]> getSanctionLetter(@RequestParam String loanApplicationId) {
        ByteArrayOutputStream byteArrayOutputStream = pdfGenerator.sanctionLetterPdf(loanApplicationId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "Sanction Letter " + loanApplicationId + ".pdf");
        headers.setContentLength(byteArrayOutputStream.size());

        return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
    }

    @GetMapping("/getAnnexureB")
    public ResponseEntity<byte[]> getAnnexureB(@RequestParam String loanId) {
        ByteArrayOutputStream byteArrayOutputStream = pdfGenerator.annexureBPdf(loanId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "Annexure B " + loanId + ".pdf");
        headers.setContentLength(byteArrayOutputStream.size());

        return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
    }

    @GetMapping("/getLoanAggrementPdf")
    public ResponseEntity<byte[]> getLoanAggrementPdf(@RequestParam String loanId) {
        ByteArrayOutputStream byteArrayOutputStream = pdfGenerator.loanAggrementPdf(loanId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "Loan Agreement " + loanId + ".pdf");
        headers.setContentLength(byteArrayOutputStream.size());
        return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
    }

    @GetMapping("/getNocPdf")
    public ResponseEntity<byte[]> getNocPdf(@RequestParam String loanId) {
        ByteArrayOutputStream byteArrayOutputStream = pdfGenerator.noObjectionCertificatePdf(loanId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_PDF);
        headers.setContentDispositionFormData("attachment", "No Objection Certificate " + loanId + ".pdf");
        headers.setContentLength(byteArrayOutputStream.size());
        return new ResponseEntity<>(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);
    }
}

