 fuzzaldrin = require "fuzzaldrin"
 XRegExp = require('xregexp').XRegExp
 titlecaps = require "./titlecaps"
-
+yaml = require "yaml-js"
 
 module.exports =
-class BibtexProvider
+class ReferenceProvider
   ###
   For a while, I intended to continue using XRegExp with this `wordRegex`:
 

  bibtex = []

  atom.deserializers.add(this)
  @deserialize: ({data}) -> new BibtexProvider(data)

  constructor: (state) ->
    if state and Object.keys(state).length != 0
      @bibtex = state.bibtex
      @possibleWords = state.possibleWords
    else
      @buildWordListFromFiles(atom.config.get "autocomplete-bibtex.bibtex")

    if @bibtex.length == 0
      @buildWordListFromFiles(atom.config.get "autocomplete-bibtex.bibtex")

    atom.config.onDidChange "autocomplete-bibtex.bibtex", (bibtexFiles) =>
      @buildWordListFromFiles(bibtexFiles)

    resultTemplate = atom.config.get "autocomplete-bibtex.resultTemplate"
    atom.config.observe "autocomplete-bibtex.resultTemplate", (resultTemplate) =>


    @possibleWords = possibleWords

  buildWordListFromFiles: (referenceFiles) =>
    @readReferenceFiles(referenceFiles)
    @buildWordList()

  readReferenceFiles: (referenceFiles) =>
    # Make sure our list of files is an array, even if it's only one file
    if not Array.isArray(referenceFiles)
      referenceFiles = [referenceFiles]
    try
      references = []
      for file in referenceFiles
        # What type of file is this?
        console.warn("'#{file}'")
        ftype = file.split('.')
        console.warn("'#{ftype}'")
        ftype = ftype[ftype.length - 1]
        console.warn("'#{ftype}'")
        if fs.statSync(file).isFile()
          if ftype is "bib"
            parser = new bibtexParse(fs.readFileSync(file, 'utf-8'))
            console.warn("#{parser.parse()}")
            references = references.concat parser.parse()
          else
            alert("SOMETHING ELSE")
        else
          console.warn("'#{file}' does not appear to be a file, so autocomplete-bibtex will not try to parse it.")

      @references = references
    catch error
      console.error error

