import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.AccountDB;
import beans.Fish;
import beans.FishDB;

public class Login extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse res) 
  throws ServletException, IOException {
    req.setCharacterEncoding("Windows-31J");
    String account = req.getParameter("account");
    String passwd  = req.getParameter("passwd");
    String tmp;
    HashMap<String, String> accounts = AccountDB.getAccounts();
    HashMap<String, Fish> fish = FishDB.getFish();

    
    if (account!=null && passwd!=null && 
        (tmp=accounts.get(account))!=null && tmp.equals(passwd)) {
      // ログインに成功した。      
      HttpSession session = req.getSession();
      session.setAttribute("account", account);
      session.setAttribute("fish", fish);
      session.setAttribute("cart", new HashMap<String,Integer>());
      session.setAttribute("total", 0);
      res.sendRedirect(res.encodeRedirectURL("ItemList"));      
    } else {
      res.sendRedirect(res.encodeRedirectURL("jsp/LoginFailed.jsp"));
    }   
  }
}
