#==============================================================================
# ** Window_MapTransfer
#==============================================================================----
#  Map trasnfer (Request from Cryro)
#==============================================================================
class Window_MapTransfer < Window_Selectable
  def loadMaps(parent)
    @maps = []

    # ? append maps to the menu unless their parent ID is not the given ID
    @mapinfos.each do |key, value|
      @maps << [value.name, key.to_i] if value.parent_id == parent
    end

    @item_max = @maps.size
    @row_max = @item_max
    @column_max = 1
    @index = 0

    # ? 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, normal_color)
    end
    self.z = 9998
  end

  def initialize
    super(0, 0, 480, 480)

    # ? Initialize main Data neded
    @mapinfos = load_data("Data/MapInfos.rxdata").sort_by { |key, value| value.order }.to_h
    @hasChildren = {}
    @mapinfos.each do |key, value|
      if value.parent_id != 0
        @hasChildren.store(value.parent_id, "why did I make this a hash?")
      end
    end
    @upper_id = []

    @depthColours = [
      Color.new(255, 64, 64, 190),
      Color.new(0, 224, 0, 190),
      Color.new(255, 255, 0, 190),
      Color.new(64, 64, 255, 190),
      Color.new(255, 64, 255, 190),
      Color.new(64, 255, 255, 190),
      Color.new(128, 128, 128, 190),
    ]

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

    # ? Load the Maps and Make the Menu
    loadMaps(0)
  end

  def getDepthColour
    depth = @upper_id.length
    return depth >= @depthColours.length ? @depthColours.last : @depthColours[depth]
  end

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

    # Get width of text and cap
    h = 32
    # Update item
    rect = Rect.new(32, h * index, self.width - 32, h)
    indicateRect = Rect.new(16, h * index, self.width - 128, h)
    self.contents.fill_rect(rect, Color.new(0, 0, 0, 0))
    if @hasChildren[@maps[index][1]].nil?
      self.contents.draw_text(rect, (@maps[index].to_s.gsub!(/\[|\]/, "")).gsub!(", ", " → "), 0)
    else
      self.contents.font.color = getDepthColour
      self.contents.draw_text(indicateRect, "*", 0)
      self.contents.font.color = color
      self.contents.draw_text(rect, (@maps[index].to_s.gsub!(/\[|\]/, "").gsub!(", ", " → ")), 0)
    end
  end

  def update
    super
    # Handle fade-in effect
    if @fade_in
      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_T) ||
       Input.trigger?(Input::KEY_Y)
      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::RIGHT)
      return $game_system.se_play($data_system.buzzer_se) if @hasChildren[@maps[@index][1]].nil? == true
      @upper_id.push(@mapinfos[@maps[@index][1]].parent_id)
      loadMaps(@maps[@index][1])
    end

    if Input.trigger?(Input::LEFT)
      return if @upper_id.length == 0
      loadMaps(@upper_id.pop)
    end

    if Input.trigger?(Input::ACTION)
      self.active = false
      self.opacity = 127

      $game_temp.player_new_map_id = @maps[@index][1]
      $game_temp.player_transferring = true
      $game_temp.player_new_x = load_data("Data/Map" + @maps[@index][1].to_s.rjust(3, "0") + ".rxdata").width / 2
      $game_temp.player_new_y = load_data("Data/Map" + @maps[@index][1].to_s.rjust(3, "0") + ".rxdata").height / 2
      @fade_out = true
      return
    end
  end

  #==============================================================================
  # * Open
  #==============================================================================
  def open
    @upper_id = []
    @maps = []
    @mapinfos.each do |key, value|
      @maps << [value.name, key.to_i] if value.parent_id == @upper_id
    end
    @item_max = @maps.size
    @row_max = @item_max
    @column_max = 1

    loadMaps(0)
    loadMaps(0)

    self.opacity = 0
    self.contents_opacity = 0
    self.visible = true
    @fade_in = true
    self.index = 0
    self.active = true
  end
end
