package eu.dnetlib.server;

import eu.dnetlib.goldoa.domain.*;
import eu.dnetlib.goldoa.service.AlternativeFundingBidManager;
import eu.dnetlib.goldoa.service.BankTransferReceiptManager;
import eu.dnetlib.goldoa.service.FileManager;
import eu.dnetlib.goldoa.service.InvoiceManager;
import org.apache.commons.io.IOUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by panagiotis on 14/12/2017.
 */
public class AlternativeFundingBidServlet extends HttpServlet {

    private AlternativeFundingBidManager alternativeFundingBidManager;

    @Override
    public void init(ServletConfig config) throws ServletException {

        super.init(config);

        ApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(getServletContext());

        this.alternativeFundingBidManager = (AlternativeFundingBidManager) context.getBean("alternativeFundingBidManager");
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileId = req.getParameter("fileId");
        String mode = req.getParameter("mode");
        String fileType = req.getParameter("fileType");

        try {

            if(fileType.equals("receipt")) {
                BankTransferReceipt file = alternativeFundingBidManager.getBankTransferReceipt(fileId, mode);
                resp.setContentType(file.getContentType());
                resp.setContentLength(file.getContent().length);
                IOUtils.copy(new ByteArrayInputStream(file.getContent()), resp.getOutputStream());
            }else if(fileType.equals("invoice")) {
                Invoice invoice = alternativeFundingBidManager.getInvoice(fileId, mode);
                resp.setContentType(invoice.getFile().getMimetype());
                resp.setContentLength(invoice.getFile().getFile().length);
                IOUtils.copy(new ByteArrayInputStream(invoice.getFile().getFile()), resp.getOutputStream());
            }else {
                File file = alternativeFundingBidManager.getContract(fileId);
                resp.setContentType(file.getMimetype());
                resp.setContentLength(file.getFile().length);
                IOUtils.copy(new ByteArrayInputStream(file.getFile()), resp.getOutputStream());
            }

        } catch (ManagerException e) {
            switch (e.getErrorCause()) {
                case NOT_EXISTS:
                    resp.setStatus(404);
                    resp.getOutputStream().println("File " + fileId + " not found");
                    break;
                case UNKNOWN:
                default:
                    resp.setStatus(500);
                    resp.getOutputStream().println("Error getting file " + fileId);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}