package net.sourceforge.phpeclipse.builder;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
import net.sourceforge.phpdt.core.compiler.InvalidInputException;
import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
import net.sourceforge.phpdt.internal.compiler.parser.SyntaxError;
import net.sourceforge.phpeclipse.mover.obfuscator.PHPIdentifier;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

/**
 * Manages the identifer index information for a specific project
 *
 */
public class IdentifierIndexManager {

  public class LineCreator implements ITerminalSymbols {

    private Scanner fScanner;
    private int fToken;

    public LineCreator() {
      fScanner = new Scanner(true, false);
    }

    /**
     * Add the information of the current identifier to the line
     * 
     * @param typeOfIdentifier the type of the identifier ('c'lass, 'd'efine, 'f'unction, 'm'ethod, 'v'ariable)
     * @param identifier current identifier
     * @param line Buffer for the current index line
     * @param phpdocOffset the offset of the PHPdoc comment if available
     * @param phpdocLength the length of the PHPdoc comment if available
     */
    private void addIdentifierInformation(
      char typeOfIdentifier,
      char[] identifier,
      StringBuffer line,
      int phpdocOffset,
      int phpdocLength) {

      line.append('\t');
      line.append(typeOfIdentifier);
      line.append(identifier);
      line.append("\to"); // Offset 
      line.append(fScanner.getCurrentTokenStartPosition());
      if (phpdocOffset >= 0) {
        line.append("\tp"); // phpdoc offset
        line.append(phpdocOffset);
        line.append("\tl"); // phpdoc length
        line.append(phpdocLength);
      }

    }
    /**
     * Get the next token from input
     */
    private void getNextToken() {
      try {
        fToken = fScanner.getNextToken();
        if (Scanner.DEBUG) {
          int currentEndPosition = fScanner.getCurrentTokenEndPosition();
          int currentStartPosition = fScanner.getCurrentTokenStartPosition();

          System.out.print(currentStartPosition + "," + currentEndPosition + ": ");
          System.out.println(fScanner.toStringAction(fToken));
        }
        return;
      } catch (InvalidInputException e) {
        // ignore errors
      }
      fToken = TokenNameERROR;
    }

    private void parseDeclarations(StringBuffer buf, boolean goBack) {
      char[] ident;
			char[] classVariable;
      int counter = 0;
      int phpdocOffset = -1;
      int phpdocLength = -1;

      try {
        while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
          phpdocOffset = -1;
          if (fToken == TokenNameCOMMENT_PHPDOC) {
            phpdocOffset = fScanner.getCurrentTokenStartPosition();
            phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
            getNextToken();
            if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
              break;
            }
          }
          if (fToken == TokenNamevar) {
            getNextToken();
            if (fToken == TokenNameVariable) {
              ident = fScanner.getCurrentIdentifierSource();
							classVariable = new char[ident.length-1];
							System.arraycopy(ident, 1, classVariable, 0, ident.length-1);
              addIdentifierInformation('v', classVariable, buf, phpdocOffset, phpdocLength);
              getNextToken();
            }
          } else if (fToken == TokenNamefunction) {
            getNextToken();
            if (fToken == TokenNameAND) {
              getNextToken();
            }
            if (fToken == TokenNameIdentifier) {
              ident = fScanner.getCurrentIdentifierSource();
              addIdentifierInformation('m', ident, buf, phpdocOffset, phpdocLength);
              getNextToken();
              parseDeclarations(buf, true);
            }
          } else if (fToken == TokenNameclass) {
            getNextToken();
            if (fToken == TokenNameIdentifier) {
              ident = fScanner.getCurrentIdentifierSource();
              addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
              getNextToken();

              //skip tokens for classname, extends and others until we have the opening '{'
              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
                getNextToken();
              }
              parseDeclarations(buf, true);
            }
          } else if (fToken == TokenNamedefine) {
            getNextToken();
            if (fToken == TokenNameLPAREN) {
              getNextToken();
              if (fToken == TokenNameStringLiteral) {
                ident = fScanner.getCurrentStringLiteralSource();
                addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
                getNextToken();
              }
            }
          } else if ((fToken == TokenNameLBRACE) || (fToken == TokenNameDOLLAR_LBRACE)) {
            getNextToken();
            counter++;
          } else if (fToken == TokenNameRBRACE) {
            getNextToken();
            --counter;
            if (counter == 0 && goBack) {
              return;
            }
          } else {
            getNextToken();
          }
        }
      } catch (SyntaxError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    public void parseIdentifiers(char[] charArray, StringBuffer buf) {
      char[] ident;
      String identifier;
      int counter = 0;
      int phpdocOffset = -1;
      int phpdocLength = -1;

      fScanner.setSource(charArray);
      fScanner.setPHPMode(false);
      fToken = TokenNameEOF;
      getNextToken();

      try {
        while (fToken != TokenNameEOF && fToken != TokenNameERROR) {
          phpdocOffset = -1;
          if (fToken == TokenNameCOMMENT_PHPDOC) {
            phpdocOffset = fScanner.getCurrentTokenStartPosition();
            phpdocLength = fScanner.getCurrentTokenEndPosition() - fScanner.getCurrentTokenStartPosition() + 1;
            getNextToken();
            if (fToken == TokenNameEOF || fToken == TokenNameERROR) {
              break;
            }
          }
          if (fToken == TokenNamefunction) {
            getNextToken();
            if (fToken == TokenNameAND) {
              getNextToken();
            }
            if (fToken == TokenNameIdentifier) {
              ident = fScanner.getCurrentIdentifierSource();
              addIdentifierInformation('f', ident, buf, phpdocOffset, phpdocLength);
              getNextToken();
              parseDeclarations(buf, true);
            }
          } else if (fToken == TokenNameclass) {
            getNextToken();
            if (fToken == TokenNameIdentifier) {
              ident = fScanner.getCurrentIdentifierSource();
              addIdentifierInformation('c', ident, buf, phpdocOffset, phpdocLength);
              getNextToken();

              //skip fTokens for classname, extends and others until we have the opening '{'
              while (fToken != TokenNameLBRACE && fToken != TokenNameEOF && fToken != TokenNameERROR) {
                getNextToken();
              }

              parseDeclarations(buf, true);

            }
          } else if (fToken == TokenNamedefine) {
            getNextToken();
            if (fToken == TokenNameLPAREN) {
              getNextToken();
              if (fToken == TokenNameStringLiteral) {
                ident = fScanner.getCurrentStringLiteralSource();
                addIdentifierInformation('d', ident, buf, phpdocOffset, phpdocLength);
                getNextToken();
              }
            }
          } else {
            getNextToken();
          }
        }
      } catch (SyntaxError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }

  private HashMap fFileMap;
  private String fFilename;
  private HashMap fIndentifierMap;

  public IdentifierIndexManager(String filename) {
    fFilename = filename;
    initialize();
    readFile();
  }

  /**
   * Add the information for a given IFile resource
   *
   */
  public void addFile(IFile fileToParse) {
    InputStream iStream;
    LineCreator lineCreator = new LineCreator();
    try {
      iStream = fileToParse.getContents();

      StringBuffer buf = new StringBuffer();
      int c0;
      try {
        while ((c0 = iStream.read()) != (-1)) {
          buf.append((char) c0);
        }
      } catch (IOException e) {
        return;
      }

      StringBuffer lineBuffer = new StringBuffer();
      //      lineBuffer.append(fileToParse.getLocation().toString());
      lineBuffer.append(fileToParse.getFullPath().toString());
      int lineLength = lineBuffer.length();
      lineCreator.parseIdentifiers(buf.toString().toCharArray(), lineBuffer);
      if (lineLength != lineBuffer.length()) {
        addLine(lineBuffer.toString());
      }
    } catch (CoreException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
  }

  /**
   * Adds a line of the index file for function, class, class-method and class-variable names
   * 
   * @param line
   */
  private void addLine(String line) {
    StringTokenizer tokenizer;
    String phpFileName = null;
    String token;
    String identifier = null;
    String classname = null;
    String offset = null;
    PHPIdentifierLocation phpIdentifier = null;
    boolean tokenExists = false;

    tokenizer = new StringTokenizer(line, "\t");
    // first token contains the filename:
    if (tokenizer.hasMoreTokens()) {
      phpFileName = tokenizer.nextToken();
      //System.out.println(token);
    } else {
      return;
    }
    // all the other tokens are identifiers:
    while (tokenizer.hasMoreTokens()) {
      token = tokenizer.nextToken();
      //System.out.println(token);
      switch (token.charAt(0)) {
        case 'c' : // class name
          identifier = token.substring(1);
          classname = identifier;
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
          break;
        case 'd' : // define
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
          break;
        case 'f' : // function name
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
          break;
        case 'm' : //method inside a class
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
          break;
        case 'v' : // variable inside a class
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
          break;
        case 'o' : // offset information
          identifier = null;
          if (phpIdentifier != null) {
            offset = token.substring(1);
            phpIdentifier.setOffset(Integer.parseInt(offset));
          }
          break;
        case 'p' : // PHPdoc offset information
          identifier = null;
          if (phpIdentifier != null) {
            offset = token.substring(1);
            phpIdentifier.setPHPDocOffset(Integer.parseInt(offset));
          }
          break;
        case 'l' : // PHPdoc length information
          identifier = null;
          if (phpIdentifier != null) {
            offset = token.substring(1);
            phpIdentifier.setPHPDocLength(Integer.parseInt(offset));
          }
          break;
        default :
          identifier = null;
          phpIdentifier = null;
          classname = null;
      }
      if (identifier != null && phpIdentifier != null) {
        tokenExists = true;
        ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
        if (list == null) {
          list = new ArrayList();
          list.add(phpIdentifier);
          fIndentifierMap.put(identifier, list);
        } else {
          boolean flag = false;
          for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(phpIdentifier)) {
              flag = true;
              break;
            }
          }
          if (flag == false) {
            list.add(phpIdentifier);
          }
        }
      }
    }
    if (tokenExists) {
      fFileMap.put(phpFileName, line);
    }
  }

  /**
   * Change the information for a given IFile resource
   *
   */
  public void changeFile(IFile fileToParse) {
    removeFile(fileToParse);
    addFile(fileToParse);
  }

  /**
   * Get a list of all PHPIdentifierLocation object's associated with an identifier
   * 
   * @param identifier
   * @return
   */
  public List getLocations(String identifier) {
    return (List) fIndentifierMap.get(identifier);
  }

  /**
   * Initialize (i.e. clear) the current index information
   *
   */
  public void initialize() {
    fIndentifierMap = new HashMap();
    fFileMap = new HashMap();
  }

  private void readFile() {

    FileReader fileReader;
    try {
      fileReader = new FileReader(fFilename);

      BufferedReader bufferedReader = new BufferedReader(fileReader);

      String line;
      while (bufferedReader.ready()) {
        // all entries for one file are in a line
        // separated by tabs !
        line = bufferedReader.readLine();
        addLine(line);
      }

      fileReader.close();
    } catch (FileNotFoundException e) {
      // ignore this
      // TODO DialogBox which asks the user if she/he likes to build new index?
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

  }

  /**
   * Remove the information for a given IFile resource
   *
   */
  public void removeFile(IFile fileToParse) {
    //    String line = (String) fFileMap.get(fileToParse.getLocation().toString());
    String line = (String) fFileMap.get(fileToParse.getFullPath().toString());
    if (line != null) {
      removeLine(line);
    }
  }

  /**
   * Removes a line of the index file for function, class, class-method and class-variable names
   * 
   * @param line
   */
  private void removeLine(String line) {
    StringTokenizer tokenizer;
    String phpFileName = null;
    String token;
    String identifier = null;
    String classname = null;
    PHPIdentifier phpIdentifier = null;
    boolean tokenExists = false;

    tokenizer = new StringTokenizer(line, "\t");
    // first token contains the filename:
    if (tokenizer.hasMoreTokens()) {
      phpFileName = tokenizer.nextToken();
      //System.out.println(token);
    } else {
      return;
    }
    // all the other tokens are identifiers:
    while (tokenizer.hasMoreTokens()) {
      token = tokenizer.nextToken();
      //System.out.println(token);
      switch (token.charAt(0)) {
        case 'c' : // class name
          identifier = token.substring(1);
          classname = identifier;
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.CLASS, phpFileName);
          break;
        case 'd' : // define
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.DEFINE, phpFileName);
          break;
        case 'f' : // function name
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.FUNCTION, phpFileName);
          break;
        case 'm' : //method inside a class
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.METHOD, phpFileName, classname);
          break;
        case 'v' : // variable inside a class
          identifier = token.substring(1);
          phpIdentifier = new PHPIdentifierLocation(identifier, PHPIdentifier.VARIABLE, phpFileName, classname);
          break;
        default :
          identifier = null;
          phpIdentifier = null;
          classname = null;
      }
      if (identifier != null && phpIdentifier != null) {
        ArrayList list = (ArrayList) fIndentifierMap.get(identifier);
        if (list == null) {
        } else {
          for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(phpIdentifier)) {
              list.remove(i);
              break;
            }
          }
          if (list.size() == 0) {
            fIndentifierMap.remove(identifier);
          }
        }
      }
    }
    fFileMap.remove(phpFileName);
  }

  /**
   * Save the current index information in the projects index file
   *
   */
  public void writeFile() {
    FileWriter fileWriter;
    try {
      fileWriter = new FileWriter(fFilename);
      String line;
      Collection collection = fFileMap.values();
      Iterator iterator = collection.iterator();
      while (iterator.hasNext()) {
        line = (String) iterator.next();
        fileWriter.write(line + '\n');
      }
      fileWriter.close();
    } catch (FileNotFoundException e) {
    	// ignore exception; project is deleted by user
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}