package org.hso.ecommerce.app;

import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Optional;

/**
 * TODO clean up this class
 */
@Controller
public class RequestController {

    @Autowired
    private final UserRepository userRepository = null;

    static int notSoRandom = 0;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    public String loginPost(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            HttpSession session
    ) {
        String gto = (String) session.getAttribute("afterLogin");

        Optional<User> user = userRepository.findByEmail(username);
        if (!user.isPresent()) {
            request.setAttribute("error", "Email Adresse falsch.");
            response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
            return "login";
        }

        if (!user.get().validatePassword(password)) {
            request.setAttribute("error", "Passwort falsch.");
            response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
            return "login";
        }

        if (!user.get().isActive) {
            request.setAttribute("error", "User ist deaktiviert.");
            response.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
            return "login";
        }
        
        session.setAttribute("userId", user.get().getId());

        if (gto != null && gto.startsWith("/")) {
            return "redirect:" + gto;
        } else {
            return "redirect:/";
        }
    }

    @PostMapping("/logout")
    public String logoutPost(HttpServletResponse response,
                             HttpSession session
    ) {
        session.removeAttribute("userId");
        return "redirect:/";
    }

    @GetMapping("/intern/")
    public String intern() {
        return "intern/index";
    }
  
    @GetMapping("/intern/customers/")
    public String internCustomers() {
        return "intern/customers/index";
    }

    @GetMapping("/intern/customers/{id}")
    public String internCustomersId() {
        return "intern/customers/id";
    }

    @GetMapping("/intern/customerOrders/")
    public String internCustomerOrder() {
        return "intern/customerOrders/index";
    }

    @GetMapping("/intern/customerOrders/{id}")
    public String internCustomerOrdersId() {
        return "intern/customerOrders/id";
    }

    @GetMapping("/intern/accounting/")
    public String accounting() {
        return "intern/accounting/index";
    }

    @GetMapping("/intern/accounting/vat")
    public String accountingVat() {
        return "intern/accounting/vat";
    }

    @GetMapping("/intern/accounting/main")
    public String accountingIntern() {
        return "intern/accounting/main";
    }

    @GetMapping("/intern/accounting/addManual")
    public String accountingAddManual() {
        return "intern/accounting/addManual";
    }
}
