package WheelOfFortune.puzzleManager;

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

public class PuzzleManager {
 private static final String localDir = ".\\puzzles\\";
 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;
 public static final int UPLOADING = 4;

 PuzzleManagerOutput out;
 private int state;
 private FTPClient ftpc;

 String[] localFileNames;
 String[] remoteFileNames;

 public PuzzleManager(PuzzleManagerOutput out) throws Exception {
  this.out = out;
  state = NOT_CONNECTED;
  ftpc = null;
  localFileNames = null;
  remoteFileNames = null;
  refreshLocalList();
  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 refreshLocalList() {
  File f = new File(localDir);
  localFileNames = f.list();
  out.setLocalList(localFileNames);
 }

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

  try {
  remoteFileNames = ftpc.dir(remoteFinalDir);
  out.setRemoteList(remoteFileNames);
  }
  catch(Exception e) { out.printErrorMessage("An error occurred"); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

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

  try {
  state = UPLOADING;
  out.setState(state);
  ftpc.put(localDir + localFileNames[i],remotePendingDir + localFileNames[i]);
  }
  catch(Exception e) { System.out.println(e); }
  if(connected()) state = IDLE;
  else state = NOT_CONNECTED;
  out.setState(state);
 }

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

  try {
  state = DOWNLOADING;
  out.setState(state);
  ftpc.get(localDir + remoteFileNames[i],remoteFinalDir + remoteFileNames[i]);
  refreshLocalList();
  }
  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;
 }
}