
import java.net.*;
import java.io.*;
import java.util.*;

class Forest {
  public static void main(String[] args) throws Exception {
    int port = 12345;
    ServerSocket ss = new ServerSocket(port);

    final int[] feeder = new int[11];
    final int[] cap = new int[11];
    int index;
    int val;

    try {
      ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
              "forest.data"));
//If this file exists when the program is started,
//load the forest data from it instead of using feeders.txt
      System.out.println(
              "forest.data was opened sucessfully, restoring forest state");
      for (int i = 1; i <= 10; i++) {
        feeder[i] = ois.readInt(); // readInt is wrong, ToDo fix
        cap[i] = ois.readInt();
        System.out.println(
                "Restored " + feeder[i] + " food at capacity " + cap[i]);
      }

    } catch (IOException ex) {
      if (ex != null) {
        System.out.
                println("forest.data was not opened, using feeders.txt instead");
        Scanner sp = new Scanner(new File("feeders.txt"));
        for (int i = 1; i <= 10; i++) {
          cap[i] = sp.nextInt();
        }
      }
    }

//Using an ObjectOutputStream,
//save the state of the forest (the capacity and the current amount of food of the feeders)
//into a file called forest.data
//when 10 seconds have passed from the start of the program.
    new Thread("10sectimeout") {
      @Override
      public void run() {
        try {
          Thread.sleep(1 * 1000);
          ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
                  "forest.data"));
          for (int i = 1; i <= 10; i++) {
            oos.writeObject(feeder[i]);
            oos.writeObject(cap[i]);
          }
          oos.flush();
          oos.close();
          System.out.println("Successfully saved forest state.");
        } catch (IOException | InterruptedException ex) {
          System.out.println("Failed to save forest state.");
        }
      }
    }.start();


    while (true) {
      Socket s = ss.accept();
      Scanner sc = new Scanner(s.getInputStream());
      PrintWriter pw = new PrintWriter(s.getOutputStream());

      while (sc.hasNext()) {
        val = sc.nextInt();
        index = sc.nextInt();
        if (val > 0) {
          //Positive = refill (warden)
          if ((feeder[index] + val) > cap[index]) {
            feeder[index] = cap[index];
          } else {
            feeder[index] += val;
          }
        } else {
          //Negative - A deer ate food
          if ((feeder[index] + val) >= 0) {
            feeder[index] += val;
            pw.println("success");
          } else { //The deer tried to eat more than available
            feeder[index] = 0;
            pw.println("failure, deer is still hungry");
          }
          pw.flush();
        }

        for (int i = 1; i <= 10; i++) {
          if (feeder[i] > 0) {
            System.out.println("Feeder #" + i + " has: " + feeder[i]
                               + " food and its capacity is: " + cap[i]);
          }
        }
      }
    }
//    pw.close();
//    sc.close();
//    sp.close();
//    s.close();
  }
}