module EquinoxScript
  def draw_map_text(x, y, str)
    canvas = Bitmap.new($game_map.width, $game_map.height)
    canvas.draw_text(x, y, 16, 8, str)
  end
  
  # This entire section concerns the "fake console" visual for Equinox
  
  def self.add_console_line(str)
    $FAUX_CONSOLE.push str
    
    while $FAUX_CONSOLE.length >= 25
      $FAUX_CONSOLE.delete_at 0
    end
    
    $game_temp.equinox_canvas_changed = true
  end
  
  def self.replace_line(str)
    $FAUX_CONSOLE.pop
    $FAUX_CONSOLE.push str
  end
  
  def self.toggle_faux_console
    $game_switches[504] = !$game_switches[504]
    $game_temp.equinox_canvas_togglevis = true
  end
  
  def self.faux_console_normalbg
    $FAUX_CONSOLE_BG = Color.new(0, 0, 0, 192)
  end
  
  def self.faux_console_clearbg
    $FAUX_CONSOLE_BG = Color.new(0, 0, 0, 0)
  end
  
  # This entire section concerns custom file creation/handling, and tracking of a single file's contents
  
  TRACKED_FILE = "TrackedFile"
  
  def self.write_untracked_file(filename = "UntrackedFile", contents = "Unknown Contents")
    Dir.mkdir(Oneshot_UTF::DOCS_PATH) unless File.exists?(Oneshot_UTF::DOCS_PATH)
    File.open("#{Oneshot_UTF::DOCS_PATH}/" + filename + ".txt", "w") do |file|
      file.puts(contents.to_s)
    end
  end
  
  def self.write_tracked_file(contents = "Unknown Contents")
    Dir.mkdir(Oneshot_UTF::DOCS_PATH) unless File.exists?(Oneshot_UTF::DOCS_PATH)
    File.open("#{Oneshot_UTF::DOCS_PATH}/" + TRACKED_FILE + ".txt", "w") do |file|
      file.puts(contents.to_s)
    end
  end
  
  def self.check_tracked_file(key = "SomeSpecifiedString")
    if File.exists?(Oneshot_UTF::DOCS_PATH + "/" + TRACKED_FILE + ".txt")
      File.readlines("#{Oneshot_UTF::DOCS_PATH}/" + TRACKED_FILE + ".txt").each do |text|
        # The array accessor is there to trim the whitespace cuz I can't find a way to do so
        if text[0, key.length].downcase == key.downcase
          return true
        end
      end
    end
    return false
  end

  def self.dump_log(num)
    log_path = Oneshot_UTF::DOCS_PATH + "/My Games/Oneshot/EquinoxMachineLogs"
    Dir.mkdir(log_path) unless File.exists?(log_path)

    log_name = "unknown"
    log_content = "missing"
    case num
    when 1
      log_name = tr("TBA")
      log_content = tr("TBA")
    end

    File.open(log_path + "/" + log_name + ".txt", "w") do |file|
      file.puts(log_content)
    end
  end
  
end