package WheelOfFortune.webMonitor;

import WheelOfFortune.storage.*;
import java.util.*;
import java.io.*;
import com.enterprisedt.net.ftp.*;

public class WebMonitor {
 private static final String tempLocalDir = ".\\temp\\";
 private static final String remoteFinalDir = "./final/";
 private static final String remotePendingDir = "./pending/";

 public static final int NOT_CONNECTED = 1;
 public static final int IDLE = 2;
 public static final int DOWNLOADING = 3;

 WebMonitorOutput out;
 private int state;
 private FTPClient ftpc;

 String[] remoteFileNames;
 int viewIndex;

 public WebMonitor(WebMonitorOutput out) throws Exception {
  this.out = out;
  state = NOT_CONNECTED;
  ftpc = null;
  remoteFileNames = null;
  viewIndex = -1;
  out.setState(state);
 }

 public void connect(String remoteLocation, int remotePort, String userName, String password) {
  try {
  ftpc = new FTPClient(remoteLocation, remotePort);
  ftpc.login(userName, password);
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) {
   state = IDLE;
   refreshRemoteList();
  }
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 public void disconnect() {
  try {
   ftpc.quit();
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 //Filter these lists so only files with the correct extension are listed
 public void refreshRemoteList() {
  if(state == NOT_CONNECTED) {
  out.printErrorMessage("You must connect to a server first.");
  return;
  }

  try {
  state = DOWNLOADING;
  out.setState(state);
  remoteFileNames = ftpc.dir(remotePendingDir);
  out.setRemoteList(remoteFileNames);
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 public void viewFile(int i) {
  if(state == NOT_CONNECTED) {
   out.printErrorMessage("You must connect to a server first.");
   return;
  }

  if(viewIndex == -1) {
  }
  try {
   state = DOWNLOADING;
   out.setState(state);
   ftpc.get(tempLocalDir + remoteFileNames[i],remotePendingDir + remoteFileNames[i]);
   String text = Utilities.fileToString(tempLocalDir + remoteFileNames[i]);
   out.setViewAreaText(text);
   viewIndex = i;
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 public void approve() {
  if(state == NOT_CONNECTED) {
  out.printErrorMessage("You must connect to a server first.");
  return;
  }

  try {
   ftpc.rename(remotePendingDir + remoteFileNames[viewIndex],remoteFinalDir + remoteFileNames[viewIndex]);
   refreshRemoteList();
   out.setViewAreaText("");
   viewIndex = -1;
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 public void reject() {
  if(state == NOT_CONNECTED) {
  out.printErrorMessage("You must connect to a server first.");
  return;
  }

  try {
   ftpc.delete(remotePendingDir + remoteFileNames[viewIndex]);
   refreshRemoteList();
   out.setViewAreaText("");
   viewIndex = -1;
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

 public boolean connected() {
  try {
   ftpc.pwd();
  }
  catch(IOException io) {
   return false;
  }
  catch(FTPException f) {
   return false;
  }
  catch(NullPointerException npe) {
   return false;
  }
  return true;
 }
}