package com.reallinkbilling.render;

import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class RenderInvoiceImgPDFFile extends ActionSupport {

    private static final long serialVersionUID = 1L;

    private HttpSession session;

    public void rederImgPassing() {

        HttpServletResponse response =
                ServletActionContext.getResponse();

        String purchaseNumber =
                ServletActionContext.getRequest().getParameter("id");

        session =
                ServletActionContext.getRequest().getSession(false);

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            String db = (String) session.getAttribute("db");

            String shopId =
                    (String) session.getAttribute("shopId");

            String shopCode =
                    (String) session.getAttribute("shopCode");

            if (db != null) {

                connection =
                        new GetDBConnection().getDBConnection(db);

                String sql =
                        "SELECT invoice_pic " +
                        "FROM shop_purchase_info_master " +
                        "WHERE purchase_number = ? " +
                        "AND shop_id = ? " +
                        "AND shop_code = ?";

                preparedStatement =
                        connection.prepareStatement(sql);

                preparedStatement.setString(1, purchaseNumber);
                preparedStatement.setString(2, shopId);
                preparedStatement.setString(3, shopCode);

                resultSet =
                        preparedStatement.executeQuery();

                if (resultSet.next()) {

                    Blob imageBlob =
                            resultSet.getBlob("invoice_pic");

                    if (imageBlob != null) {

                        byte[] data =
                                imageBlob.getBytes(
                                        1,
                                        (int) imageBlob.length()
                                );

                        String contentType =
                                "application/octet-stream";

                        String fileExtension =
                                ".bin";

                        /*
                         * PDF
                         */
                        if (data.length >= 4 &&
                            data[0] == 0x25 &&
                            data[1] == 0x50 &&
                            data[2] == 0x44 &&
                            data[3] == 0x46) {

                            contentType =
                                    "application/pdf";

                            fileExtension =
                                    ".pdf";
                        }

                        /*
                         * JPG / JPEG
                         */
                        else if (data.length >= 3 &&
                                (data[0] & 0xFF) == 0xFF &&
                                (data[1] & 0xFF) == 0xD8 &&
                                (data[2] & 0xFF) == 0xFF) {

                            contentType =
                                    "image/jpeg";

                            fileExtension =
                                    ".jpg";
                        }

                        /*
                         * PNG
                         */
                        else if (data.length >= 4 &&
                                (data[0] & 0xFF) == 0x89 &&
                                (data[1] & 0xFF) == 0x50 &&
                                (data[2] & 0xFF) == 0x4E &&
                                (data[3] & 0xFF) == 0x47) {

                            contentType =
                                    "image/png";

                            fileExtension =
                                    ".png";
                        }

                        /*
                         * GIF
                         */
                        else if (data.length >= 6 &&
                                data[0] == 'G' &&
                                data[1] == 'I' &&
                                data[2] == 'F') {

                            contentType =
                                    "image/gif";

                            fileExtension =
                                    ".gif";
                        }

                        response.setContentType(contentType);

                        /*
                         * Open file in browser
                         */
                        response.setHeader(
                                "Content-Disposition",
                                "inline; filename=\"Purchase_Invoice_"
                                + purchaseNumber
                                + fileExtension
                                + "\""
                        );

                        response.setContentLength(data.length);

                        OutputStream out =
                                response.getOutputStream();

                        out.write(data);
                        out.flush();
                        out.close();
                    }
                }
            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (Exception e) {
                }
            }

            if (preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (Exception e) {
                }
            }

            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                }
            }
        }
    }
}