package javakurs.musterloesung.uebung06;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * {@link Fridge} is a FCM (fridge content management) system; it provides data
 * structure access and print methods for the fridge dialog system.
 *
 * @author Ulrich Schaefer, DFKI
 * @author Joerg Steffen, DFKI
 * @version $Id: Fridge.java,v 1.1 2010/12/06 18:18:46 steffen Exp $
 */
public class Fridge {

  /**
   * Contains the path where the inventory is stored.
   */
  private static String pfilename = "data/uebung06/inventory.xml";

  /**
   * Contains the fridge inventory.
   */
  private PropertiesIF inventory;


  /**
   * This creates a new instance of {@link Fridge} and loads the inventory.
   */
  public Fridge() {

    this.printWelcome();
    this.loadInventory(new SimpleProperties());
  }


  /**
   * This prints the welcome message.
   */
  protected void printWelcome() {

    System.out.println("  ______");
    System.out.println(" /_____/|");
    System.out.println("|     |\\");
    System.out.println("| O O | \\");
    System.out.println("|_____|  |");
    System.out.println("|\\___/|\\ |");
    System.out.println("|_____| \\|]");
    System.out.println("       \\ |");
    System.out.println("        \\|");
    System.out.println("         '");
    this.message("Hi, ich bin Dein K\u00fchlschrank!");
  }


  /**
   * This load the inventory into the given properties object.
   *
   * @param props the properties object to use
   */
  protected void loadInventory(PropertiesIF props) {

    try {
      this.message("Lese Datei " + pfilename);
      this.inventory = props;
      FileInputStream in = new FileInputStream(pfilename);
      props.loadFromXML(in);
    } catch (IOException ioe) {
      this.message("Huch! Ein Dateifehler ist aufgetreten:");
      System.exit(-1);
    }
  }


  /**
   * This returns the inventory.
   *
   * @return the inventory
   */
  public PropertiesIF getInventory() {

    return this.inventory;
  }


  /**
   * This shuts down the fridge and makes the current inventory persistent.
   */
  public void finish() {

    if (this.inventory != null && !this.inventory.isEmpty()) {
      try {
        System.out.println("Schreibe Datei " + pfilename);
        FileOutputStream propsfile = new FileOutputStream(pfilename);
        this.inventory.storeToXML(propsfile, "Fridge-Inventory");
        propsfile.flush();
        propsfile.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    this.message("Ende und tsch\u00fcss!");
    System.exit(-1);
  }


  /**
   * This prints the given message to stdout; will be extended later to speak
   * using a TTS system.
   *
   * @param message
   *          the message
   */
  public void message(String message) {

    System.out.println(message);
  }


  /**
   * This prints the current time.
   */
  public void printTime() {

    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    this.message("Es ist " + sdf.format(now) + " Uhr.");
  }


  /**
   * This lists the amount of the given product or the full inventory if given
   * product is {@code null}.
   *
   * @param product
   *          the product as token or {@code null}
   */
  public void listStock(Token product) {

    if (product == null) {
      this.inventory.list(System.out);
    }
    else {
      // TODO override in subclass to use product.image
    }
  }
}
