package org.hso.ecommerce.app;

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

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("/")
//    public String home() {
//        return "redirect:/shop/";
//    }

    @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";
        }

        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("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String registerPost(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("password2") String password2,
            @RequestParam("type") String type
    ) {

        return "redirect:/";
    }

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

    @GetMapping("/shop/search")
    public String shopSearch() {
        return "shop/search";
    }

/*    @GetMapping("/shop/checkout")
    public String shopCheckout(HttpSession session, HttpServletRequest request) {
        session.setAttribute("afterLogin", request.getRequestURI());
        return "shop/checkout";
    }

    @PostMapping("/shop/checkoutFinish")
    public String shopCheckoutFinish() {
        return "shop/checkoutFinish";
    }

    @GetMapping("/shop/checkoutFinish")
    public String shopCheckoutFinishGET() {
        return "shop/checkoutFinish";
    }*/

//    @GetMapping("/shop/articles/{id}")
//    public String shopArticlesById() {
//        return "shop/articles/id";
//    }
//
//    @PostMapping("/shop/articles/{id}")
//    public String shopArticlesByIdBuy(HttpSession session,
//                                      @RequestAttribute(value = "user", required = false) User customer,
//                                      @PathVariable("id") Integer id,
//                                      @RequestParam("fastcheckout") Boolean fastcheckout
//    ) {
//        if (customer != null) {
//            if (!fastcheckout) {
//                return "shop/articles/post_add";
//            } else {
//                return "shop/checkout";
//            }
//        } else {
//            session.setAttribute("afterLogin", "/shop/articles/" + id);
//            return "redirect:/login";
//        }
//    }

//    @GetMapping("/about")
//    public String about() {
//        return "about";
//    }
//
//    @GetMapping("/terms")
//    public String terms() {
//        return "terms";
//    }
//
//    @GetMapping("/privacy")
//    public String privacy() {
//        return "privacy";
//    }


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

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

    @GetMapping("/intern/listedArticles/{id}")
    public String internListedArticlesId() {
        return "intern/listedArticles/id";
    }


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

    @GetMapping("/intern/articles/{id}")
    public String internArticlesId() {
        return "intern/articles/id";
    }

    @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/suppliers/")
    public String internSuppliers() {
        return "intern/suppliers/index";
    }

    @GetMapping("/intern/suppliers/{id}")
    public String internSuppliersId() {
        return "intern/suppliers/id";
    }

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

    @GetMapping("/intern/supplierOrders/{id}")
    public String internSupplierOrdersId() {
        return "intern/supplierOrders/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";
    }

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

    @GetMapping("/intern/warehouse/todo")
    public String accountingWarehouseTodo() {
        return "intern/warehouse/todo";
    }

    @GetMapping("/intern/warehouse/addManual")
    public String accountingWarehouseAddManual() {
        return "intern/warehouse/addManual";
    }

    @PostMapping("/intern/warehouse/progress/{id}")
    public String accountingWarehouseProgressIdPost(HttpServletResponse response) {
        if ((notSoRandom++) % 2 == 1) {
            return "redirect:/intern/warehouse/progress/450";
        } else {
            response.setStatus(409);
            return "intern/warehouse/error_progress_failed";
        }
    }

    @GetMapping("/intern/warehouse/progress/{id}")
    public String accountingWarehouseProgressId() {
        return "intern/warehouse/id_progress";
    }

    @GetMapping("/intern/warehouse/slots/")
    public String accountingWarehouseSlots() {
        return "intern/warehouse/slots/index";
    }
}
