#==============================================================================
# ** Window_MapTransfer
#------------------------------------------------------------------------------
#  Map trasnfer (Request from Cryro)
#==============================================================================
class Window_SoundTest < Window_Selectable
  def initialize
    super(0, 0, Graphics.height, Graphics.height)

    @bgms = Dir.children("Audio/BGM")

    @item_max = @bgms.length
    @row_max = @item_max
    @column_max = 1
    @playing = -1

    # Make invisible by default
    self.visible = false
    self.active = false
    self.back_opacity = 230
    @fade_in = false
    @fade_out = false

    # Render menu
    self.contents = Bitmap.new(width - 32, @item_max * 32)
    Language.register_text_sprite(self.class.name + "_contents", self.contents)
    for i in 0...(@item_max)
      draw_item(i, Color.new(255, 255, 255, 255), 0)
      next if i != @playing
      draw_item(i, Color.new(255, 64, 255, 190), 1)
    end
    self.z = 9998
  end

  def draw_item(index, color, mode = 0)
    # Set color
    self.contents.font.color = color

    # Get width of text and cap
    h = 32
    # Update item
    rect = Rect.new(0, h * index, self.width - 32, h)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    self.contents.draw_text(rect, @bgms[index].gsub(/\.\w*\z/, ""), mode)
  end

  def update
    super

    # Handle fade-in effect
    if @fade_in == true
      self.opacity += 48
      self.contents_opacity += 48
      if self.contents_opacity == 255
        @fade_in = false
      end
      return
    end

    # Handle fade-out effect
    if @fade_out
      self.opacity -= 48
      self.contents_opacity -= 48
      if self.contents_opacity == 0
        @fade_out = false
        self.visible = false
        self.active = false
      end
      return
    end

    # Don't do anything if not active
    if !self.active || $game_system.map_interpreter.running?
      return
    end

    # Cancel menu
    if Input.trigger?(Input::CANCEL) ||
       Input.trigger?(Input::MENU) ||
       Input.trigger?(Input::ITEMS) ||
       Input.trigger?(Input::L) ||
       Input.trigger?(Input::KEY_Y) ||
       Input.trigger?(Input::KEY_T)
      unless Input.trigger?(Input::ITEMS) || Input.trigger?(Input::L)
        $game_system.se_play($data_system.cancel_se)
      end
      @fade_out = true
      return
    end
    # Select menu item
    if Input.trigger?(Input::ACTION)
      $game_system.se_play($data_system.decision_se)
      @playing = @index
      for i in 0...(@item_max)
        draw_item(i, Color.new(255, 255, 255, 255), 0)
        next if i != @playing
        draw_item(i, Color.new(255, 64, 255, 190), 1)
      end
      Audio.bgm_play("Audio/BGM/#{@bgms[@index]}")
      return
    end
  end

  #--------------------------------------------------------------------------
  # * Open
  #--------------------------------------------------------------------------
  def open
    self.index = @playing == -1 ? 0 : @playing
    # redraw in case language has been updated
    for i in 0...(@item_max)
      draw_item(i, Color.new(255, 255, 255, 255), 0)
      next if i != @playing
      draw_item(i, Color.new(255, 64, 255, 190), 1)
    end
    self.opacity = 0
    self.contents_opacity = 0
    self.visible = true
    @fade_in = true
    self.active = true
  end
end
