class 'GameInfo' (GlobalScript)

function GameInfo:__init() super()
  self.prev_info = ''
  self.prev_scene_name = ''
  self.show_hide_time = 1

  self.text_max_width = 700
  self.font_id = 'Trajan'
  self.text_color = Color(255, 255, 255)
  self.line_offset = 0
  self.text_pos = Position(298, 668)
  self.text_dim = Dimensions(700, 100)

  self.back_id = 'interface/game_info_back'
  self.back_wide_id = 'interface/game_info_back_wide'
  self.back_position = Position(228, 641)
  self.back_wide_position = Position(100, 641)

  self.title_text_key = 'root/screens/game/@game_info'
  self.title_font_id = 'Trajan01'
  self.title_color = Color(255, 255, 255)
  self.title_pos_wide = Position(502, 638)
  self.title_pos = Position(514, 638)
  self.title_dim = Dimensions(280, 30)
end

function GameInfo:on_attach()
  self:listen_event('make_app_window')
end

function GameInfo:on_event(event)
  if(event == 'make_app_window') then
    if(self.back_renderable ~= nil) then
      self:add_back_renderable()
      self.back_renderable:set_alpha(1)
      if(self.inv ~= nil) then
        self:check_controllers(true)
        self.inv = self:inventory_control()
        if(self.back_renderable ~= nil) then
          self.text_renderable:set_alpha(1)
          self.title_renderable:set_alpha(1)
          self.inv:hide()
        end
      end
    end
  end
end

function GameInfo:on_update(dt)
  self:check_controllers(self:instant_effect())

  local game_info = self:game():game_info()
  if(self.prev_info ~= game_info) then
    if(self.inv_renderable == nil and self.show_text_controller == nil and self.hide_text_controller == nil) then
      self.prev_info = game_info

      if(self.inv == nil or not self.inv:valid()) then
        self.inv = self:inventory_control()
      end

      -- show / hide inventory
      if(self.inv ~= nil) then
        if(game_info == '') then
          if(self.inv ~= nil and not self.inv:visible()) then
            self.inv_renderable = self:add_renderable(self.inv)
            self.inv_renderable:set_position(self.inv:position())
            self.inv_renderable:set_above_interface(true)
            self.inv_renderable:set_alpha(0)
            self.show_controller = self:create_controller(self.inv_renderable)
            local time = self.show_hide_time
            if(self:instant_effect()) then time = 0 end
            self.show_controller:alpha(1, time)
          end
        else
          if(self.inv ~= nil and self.inv:visible()) then
            self.inv_renderable = self:add_renderable(self.inv)
            self.inv_renderable:set_position(self.inv:position())
            self.inv_renderable:set_above_interface(true)
            self.inv_renderable:set_alpha(1)
            self.hide_controller = self:create_controller(self.inv_renderable)
            local time = self.show_hide_time
            if(self:instant_effect()) then time = 0 end
            self.hide_controller:alpha(0, time)
            self.inv:hide()
          end
        end
      end

      -- show / hide back
      if(game_info == '') then
        if(self.back_renderable ~= nil) then
          self.hide_back_controller = self:create_controller(self.back_renderable)
          local time = self.show_hide_time
          if(self:instant_effect()) then time = 0 end
          self.hide_back_controller:alpha(0, time)
        end
      else
        self:add_back_renderable()
        self.back_renderable:set_alpha(0)

        self.show_back_controller = self:create_controller(self.back_renderable)
        local time = self.show_hide_time
        if(self:instant_effect()) then time = 0 end
        self.show_back_controller:alpha(1, time)
      end

      -- show / hide game_info text
      if(game_info == '') then
        if(self.text_renderable ~= nil) then
          self.hide_text_controller = self:create_controller(self.text_renderable)
          self.hide_title_controller = self:create_controller(self.title_renderable)
          local time = self.show_hide_time
          if(self:instant_effect()) then time = 0 end
          self.hide_text_controller:alpha(0, time)
          self.hide_title_controller:alpha(0, time)
        end
      else
        if(self.text_renderable ~= nil) then
          self.text_renderable:remove()
          self.text_renderable = nil
          self.title_renderable:remove()
          self.title_renderable = nil
        end
        self.text_renderable = self:add_text_renderable()
        --self.text_renderable:set_above_interface(true)
        self.text_renderable:set_text_key(game_info)
        self.text_renderable:set_max_width(self.text_max_width)
        self.text_renderable:set_lines_h_align('center')
        self.text_renderable:set_h_align('center')
        self.text_renderable:set_v_align('center')
        self.text_renderable:set_font(self.font_id)
        self.text_renderable:set_color(self.text_color:red(), self.text_color:green(), self.text_color:blue())
        self.text_renderable:set_line_offset(self.line_offset)
        self.text_renderable:set_dimensions(self.text_dim)
        self.text_renderable:set_position(self.text_pos)
        self.text_renderable:set_alpha(0)

        self.title_renderable = self:add_text_renderable()
        --self.title_renderable:set_above_interface(true)
        self.title_renderable:set_text_key(self.title_text_key)
        self.title_renderable:set_h_align('center')
        self.title_renderable:set_v_align('center')
        self.title_renderable:set_font(self.title_font_id)
        self.title_renderable:set_color(self.title_color:red(), self.title_color:green(), self.title_color:blue())
        self.title_renderable:set_dimensions(self.title_dim)
        self.title_renderable:set_position(self:title_position())
        self.title_renderable:set_alpha(0)

        self.show_text_controller = self:create_controller(self.text_renderable)
        local time = self.show_hide_time
        if(self:instant_effect()) then time = 0 end
        self.show_text_controller:alpha(1, time)

        self.show_title_controller = self:create_controller(self.title_renderable)
        self.show_title_controller:alpha(1, time)
      end
    end
  end

  self.prev_scene_name = self:game():current_scene():name()
end

function GameInfo:check_controllers(instant)
  if(instant == nil) then instant = false end

  if(self.hide_controller ~= nil) then
    if(instant or self.hide_controller:time() >= self.hide_controller:total_time()) then
      self.hide_controller:remove()
      self.hide_controller = nil
      self.inv_renderable:remove()
      self.inv_renderable = nil
    end
  end

  if(self.show_controller ~= nil) then
    if(instant or self.show_controller:time() >= self.show_controller:total_time()) then
      self.show_controller:remove()
      self.show_controller = nil
      self.inv_renderable:remove()
      self.inv_renderable = nil
      self.inv:show()
    end
  end

  if(self.show_back_controller ~= nil) then
    if(instant or self.show_back_controller:time() >= self.show_back_controller:total_time()) then
      self.show_back_controller:remove()
      self.show_back_controller = nil
    end
  end

  if(self.hide_back_controller ~= nil) then
    if(instant or self.hide_back_controller:time() >= self.hide_back_controller:total_time()) then
      self.hide_back_controller:remove()
      self.hide_back_controller = nil
      self.back_renderable:remove()
      self.back_renderable = nil
    end
  end

  if(self.show_text_controller ~= nil) then
    if(instant or self.show_text_controller:time() >= self.show_text_controller:total_time()) then
      self.show_text_controller:remove()
      self.show_text_controller = nil
      self.show_title_controller:remove()
      self.show_title_controller = nil
    end
  end

  if(self.hide_text_controller ~= nil) then
    if(instant or self.hide_text_controller:time() >= self.hide_text_controller:total_time()) then
      self.hide_text_controller:remove()
      self.hide_text_controller = nil
      self.text_renderable:remove()
      self.text_renderable = nil

      self.hide_title_controller:remove()
      self.hide_title_controller = nil
      self.title_renderable:remove()
      self.title_renderable = nil
    end
  end
end

function GameInfo:instant_effect()
  return self.prev_scene_name == '' or self.prev_scene_name ~= self:game():current_scene():name()
end

function GameInfo:add_back_renderable()
  if(self.back_renderable ~= nil) then
    self.back_renderable:remove()
    self.back_renderable = nil
  end
  self.back_renderable = self:add_renderable(self:background_id())
  self.back_renderable:set_position(self:background_position())
end

function GameInfo:background_id()
  if(wide_screen_mode()) then
    return self.back_wide_id
  else
    return self.back_id
  end
end

function GameInfo:background_position()
  if(wide_screen_mode()) then
    return self.back_wide_position
  else
    return self.back_position
  end
end

function GameInfo:title_position()
  if(wide_screen_mode()) then
    return self.title_pos_wide
  else
    return self.title_pos
  end
end

function GameInfo:inventory_control()
  return self:game():control('inventory')
end