package ru.lotnyk.form.action_servlet;

import ru.lotnyk.form.connection_db.ConnectionPool;
import ru.lotnyk.form.model.User;
import ru.lotnyk.http.config.Configuration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Class ShowUser.
 * @author Roman Lotnyk(romanlotnik@yandex.ru)
 * @version 1
 * @since 28.07.2017
 */
public class ShowUser extends HttpServlet {

    /**
     * action.
     */
    private Action action;

    /**
     * init.
     * @throws ServletException "ServletException"
     */
    @Override
    public void init() throws ServletException {
        Configuration configuration = new Configuration();
        ConnectionPool pool = new ConnectionPool(configuration.getValue("driver"),
                configuration.getValue("url"),
                configuration.getValue("username"),
                configuration.getValue("password"));
        this.action = new ActionDB(pool);
    }

    /**
     * doGet.
     * @param req req
     * @param resp resp
     * @throws ServletException "ServletException"
     * @throws IOException "IOException"
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        StringBuilder sb = new StringBuilder();
        sb.append("<!DOCTYPE html>")
                .append("<head>")
                .append("<title>index</title>")
                .append("<body>")
                .append(String.format("<form action=%s/add>", req.getContextPath()))
                .append("<input type = 'submit' value = 'Add User'/></form></br>")
                .append(String.format("<form action=%s/edit>", req.getContextPath()))
                .append("<input type = 'submit' value = 'Edit User'/></form></br>")
                .append(String.format("<form action=%s/delete>", req.getContextPath()))
                .append("<input type = 'submit' value = 'Delete User'/></form></br>")
                .append("<table border = '1'>")
                .append("<tr>")
                .append("<th>ID</th>")
                .append("<th>Name</th>")
                .append("<th>Login</th>")
                .append("<th>Email</th>")
                .append("<th>Create Date</th>");


            for (User user : this.action.showUser()) {
                    sb.append("<tr>")
                    .append(String.format("<td>%s</td>", user.getId()))
                    .append(String.format("<td>%s</td>", user.getName()))
                    .append(String.format("<td>%s</td>", user.getLogin()))
                    .append(String.format("<td>%s</td>", user.getEmail()))
                    .append(String.format("<td>%s</td>", user.getDate()))
                    .append("</tr>");
            }
                    sb.append("</table>")
                    .append("</body>")
                    .append("</html>");
        PrintWriter writer = resp.getWriter();
        writer.append(sb.toString());
        writer.flush();

    }
}
