/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package plate.wellmann.shop.business;

import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import plate.wellmann.shop.persistence.Color;
import plate.wellmann.shop.persistence.Customer;
import plate.wellmann.shop.persistence.Item;
import plate.wellmann.shop.persistence.Paper;

/**
 *
 * @author Patrick
 */
@Named(value = "shoppingcartManager")
@SessionScoped
public class ShoppingcartManager implements Serializable {

    @PersistenceContext(name = "MySQL-Wellmann")
    private EntityManager em;

    private List<Item> items;
    private HashMap<Integer, Integer> quantity;

    @Inject
    private Customer activeCustomer;

    /**
     * Creates a new instance of ShoppingcartManager
     */
    public ShoppingcartManager() {
        quantity = new HashMap<>();
    }

    public void addItem(Item item, Integer quantity) {
        if (items == null) {
            items = new ArrayList<>();
        }
        if (this.quantity == null) {
            this.quantity = new HashMap<>();
        }
        this.items.add(item);
        this.quantity.put(item.hashCode(), quantity);
    }

    public boolean deleteItem(Long id) {
        for (Item next : items) {
            if (Objects.equals(next.getId(), id)) {
                if (items.remove(next)) {
                    this.quantity.remove(next.hashCode());
                    return true;
                }
            }
        }

        return false;
    }

    public boolean setQuantity(Item item, Integer quantity) {
        if (items.contains(item)) {
            this.quantity.replace(item.hashCode(), quantity);
            return true;
        }
        return false;
    }

    @Transactional
    public boolean demo() {
        Item item = new Item(LocalDateTime.now());
        item.setCreator(activeCustomer);
        item.setName("Visitenkarten");
        item.setPrice(new BigInteger("53199"));

        List<Color> colors = new ArrayList<Color>();
        Color c = new Color();
        c.setColorRoom(Color.ColorRoom.SKALA);
        c.setName("Schwarz");
        c.setColorType(Color.ColorType.K);
        c.setHtmlDotStyle("height:20px;width: 20px; background-color: #000000;border-radius: 50%;display: inline-block;");
        colors.add(c);

        Color co = new Color();
        co.setColorRoom(Color.ColorRoom.SKALA);
        co.setName("Magenta");
        co.setColorType(Color.ColorType.K);
        co.setHtmlDotStyle("height:20px;width: 20px; background-color: #ff00ff;border-radius: 50%;display: inline-block;");
        colors.add(co);

        co = new Color();
        co.setColorRoom(Color.ColorRoom.SKALA);
        co.setName("Cyan");
        co.setColorType(Color.ColorType.K);
        co.setHtmlDotStyle("height:20px;width: 20px; background-color: #00FFFF;border-radius: 50%;display: inline-block;");
        colors.add(co);

        co = new Color();
        co.setColorRoom(Color.ColorRoom.SKALA);
        co.setName("Yellow");
        co.setColorType(Color.ColorType.K);
        co.setHtmlDotStyle("height:20px;width: 20px; background-color: #FFFF00;border-radius: 50%;display: inline-block;");
        colors.add(co);

        item.setColors(colors);

        Paper paper = new Paper();
        paper.setFormat("A3");
        paper.setName("Papiername1");
        paper.setSort("HeißesWeißes");
        item.setPaper(paper);
        item.setQuantity(new BigInteger("500"));

        
        try {
            em.persist(item);
        } catch (Exception e) {
            System.out.println("plate.wellmann.shop.business.ShoppingcartManager.demo() " + e);
        }
        addItem(item, 600);

        item = new Item(LocalDateTime.now());
        item.setCreator(activeCustomer);
        item.setName("Brackepappe");
        item.setPrice(new BigInteger("448745"));

        colors = new ArrayList<Color>();
        c = new Color();
        c.setColorRoom(Color.ColorRoom.SKALA);
        c.setName("Schwarz");
        c.setColorType(Color.ColorType.K);
        c.setHtmlDotStyle("height:20px;width: 20px; background-color: #000000;border-radius: 50%;display: inline-block;");
        colors.add(c);

        co = new Color();
        co.setColorRoom(Color.ColorRoom.SKALA);
        co.setName("Cyan");
        co.setColorType(Color.ColorType.K);
        co.setHtmlDotStyle("height:20px;width: 20px; background-color: #00FFFF;border-radius: 50%;display: inline-block;");
        colors.add(co);

        item.setColors(colors);

        paper = new Paper();
        paper.setFormat("A3");
        paper.setName("Altpapier");
        paper.setSort("HeißesBraunes");
        item.setPaper(paper);
        item.setQuantity(new BigInteger("452"));

        addItem(item, 1231);
        try {
            em.persist(item);
            em.flush();
        } catch (Exception e) {
            System.out.println("plate.wellmann.shop.business.ShoppingcartManager.demo() " + e);
        }
        
        return true;
    }

    public List<Item> getItems() {
        return items;
    }

    public Integer getQuantity(Item item) {
        return quantity.get(item.hashCode());
    }

}
