package com.j2ee.demo;

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 javax.servlet.http.HttpSession;
import java.io.*;

@WebServlet("/Welcome")
public class Welcome extends HttpServlet {

    static int count_3 = 0;

    void printWelcome(String username, String password, String gender, String email, String phoneNum, PrintWriter out) {
        out.println("<html><body>");
        out.println("<h1> Welcome, " + username + "</h1>");
        out.println("<h2> Here is your password, " + password + "</h2>");
        out.println("<h2> You are a " + gender + "</h2>");
        out.println("<h2> This is your email " + email + "</h2>");
        out.println("<h2> This is your phone number " + phoneNum + "</h2>");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//        String ssid = hs.getId();
//        out.println(ssid);

        String vName = req.getParameter("username");
        String vPass = req.getParameter("password");
        String vGender = req.getParameter("gender");
        String vEmail = req.getParameter("email");
        String vPhoneNumber = req.getParameter("phone");
        String rememberMe = req.getParameter("rememberMe");

        PrintWriter out = resp.getWriter();

//        HttpSession hs = req.getSession();
//        Object isLoggedIn = hs.getAttribute("isLoggedIn");
//        out.println(isLoggedIn);

        resp.setContentType("text/html");

        printWelcome(vName, vPass, vGender, vEmail, vPhoneNumber, out);


        out.println("</body></html>");
        System.out.println("rememberMe: " + rememberMe);

        Object cookieExists = req.getSession().getAttribute("cookiesExists");
        System.out.println("cookiesexst " + cookieExists);

        if (vName != null && vPass != null && vGender != null && vEmail != null && vPhoneNumber != null) {
            cookieExists = null;
        }

        if (cookieExists == null) {

            Cookie usernameC = new Cookie("username", vName);
            usernameC.setMaxAge(60 * 60 * 24);
            resp.addCookie(usernameC);

            Cookie passwordC = new Cookie("password", vPass);
            passwordC.setMaxAge(60 * 60 * 24);
            resp.addCookie(passwordC);

            Cookie genderC = new Cookie("gender", vGender);
            passwordC.setMaxAge(60 * 60 * 24);
            resp.addCookie(genderC);

            Cookie emailC = new Cookie("email", vEmail);
            passwordC.setMaxAge(60 * 60 * 24);
            resp.addCookie(emailC);

            Cookie phoneNumC = new Cookie("phoneNum", vPhoneNumber);
            passwordC.setMaxAge(60 * 60 * 24);
            resp.addCookie(phoneNumC);

            Cookie remember = new Cookie("rememberMe", rememberMe);
            passwordC.setMaxAge(60 * 60 * 24);
            resp.addCookie(remember);

            req.getSession().setAttribute("cookiesExists", true);
        }

        Object username = req.getAttribute("username");
        Object password = req.getAttribute("password");
        Object gender = req.getAttribute("gender");
        Object email = req.getAttribute("email");
        Object phoneNum = req.getAttribute("phoneNum");

        PrintWriter out = resp.getWriter();

        resp.setContentType("text/html");

        if (cookieExists != null) {
            String usernameStr = username != null ? username.toString() : "";
            String passwordStr = password != null ? password.toString() : "";
            String genderStr = gender != null ? gender.toString() : "";
            String emailStr = email != null ? email.toString() : "";
            String phoneNumStr = phoneNum != null ? phoneNum.toString() : "";
            printWelcome(usernameStr.toString(),
                    passwordStr.toString(),
                    genderStr.toString(),
                    emailStr.toString(),
                    phoneNumStr.toString(),
                    out);

        } else {
            printWelcome(vName, vPass, vGender, vEmail, vPhoneNumber, out);
        }


        out.println("</body></html>");
        String requestURI = req.getRequestURI();

        if (requestURI.endsWith("/s03")) {
            count_3++;
        }

        System.out.println(count_3);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("Go to doGet");
        doGet(req, resp);
    }
}