package requestHandlers;

import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import requestHandlers.djonDB.addItem_djon;
import requestHandlers.djonDB.addReceipt_djon;
import requestHandlers.djonDB.getCards_djon;
import requestHandlers.djonDB.getItems_djon;
import requestHandlers.djonDB.getMerchants_djon;
import requestHandlers.djonDB.getReceiptImg_djon;
import requestHandlers.djonDB.getReceipts2_djon;
import requestHandlers.djonDB.getRecordYears_djon;
import requestHandlers.djonDB.getSpendingCurve_djon;
import requestHandlers.djonDB.getStatePie_djon;
import requestHandlers.djonDB.loginH_djon;
import requestHandlers.djonDB.registerH_djon;
import requestHandlers.mongoDB.addItem_mongo;
import requestHandlers.mongoDB.addReceipt_mongo;
import requestHandlers.mongoDB.getCards_mongo;
import requestHandlers.mongoDB.getItems_mongo;
import requestHandlers.mongoDB.getMerchants_mongo;
import requestHandlers.mongoDB.getReceiptImg_mongo;
import requestHandlers.mongoDB.getReceipts2_mongo;
import requestHandlers.mongoDB.getRecordYears_mongo;
import requestHandlers.mongoDB.getSpendingCurve_mongo;
import requestHandlers.mongoDB.getStatePie_mongo;
import requestHandlers.mongoDB.loginH_mongo;
import requestHandlers.mongoDB.registerH_mongo;
import djondb.Loader;

/**
 * Servlet implementation class MainHandler
 */
@WebServlet("/MainHandler")
public class MainHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Hashtable           _postHandlers = new Hashtable(100);
    private Hashtable           _getHandlers = new Hashtable(100);
    
 // request handlers
    private RequestHandler _login;
    private RequestHandler _logout;
    private RequestHandler _register;
    private RequestHandler _getHistory;
    private RequestHandler _getStatPie;
    private RequestHandler _getMerchants;
    private RequestHandler _getCards;
    private RequestHandler _getReceipts;
    private RequestHandler _getReceipts2;
    private RequestHandler _getItems;
    private RequestHandler _addReceipt;
    private RequestHandler _addItem;
    private RequestHandler _getSpendingCurve;
    private RequestHandler _getRecordYears;
    private RequestHandler _getReceiptImg;
    
    // djon ones baby
    private RequestHandler _loginDjon;
    private RequestHandler _registerDjon;
    private RequestHandler _getStatePieDjon;
    private RequestHandler _getSpendingCurveDjon;
    private RequestHandler _getRecordYearsDjon;
    private RequestHandler _getReceipts2Djon;
    private RequestHandler _getReceiptImgDjon;
    private RequestHandler _getMerchantsDjon;
    private RequestHandler _getItemsDjon;
    private RequestHandler _getCardsDjon;
    private RequestHandler _addReceiptDjon;
    private RequestHandler _addItemDjon;
    
    // mongo ones baby
    private RequestHandler _loginMongo;
    private RequestHandler _registerMongo;
    private RequestHandler _getStatePieMongo;
    private RequestHandler _getReceipts2Mongo;
    private RequestHandler _getSpendingCurveMongo;
    private RequestHandler _getRecordYearsMongo;
    private RequestHandler _getReceiptImgMongo;
    private RequestHandler _getMerchantsMongo;
    private RequestHandler _getItemsMongo;
    private RequestHandler _getCardsMongo;
    private RequestHandler _addReceiptMongo;
    private RequestHandler _addItemMongo;
    
    private RequestHandler _uploadFile;
    
    private String              _msg = "";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainHandler() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		try
		{
	        Loader.initialize();
			removeAllPostRequestHandlers();
			removeAllGetRequestHandlers();
			_login = new loginH();
			_logout = new logoutH();
			_register = new registerH();
			_getHistory = new getHistory();
			_getStatPie = new getStatPie();
			_getMerchants = new getMerchants();
			_getCards = new getCards();
			_getReceipts = new getReceipts();
			_getReceipts2 = new getReceipts2();
			_getItems = new getItems();
			_addReceipt = new addReceipt();
			_addItem = new addItem();
			_getSpendingCurve = new getSpendingCurve();
			_getRecordYears = new getRecordYears();
			_getReceiptImg = new getReceiptImg();
			
			addPostRequestHandler("login", _login);
			addPostRequestHandler("logout", _logout);
            addPostRequestHandler("register", _register);
            addPostRequestHandler("getHisotry", _getHistory);
            addPostRequestHandler("getStatPie", _getStatPie);
            addPostRequestHandler("getMerchants", _getMerchants);
            addPostRequestHandler("getCards", _getCards);
            addPostRequestHandler("getReceipts", _getReceipts);
            addPostRequestHandler("getReceipts2", _getReceipts2);
            addPostRequestHandler("getItems", _getItems);
            addPostRequestHandler("addReceipt", _addReceipt);
            addPostRequestHandler("addItem", _addItem);
            addPostRequestHandler("getSpendingCurve", _getSpendingCurve);
            addPostRequestHandler("getRecordYears", _getRecordYears);
            addPostRequestHandler("getReceiptImg", _getReceiptImg);
            
            
            addGetRequestHandler("login", _login);
            addGetRequestHandler("logout", _logout);
            addGetRequestHandler("register", _register);
            addGetRequestHandler("getHisotry", _getHistory);
            addGetRequestHandler("getStatPie", _getStatPie);
            addGetRequestHandler("getMerchants", _getMerchants);
            addGetRequestHandler("getCards", _getCards);
            addGetRequestHandler("getReceipts", _getReceipts);
            addGetRequestHandler("getReceipts2", _getReceipts2);
            addGetRequestHandler("getItems", _getItems);
            addGetRequestHandler("addReceipt", _addReceipt);
            addGetRequestHandler("addItem", _addItem);
            addGetRequestHandler("getSpendingCurve", _getSpendingCurve);
            addGetRequestHandler("getRecordYears", _getRecordYears);
            addGetRequestHandler("getReceiptImg", _getReceiptImg);
            
            
            // djon ones baby
            
            _loginDjon = new loginH_djon();
            _registerDjon = new registerH_djon();
            _getStatePieDjon = new getStatePie_djon();
            _getSpendingCurveDjon = new getSpendingCurve_djon();
            _getRecordYearsDjon = new getRecordYears_djon();
            _getReceipts2Djon = new getReceipts2_djon();
            _getReceiptImgDjon = new getReceiptImg_djon();
            _getMerchantsDjon = new getMerchants_djon();
            _getItemsDjon = new getItems_djon();
            _getCardsDjon = new getCards_djon();
            _addReceiptDjon = new addReceipt_djon();
            _addItemDjon = new addItem_djon();
            
            addPostRequestHandler("loginDjon", _loginDjon);
            addPostRequestHandler("registerDjon", _registerDjon);
            addPostRequestHandler("getStatPieDjon", _getStatePieDjon);
            addPostRequestHandler("getSpendingCurveDjon", _getSpendingCurveDjon);
            addPostRequestHandler("getRecordYearsDjon", _getRecordYearsDjon);
            addPostRequestHandler("getReceipts2Djon", _getReceipts2Djon);
            addPostRequestHandler("getReceiptImgDjon", _getReceiptImgDjon);
            addPostRequestHandler("getMerchantsDjon", _getMerchantsDjon);
            addPostRequestHandler("getItemsDjon", _getItemsDjon);
            addPostRequestHandler("getCardsDjon", _getCardsDjon);
            addPostRequestHandler("addItemDjon", _addItemDjon);
            addPostRequestHandler("addReceiptDjon", _addReceiptDjon);
            
            
            addGetRequestHandler("loginDjon", _loginDjon);
            addGetRequestHandler("registerDjon", _registerDjon);
            addGetRequestHandler("getStatPieDjon", _getStatePieDjon);
            addGetRequestHandler("getSpendingCurveDjon", _getSpendingCurveDjon);
            addGetRequestHandler("getRecordYearsDjon", _getRecordYearsDjon);
            addGetRequestHandler("getReceipts2Djon", _getReceipts2Djon);
            addGetRequestHandler("getReceiptImgDjon", _getReceiptImgDjon);
            addGetRequestHandler("getMerchantsDjon", _getMerchantsDjon);
            addGetRequestHandler("getItemsDjon", _getItemsDjon);
            addGetRequestHandler("getCardsDjon", _getCardsDjon);
            addGetRequestHandler("addItemDjon", _addItemDjon);
            addGetRequestHandler("addReceiptDjon", _addReceiptDjon);
            
            // mongo ones baby
            _loginMongo = new loginH_mongo();
            _getReceipts2Mongo = new getReceipts2_mongo();
            _registerMongo = new registerH_mongo();
            _getStatePieMongo = new getStatePie_mongo();
            _getSpendingCurveMongo = new getSpendingCurve_mongo();
            _getRecordYearsMongo = new getRecordYears_mongo();
            _getReceiptImgMongo = new getReceiptImg_mongo();
            _getMerchantsMongo = new getMerchants_mongo();
            _getItemsMongo = new getItems_mongo();
            _getCardsMongo = new getCards_mongo();
            _addReceiptMongo = new addReceipt_mongo();
            _addItemMongo = new addItem_mongo();
            
            addPostRequestHandler("loginMongo", _loginMongo);
            addPostRequestHandler("registerMongo", _registerMongo);
            addPostRequestHandler("getReceipts2Mongo", _getReceipts2Mongo);
            addPostRequestHandler("getStatPieMongo", _getStatePieMongo);
            addPostRequestHandler("getSpendingCurveMongo", _getSpendingCurveMongo);
            addPostRequestHandler("getRecordYearsMongo", _getRecordYearsMongo);
            addPostRequestHandler("getReceiptImgMongo", _getReceiptImgMongo);
            addPostRequestHandler("getMerchantsMongo", _getMerchantsMongo);
            addPostRequestHandler("getItemsMongo", _getItemsMongo);
            addPostRequestHandler("getCardsMongo", _getCardsMongo);
            addPostRequestHandler("addItemMongo", _addItemMongo);
            addPostRequestHandler("addReceiptMongo", _addReceiptMongo);
            
            
            addGetRequestHandler("loginMongo", _loginMongo);
            addGetRequestHandler("registerMongo", _registerMongo);
            addGetRequestHandler("getReceipts2Mongo", _getReceipts2Mongo);
            addGetRequestHandler("getStatPieMongo", _getStatePieMongo);
            addGetRequestHandler("getSpendingCurveMongo", _getSpendingCurveMongo);
            addGetRequestHandler("getRecordYearsMongo", _getRecordYearsMongo);
            addGetRequestHandler("getReceiptImgMongo", _getReceiptImgMongo);
            addGetRequestHandler("getMerchantsMongo", _getMerchantsMongo);
            addGetRequestHandler("getItemsMongo", _getItemsMongo);
            addGetRequestHandler("getCardsMongo", _getCardsMongo);
            addGetRequestHandler("addItemMongo", _addItemMongo);
            addGetRequestHandler("addReceiptMongo", _addReceiptMongo);
            
            _uploadFile = new uploadFile();
            addPostRequestHandler("uploadFile", _uploadFile);
            addGetRequestHandler("uploadFile", _uploadFile);
		}
		catch(Exception e)
        {
            _msg = _msg + "init() failed. " + e.getMessage();
            System.out.println(_msg);
        }
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		String handlerName = request.getParameter("handler");
		System.err.println("handler="+handlerName);
		RequestHandler rh = getGetRequestHandler(handlerName);
		rh.processRequest(this, request, response, null, null);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");

		String handlerName = request.getParameter("handler");
		RequestHandler rh = getPostRequestHandler(handlerName);
		System.err.println("handler="+handlerName);
		rh.processRequest(this, request, response, null, null);
	}
    public void addPostRequestHandler(String name, RequestHandler handler)
    {
        _postHandlers.put(name, handler);
    }

    public void removePostRequestHandler(String name)
    {
        _postHandlers.remove(name);
    }

    public void removeAllPostRequestHandlers()
    {
        _postHandlers.clear();
    }

    public void addGetRequestHandler(String name, RequestHandler handler)
    {
        _getHandlers.put(name, handler);
    }

    public void removeGetRequestHandler(String name)
    {
        _getHandlers.remove(name);
    }

    public void removeAllGetRequestHandlers()
    {
        _getHandlers.clear();
    }

    public RequestHandler getPostRequestHandler(String name)
    {
        return (RequestHandler)_postHandlers.get(name);
    }
    
    public RequestHandler getGetRequestHandler(String name)
    {
    	return (RequestHandler)_getHandlers.get(name);
    }
}
