package org.hso.ecommerce.app;

import org.hso.ecommerce.contoller.Login;
import org.hso.ecommerce.db.CustomerRepository;
import org.hso.ecommerce.entities.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

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

    private final CustomerRepository customerRepo;

    @Autowired
    public RequestController(CustomerRepository customerRepo) {
        this.customerRepo = customerRepo;
    }

    @GetMapping("/")
    public String greeting() {
        return "redirect:/home";
    }

    @GetMapping("/home")
    public String home(@CookieValue(value = "loginToken", defaultValue = "") String loginToken, Model model) {
        if (!loginToken.equals("")) {
            model.addAttribute(new Customer());
        }
        return "home";
    }

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

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

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

    @GetMapping("/intern/customerdetail")
   public String customerdetail() {
      // TODO @PH
        return "intern/customer";
   }

    @GetMapping("/intern/customerorders")
    public String customerorders() {
      // TODO @PH
        return "intern/customerorders";
    }

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

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

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

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

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

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

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

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

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

    @GetMapping("/customer/accountsettings")
    public String customerAccountSettings(Model model) {
        Customer customer = new Customer();
        customer.setFirstname("Max");
        customer.setLastname("Mustermann");
        customer.setUsername("Max.TestKunde");
        customer.setPassword("test123");
        model.addAttribute(customer);

        model.addAttribute("fullname", customer.getFirstname() + " " + customer.getLastname());
        model.addAttribute("email", "Test.User@ecommere.com");

        model.addAttribute("street", "Musterstraße 42a");
        model.addAttribute("city", "Musterstadt");
        model.addAttribute("zipcode", "12345");
        model.addAttribute("country", "Musterland");

        return "/customer/accountSettings";
    }

    @RequestMapping(value="/updateAccountSettings", method=RequestMethod.POST, params="action=updateAccountSettings")
    public String updateAccountSettings(@ModelAttribute Customer customer, HttpServletResponse response) {
        // do the login magic and get a loginToken
        System.out.println(customer.username);
        System.out.println(customer.password);

        return "/customer/accountsettings";
    }

    @GetMapping("/login")
    public String login(@CookieValue(value = "loginToken", defaultValue = "") String loginToken, Model model) {
        model.addAttribute(new Customer());

        System.out.println(loginToken); // TODO if cookie is present, redirect to home
        return "login";
    }
    @RequestMapping(value="/login", method=RequestMethod.POST, params="action=login")
    public String loginAction(@ModelAttribute Customer customer, HttpServletResponse response) {
        Cookie cookie = new Login(customerRepo).getLoginToken(customer);
        if (cookie != null) {
            response.addCookie(cookie);
            return "redirect:home";
        } else {
            return "redirect:login"; // redirect so the input files get cleared, otherwise only pwd gets cleared
        }
    }

    @GetMapping("/register")
    public String register(@CookieValue(value = "loginToken", defaultValue = "") String loginToken, Model model) {
        model.addAttribute(new Customer());

        System.out.println(loginToken); // TODO if cookie is present, redirect to home
        return "register";
    }



    @RequestMapping(value="/register", method=RequestMethod.POST, params="action=register")
    public String registerAction(@ModelAttribute Customer customer, HttpServletResponse response) {
        // do the register magic and get a loginToken
        System.out.println(customer.username);
        System.out.println(customer.password);

        if (customerRepo.findByUsername(customer.username).size() != 0) {
            // TODO
            System.out.println("The customer exists already");
            return "redirect:register";
        } else {
            customerRepo.save(customer);
            System.out.println(customerRepo.findByUsername(customer.username).size());
        }

        // return a login token after successful registration
        String loginToken = UUID.randomUUID().toString();

        // set the loginToken as session cookie
        Cookie cookie = new Cookie("loginToken", loginToken);
        response.addCookie(cookie);
        return "home";
    }

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

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

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

}
