/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Dao.BulkMarkDaoSTB;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 1004242
 */
public class BulkMarkServletSTB extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
     @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession(false);
        String pacsIdSTB = (String) session.getAttribute("pacsId");
        //String pacsId = request.getParameter("pacsId_STB");
        System.out.println("PacsId SIB: "+pacsIdSTB+" PacsId Brch: "+pacsId);
        //String userId = request.getParameter("userName");
        String userId = (String) session.getAttribute("user");
        ServletOutputStream stream = null;
        BufferedInputStream buf = null;
        BulkMarkDaoSTB bDao = new BulkMarkDaoSTB();
        String action = request.getParameter("temp");

        try {
          if (action.equalsIgnoreCase("allAcc")) {
           // String accNo = request.getParameter("accNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccounts(accNo,pacsId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("markBulk")) {
            response.setContentType("application/json");
           // String accNo = request.getParameter("accNo");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.markBulkAccount(accNo,pacsId,userId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("allAccAuth")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccountsAuth(pacsId);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("allAccAuth_Exs")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.bulkMarkAccountsAuth_Exs(pacsId);
            out.print(jsonStr);
            out.flush();
          }
           else if (action.equalsIgnoreCase("unmark")) {
            //String accNo = request.getParameter("accNo");
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.unmarkAccountFromAuth(accNo,pacsId,userId);
            out.print(jsonStr);
            out.flush();
          }
           else if (action.equalsIgnoreCase("authBulk")) { 
           // String jsonStr =  bDao.updateHeader(pacsId,userId);            
            String jsonStr="success";
            if(jsonStr.contains("success")){
            String mark = request.getParameter("mark_Type");
                if (mark.equalsIgnoreCase("E")) {
                    String message = bDao.gerateBulkFlatFile_Exs(pacsId,userId);
                 if (message != null) {
                     request.setAttribute("message", message);
                     request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);

                   }
                 else {
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
                   }
                }
               else {
               String message = bDao.gerateBulkFlatFile(pacsId,userId);
                 if (message != null) {
//                       stream = response.getOutputStream();
//                      response.setContentType("text/plain");
//                       response.addHeader("Content-Disposition", "attachment; filename="
//                               + bulkFile.getName());
//                       response.setContentLength((int) bulkFile.length());
//                      FileInputStream input = new FileInputStream(bulkFile);
//                      buf = new BufferedInputStream(input);
//                       int readBytes = 0;
//
//                       while ((readBytes = buf.read()) != -1) {
//                           stream.write(readBytes);
//                       }
                     request.setAttribute("message", message);
                     request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
                       
                   }
                 else {
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
                   }
                }
             }
            else{
                     request.setAttribute("message", jsonStr);
                     request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
             }
           
          }
//Added New for Viewing PAcs wise count of Txns for Bank Login
          else if (action.equalsIgnoreCase("viewAccAuthSTB")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.viewBulkMarkAccountsAuth(pacsIdSTB);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("viewAccAuthSTB_Exs")) {
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            String jsonStr = bDao.viewBulkMarkAccountsAuth_Exs(pacsIdSTB);
            out.print(jsonStr);
            out.flush();
          }
          else if (action.equalsIgnoreCase("redirectBulkForm")) {
              //String paccsId = request.getParameter("tempId2");
              String mType = "N";
              System.out.println("Pacs ID for click: "+paccsId);
              request.setAttribute("messageSTB", paccsId);
              request.setAttribute("markType", mType);
              request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
          }
          else if (action.equalsIgnoreCase("redirectBulkForm_Exs")) {
             // String paccsId = request.getParameter("tempId2");
              String mType = "E";
              System.out.println("Pacs ID for click: "+paccsId);
              request.setAttribute("messageSTB", paccsId);
              request.setAttribute("markType", mType);
              request.getRequestDispatcher("/Deposit/authBulkIncr_STB.jsp").forward(request, response);
          }
//End Changes
          
//          else if (action.equalsIgnoreCase("allTran"))
//          {
//            response.setContentType("application/json");
//            PrintWriter out = response.getWriter();
//            String jsonStr = wDao.getAllTranDetails(userId ,pacsId);
//            out.print(jsonStr);
//            out.flush();
//          }

        } catch (Exception e) {
           // e.printStackTrace();
           System.out.println("Error occurred during processing.");

        } finally { 
                    System.out.println("Processing done."); 
                }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}