package fr.umlv.td11;

import java.util.concurrent.locks.ReentrantLock;

public class PermissiveLock {
  private int version;
  private long lastUpdate = System.nanoTime();
  private final ReentrantLock myLock = new ReentrantLock();

  public int getVersion() {
    myLock.lock();
    try {
      return version;
    } finally {
      myLock.unlock();
    }
  }

  public void setVersion(int version) {
    myLock.lock();
    try {
      this.version = version;
    } finally {
      myLock.unlock();
    }
  }

  public long getLastUpdate() {
    myLock.lock();
    try {
      return lastUpdate;
    } finally {
      myLock.unlock();
    }
  }

  public void setLastUpdate(long lastUpdate) {
    myLock.lock();
    try {
      this.lastUpdate = lastUpdate;
    } finally {
      myLock.unlock();
    }
  }

  public static void main(String[] args) {
    final PermissiveLock pl = new PermissiveLock();
    for (int i = 0; i < 10; i++) {
      final int id = i;
      new Thread(() -> {
        for(;;) {
          System.out.println(
              "lecteur " + id + " webcam_" + pl.getVersion()
              + ".jpg " + "(" + pl.getLastUpdate() + ")");
        }
      }).start();
    }

    new Thread(() -> {
      for(;;) {
        long cur_time = System.nanoTime();
        if (cur_time > (pl.getLastUpdate() + 1000000000L)){
          pl.setVersion(pl.getVersion() + 1);
          pl.setLastUpdate(cur_time);
        }
      }
    }).start();
  }
}
