package com.ffii.towngas.example.web;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ffii.core.common.mail.pojo.MailRequest;
import com.ffii.core.common.mail.service.MailService;
import com.ffii.core.exception.InternalServerErrorException;
import com.ffii.core.utils.JasperUtils;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

// @RestController
@RequestMapping("/public/example")
public class PublicExampleController {

  private MailService mailService;

  public PublicExampleController(MailService mailService) {
    this.mailService = mailService;
  }

  @GetMapping("/example.pdf")
  public void examplePdf(HttpServletResponse response) throws JRException, IOException {
    try (InputStream imgIn = new ClassPathResource("reports/cat.jpg").getInputStream()) {
      var jRerpot = JasperUtils.compile(
          "reports/example",
          Map.of(
              "field1", "field111",
              "subField1", "subField11111",
              "img", ImageIO.read(imgIn),
              "subreport1Records",
              new JRBeanCollectionDataSource(
                  Arrays.asList(Map.of("subColumn1", "sub column1111", "subColumn2", "sub column222")))),
          new JRBeanCollectionDataSource(Arrays.asList(Map.of("column1", "column1111", "column2", "column222"))),
          Map.of("subreport1", "reports/subexample"));
      JasperUtils.responsePdf(response, jRerpot, "filename1", false);
    }
  }

  @GetMapping("/example.docx")
  public void exampleDocx(HttpServletResponse response) throws JRException, IOException {
    try (InputStream imgIn = new ClassPathResource("reports/cat.jpg").getInputStream()) {
      var jRerpot = JasperUtils.compile(
          "reports/example",
          Map.of(
              "field1", "field111",
              "subField1", "subField11111",
              "img", ImageIO.read(imgIn),
              "subreport1Records",
              new JRBeanCollectionDataSource(
                  Arrays.asList(Map.of("subColumn1", "sub column1111", "subColumn2", "sub column222")))),
          new JRBeanCollectionDataSource(Arrays.asList(Map.of("column1", "column1111", "column2", "column222"))),
          Map.of("subreport1", "reports/subexample"));
      JasperUtils.responseDocx(response, jRerpot, "filename1");
    }
  }

  private MailRequest genMailReq() {
    try (InputStream imgIn = new ClassPathResource("reports/cat.jpg").getInputStream()) {
      return MailRequest.builder()
          .subject("hi")
          .template("mail/content")
          .args(Map.of("content", "hi"))
          .priority(MailRequest.PRIORITY_HIGHEST)
          .addTo(new InternetAddress("kf.chu@2fi-solutions.com.hk", "fung"))
          // .addCc(new InternetAddress("matthew.ng@2fi-solutions.com.hk", "matthew"))
          // .replyTo(new InternetAddress("matthew.ng@2fi-solutions.com.hk", "matthew"))
          .addAttachment("cat.jpg", imgIn.readAllBytes())
          .build();
    } catch (UnsupportedEncodingException e) {
      throw new InternalServerErrorException();
    } catch (FileNotFoundException e) {
      throw new InternalServerErrorException();
    } catch (IOException e) {
      throw new InternalServerErrorException();
    }
  }

  @GetMapping("/mail")
  public void mail() {
    mailService.send(genMailReq());
  }

  @GetMapping("/mail-async")
  public void mailAsync() {
    mailService.asyncSend(genMailReq());
  }

}
