
package controller;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import model.Item;

@ManagedBean
@SessionScoped
public class Catalog implements Serializable {

    @EJB
    private ItemFacade itemFacade;
    private Item item = null;
    private List<Item> items = null;
    private PagingInfo pagingInfo = null;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
    private String country;
    private static final Locale[] countries = {Locale.CHINA, Locale.US};

    public Locale[] getCountries() {
        return countries;
    }

    /**
     * Creates a new instance of Catalog
     */
    public Catalog() {
        pagingInfo = new PagingInfo();
    }

    public PagingInfo getPagingInfo() {
        if (pagingInfo.getItemCount() == -1) {
            pagingInfo.setItemCount(getItemCount());
        }
        return pagingInfo;
    }

    public List<Item> getNextItems(int maxResults, int firstResult) {
        return itemFacade.findRange(maxResults, firstResult);
    }

    public List<Item> getItems() {
        if (items == null) {
            getPagingInfo();
            items = getNextItems(pagingInfo.getBatchSize(), pagingInfo.getFirstItem());
        }
        return items;
    }

    public Item getItem() {
        return item;

    }

    public String next() {
        reset(false);
        getPagingInfo().nextPage();
        return "list";
    }

    public String prev() {
        reset(false);
        getPagingInfo().previousPage();
        return "list";
    }

    public String showDetail(Item item) {
        this.item = item;
        return "detail";
    }

    public int getItemCount() {
        return itemFacade.getItemCount();
    }

    private void reset(boolean resetFirstItem) {
        item = null;
        items = null;
        pagingInfo.setItemCount(-1);
        if (resetFirstItem) {
            pagingInfo.setFirstItem(0);
        }
    }

    public void countryChanged(ValueChangeEvent event) {
        System.out.println("aaaaaa");
      for (Locale loc : countries){
         if (loc.getCountry().equals(event.getNewValue())){
            FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
      Locale.setDefault(loc);
      countries[0]= Locale.CHINA;
      countries[1]= Locale.US;
         }
      FacesContext.getCurrentInstance().renderResponse();
      }
   }

}
