/*
 * 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.controller;

import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.log4j.Logger;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.SlideEndEvent;
import plate.wellmann.shop.persistence.Color;
import plate.wellmann.shop.persistence.Format;
import plate.wellmann.shop.persistence.Item;
import plate.wellmann.shop.persistence.Paper;

/**
 *
 * @author Patrick
 */
@Named(value = "productController")
@ViewScoped
public class ProductController implements Serializable{
    final static Logger logger = Logger.getLogger(ProductController.class);
    
    private Item item;
    private int size;
    private String variations;
    private List<Price> prices;
    private Price selectedPrice;
    
    public class Price {
        private final int id;
        private final BigInteger price;
        private final int elements;

        public Price(int id, BigInteger price, int elements) {
            this.id = id;
            this.price = price;
            this.elements = elements;
        }

        public int getId() {
            return id;
        }

        public BigInteger getPrice() {
            return price;
        }

        public int getElements() {
            return elements;
        }
        
        public String euros(){
            return price.toString().substring(0, price.toString().length()-2);
        }
        
        public String cents(){
            return price.toString().substring(price.toString().length()-2);
        }
    }
    
    @Inject
    private ShoppingcartController shoppingcartController;
    
    private String formatString;

    public ProductController() {
        this.item = new Item(LocalDateTime.now());
        this.item.setDatacheck("Basis-Datencheck");
        this.item.setRoundedCorners(false);
        this.item.setNumberAdresses(1);
    }
    
    public String onFlowProcess(FlowEvent event) {
        return event.getNewStep();
    }
    
    public String toCart(){
        info(item.getName() + " wurde dem Warenkorb hiznugefügt!");
        
        shoppingcartController.addItem(item, size);
        return "/resources/manager/shoppingcart.xhtml";
    }
    
    public void addFormat(String x, String y){
        item.setFormat(new Format(new BigInteger(x), new BigInteger(y)));
        logger.debug("added Format "+ item.getFormat().getX() + " x " + item.getFormat().getY());
        createFormatString();
    }
    
    public void createFormatString(){
        String x = item.getFormat().getX().toString();
        String y = item.getFormat().getY().toString();
        
        this.formatString = x.substring(0, x.length()-2)+ ","+ x.substring(x.length()-2) + "cm x "
                + y.substring(0, y.length()-2) + ","+ y.substring(y.length()-2) + "cm";
    }
    
    public void addPaper(String name){
        Paper paper = new Paper();
        paper.setFormat("A3");
        paper.setName(name);
        paper.setSort("HeißesWeißes");
        item.setPaper(paper);
    }
    
    public void addFinishing (String finishing){
        item.setFinishing(finishing);
    }
    
    public void addColor(int id){
        switch(id){
            case 0: 
                item.setColors(cmyk());
                break;
            default:
        }
    }
    
    public void createPricesV(){
        logger.debug("started Pricegenerator");
        prices = new ArrayList<>();
        BigInteger var = new BigInteger(variations);
        
        int i = 1;
        
        BigInteger nr = new BigInteger("5043").multiply(var);
        Price price = new Price(i, nr, 100);
        prices.add(price);
        i++;
        
        nr = new BigInteger("8064").multiply(var);
        Price price2 = new Price(i, nr, 250);
        prices.add(price2);
        i++;
        
        nr = new BigInteger("11022").multiply(var);
        Price price3 = new Price(i, nr, 1000);
        prices.add(price3);
        i++;
        
        nr = new BigInteger("12011").multiply(var);
        Price price4 = new Price(i, nr, 2500);
        prices.add(price4);
        i++;
        
        nr = new BigInteger("13522").multiply(var);
        Price price5 = new Price(i, nr, 5000);
        prices.add(price5);
        
        selectedPrice = prices.get(0);
    }
    
    private List<Color> cmyk(){
        List<Color> 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);

        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);
        
        return colors;
    }
    
    private void error(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Fehler", message));

    }

    private void info(String message) {
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Erfolg", message));
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public String getFormatString() {
        return formatString;
    }
    
    public String paperString(){
        return item.getPaper().getName();
    }
    
    public String finishingString(){
        return item.getFinishing();
    }

    public String getVariations() {
        return variations;
    }

    public void setVariations(String variations) {
        this.variations = variations;
    }

    public List<Price> getPrices() {
        return prices;
    }

    public Price getSelectedPrice() {
        return selectedPrice;
    }

    public void setSelectedPrice(Price selectedPrice) {
        this.selectedPrice = selectedPrice;
    }
    
    public String getDatacheck(){
        return item.getDatacheck();
    }
    
    public void setDatacheck(String datacheck){
        item.setDatacheck(datacheck);
    }
    
    public boolean isRoundedCorners(){
        return item.isRoundedCorners();
    }
    
    public void setRoundedCorners(boolean roundedCorners){
        item.setRoundedCorners(roundedCorners);
    }
    
    public int getNumberAdresses(){
        return item.getNumberAdresses();
    }
    
    public void setNumberAdresses(int numberAdresses){
        item.setNumberAdresses(numberAdresses);
    }
}
