package org.hso.ecommerce.controller;

import org.hso.ecommerce.action.user.UpdateUserSettingsAction;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private final UserRepository userRepository = null;

    @Autowired
    private final CustomerOrderRepository customerOrderRepository = null;

    @GetMapping("/")
    public String user() {
        return "redirect:/user/settings";
    }

    @GetMapping("/settings")
    public String userSettings(Model model,
                               HttpSession session
    ) {
        long userId = (long) session.getAttribute("userId");
        User user = userRepository.findById(userId).get();
        model.addAttribute("user", user);

        return "user/settings";
    }

    @GetMapping("/orders/")
    public String userOrdeers(HttpSession session,
                              Model model
    ) {
        List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId((long) session.getAttribute("userId"));
        model.addAttribute("orders", orders);

        return "user/orders/index";
    }

    @PostMapping("/settings/changeMail")
    public String changeMail(HttpSession session,
                             @RequestParam("email") String email,
                             HttpServletRequest request
    ) {
        User user = userRepository.findById((long) session.getAttribute("userId")).get();

        UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
        UpdateUserSettingsAction.UpdateResult result = cusa.updateEmail(email);
        if (result.updated == false) {
            request.setAttribute("error", result.errorString);
            return "user/settings";
        }

        return "redirect:/user/settings";
    }

    @PostMapping("/settings/changePwd")
    public String changePwd(HttpSession session,
                            @RequestParam("old-password") String oldPassword,
                            @RequestParam("password1") String password1,
                            @RequestParam("password2") String password2,
                            HttpServletRequest request
    ) {
        User user = userRepository.findById((long) session.getAttribute("userId")).get();

        UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
        UpdateUserSettingsAction.UpdateResult result = cusa.updatePassword(oldPassword, password1, password2);
        if (result.updated == false) {
            request.setAttribute("error", result.errorString);
            return "user/settings";
        }

        return "redirect:/user/settings";
    }

    @PostMapping("/settings/changeAddress")
    public String changeAddress(HttpSession session,
                                @RequestParam("salutation") String salutation,
                                @RequestParam("name") String name,
                                @RequestParam("address") String address,
                                HttpServletRequest request
    ) {
        User user = userRepository.findById((long) session.getAttribute("userId")).get();

        UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
        UpdateUserSettingsAction.UpdateResult result = cusa.updateShippingInfo(salutation, name, address);
        if (result.updated == false) {
            request.setAttribute("error", result.errorString);
            return "user/settings";
        }

        return "redirect:/user/settings";
    }

    @PostMapping("/settings/changePaymentInfo")
    public String changePaymentInfo(HttpSession session,
                                    @RequestParam("creditCardNumber") String creditCardNumber,
                                    HttpServletRequest request
    ) {
        User user = userRepository.findById((long) session.getAttribute("userId")).get();

        UpdateUserSettingsAction cusa = new UpdateUserSettingsAction(user, userRepository);
        UpdateUserSettingsAction.UpdateResult result = cusa.updatePaymentInfo(creditCardNumber);
        if (result.updated == false) {
            request.setAttribute("error", result.errorString);
            return "user/settings";
        }

        return "redirect:/user/settings";
    }
}
