package org.hso.ecommerce.controller.shop;

import org.hso.ecommerce.entities.shop.Article;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

@Controller
@RequestMapping("/")
public class ShopIndexController {

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

    @GetMapping("/shop/")
    public String shop(Model model, HttpSession session) {

        //TODO: get commercialised Articles
        ArrayList<Article> commercialArticles = getArticles();
        model.addAttribute("commercialArticles", commercialArticles);

        //check if logged in
        boolean isLoggedIn = false;
        boolean hasOrders = false;
        if (session != null && session.getAttribute("id") != null) {
            long userId = (long) session.getAttribute("id");
            isLoggedIn = true;
            if (false) {
                hasOrders = true;   //TODO: Find out whether user has orders!
            }
        }
        model.addAttribute("isLoggedIn", isLoggedIn);
        model.addAttribute("hasOrders", hasOrders);


        if (hasOrders) {
            //TODO: get up to last 4 Orders
            ArrayList<Article> suggestedArticles = getArticles();
            model.addAttribute("suggestedArticles", suggestedArticles);
        }

        return "shop/index";
    }

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

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

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


    public ArrayList<Article> getArticles() {
        ArrayList<Article> dummyArticles = new ArrayList<Article>();

        Article d1 = new Article();
        d1.description = "this is dummy1";
        d1.title = "dummy1";
        d1.shopPricePerUnitNetCent = 1500;
        d1.id = 1234;
        dummyArticles.add(d1);

        Article d2 = new Article();
        d2.description = "this is dummy2";
        d2.title = "dummy2";
        d2.shopPricePerUnitNetCent = 2000;
        d2.id = 2345;
        dummyArticles.add(d2);

        Article d3 = new Article();
        d3.description = "this is dummy3";
        d3.title = "dummy3";
        d3.shopPricePerUnitNetCent = 2500;
        d3.id = 3456;
        dummyArticles.add(d3);

        Article d4 = new Article();
        d4.description = "this is dummy4";
        d4.title = "dummy4";
        d4.shopPricePerUnitNetCent = 3000;
        d4.id = 4567;
        dummyArticles.add(d4);

        return dummyArticles;
    }


}
