/*
 * 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 managedbeans;

import com.sun.istack.internal.logging.Logger;
import entities.Customer;
import entities.DiscountCode;
import entities.MicroMarket;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import session.CustomerManager;
import session.DiscountCodeManager;
import session.MicroMarketManager;

/**
 *
 * @author Hoss
 */
@ManagedBean
@ViewScoped
public class CustomerDetailsMBean implements Serializable {

    private int idCustomer;
    private Customer customer;
    @EJB
    private CustomerManager customerManager;
    @EJB
    private DiscountCodeManager discountCodeManager;
    @EJB
    private MicroMarketManager microMarketManager;
    
     private Converter discountCodeConverter = new Converter() {

        @Override
         public Object getAsObject(FacesContext context, UIComponent component, String value) {
         return new ConverterException("On verra la conversion String-Objet plus tard...");
         }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            DiscountCode dc = (DiscountCode) value;
            return dc.getDiscountCode()+" : "+dc.getRate()+"%";
        }
    };

    /**
     * Creates a new instance of CustomerDetailsMBean
     */
    public CustomerDetailsMBean() {
    }

    public int getIdCustomer() {
        return idCustomer;
    }

    public void setIdCustomer(int idCustomer) {
        this.idCustomer = idCustomer;
    }

    public Customer getDetails() {
        return customer;
    }


    public String update() {
        customer = customerManager.update(customer);
        return "CustomerList";
    }

   
    public String list() {
         Logger log=Logger.getLogger(CustomerDetailsMBean.class);
        log.info("Listinnng");
        return "CustomerList";
    }
    
    public void test(){
        Logger log=Logger.getLogger(CustomerDetailsMBean.class);
        log.info("Oupss");
    }
    public void loadCustomer() {
        this.customer = customerManager.getCustomer(idCustomer);
    }
    
    public Converter getDiscountCodeConverter() {
        return discountCodeConverter;
    }
    /**
 * renvoie une liste de DiscountCode pour affichage dans le menu déroulant
 * . * de la page des détails d'un client
 * @return
 */
        public List<DiscountCode> getAllDiscountCodes() {
            return discountCodeManager.getDiscountCodes();
        }
        public List<MicroMarket> getAllMicroMarket(){
            return microMarketManager.getMicroMarkets();
        }

}


