-- Title screen of the game.

local title_screen = {}

function title_screen:on_started()
  -- Black screen during 0.3 seconds.
  self.phase = "black"

  self.surface = sol.surface.create(416, 240)
  sol.timer.start(self, 300, function()
    self:phase_zs_presents()
  end)
end

function title_screen:phase_zs_presents()
  -- "Zelda Solarus presents" displayed for two seconds.
  self.phase = "zs_presents"
  local zs_presents_img = sol.surface.load("languages/" .. sol.language.get_language() .. "/images/title_screen_initialization.png")

  local width, height = zs_presents_img:get_size()
  local x, y = 208 - width / 2, 80 - height / 2
  zs_presents_img:draw(self.surface, x, y)
  sol.audio.play_sound("intro")

  sol.timer.start(self, 500, function()
    sol.timer.start(self, 700, function()
      self:phase_zh_production()
    end)
  end)
end

function title_screen:phase_zh_production()
  -- "A ZeldaHistorian Production" displayed for two seconds.
  self.phase = "zh_production"
  local zh_production_img = sol.surface.load("languages/" .. sol.language.get_language() .. "/images/title_screen_initialization_2.png")

  local width, height = zh_production_img:get_size()
  local x, y = 208 - width / 2, 160 - height / 2
  zh_production_img:draw(self.surface, x, y)
  sol.audio.play_sound("intro")

  sol.timer.start(self, 1000, function()
    self.surface:fade_out(10)
    sol.timer.start(self, 700, function()
      self:phase_title()
    end)
  end)
end

function title_screen:phase_title()
  -- Actual title screen.
  self.phase = "title"

  -- Start music.
  sol.audio.play_music("title_screen")

  -- Show a background that depends on the hour of the day.
  local hours = tonumber(os.date("%H"))
  local time_of_day
  if hours >= 8 and hours < 18 then
    time_of_day = "daylight"
  elseif hours >= 18 and hours < 20 then
    time_of_day = "sunset"
  else
    time_of_day = "night"
  end

  -- Create all images.
  self.background_img = sol.surface.load("sprites/menus/title_" .. time_of_day .. "_background.png")
  self.clouds_img = sol.surface.load("sprites/menus/title_" .. time_of_day .. "_clouds.png")
  self.logo_img = sol.surface.load("sprites/menus/title_logo.png")

  self.press_space_img = sol.text_surface.create{
    font = sol.language.get_dialog_font(),
    font_size = 24,
    color = {255, 255, 255},
    text_key = "title_screen.press_space",
    horizontal_alignment = "center"
  }
  self.press_space_img_bg = sol.text_surface.create{
    font = sol.language.get_dialog_font(),
    font_size = 24,
    color = {0, 0, 0},
    text_key = "title_screen.press_space",
    horizontal_alignment = "center"
  }

  self.show_press_space = false
  function switch_press_space()
    self.show_press_space = not self.show_press_space
    sol.timer.start(self, 500, switch_press_space)
  end
  sol.timer.start(self, 6500, switch_press_space)

  -- Make the clouds move.
  self.clouds_xy = {x = 416, y = 240}
  function move_clouds()
    self.clouds_xy.x = self.clouds_xy.x + 1
    self.clouds_xy.y = self.clouds_xy.y - 1
    if self.clouds_xy.x >= 535 then
      self.clouds_xy.x = self.clouds_xy.x - 535
    end
    if self.clouds_xy.y < 0 then
      self.clouds_xy.y = self.clouds_xy.y + 299
    end
    sol.timer.start(self, 50, move_clouds)
  end
  sol.timer.start(self, 50, move_clouds)

  -- Show an opening transition.
  self.surface:fade_in(30)

  self.allow_skip = false
  sol.timer.start(self, 2000, function()
    self.allow_skip = true
  end)
end

function title_screen:on_draw(dst_surface)
  if self.phase == "title" then
    self:draw_phase_title(dst_surface)
  end

  -- Final blit (dst_surface may be larger).
  local width, height = dst_surface:get_size()
  self.surface:draw(dst_surface, width / 2 - 208, height / 2 - 120)
end

function title_screen:draw_phase_title()
  -- Background.
  self.surface:fill_color({0, 0, 0})
  self.background_img:draw(self.surface)

  -- Clouds.
  local x, y = self.clouds_xy.x, self.clouds_xy.y
  self.clouds_img:draw(self.surface, x, y)
  x = self.clouds_xy.x - 535
  self.clouds_img:draw(self.surface, x, y)
  x = self.clouds_xy.x
  y = self.clouds_xy.y - 299
  self.clouds_img:draw(self.surface, x, y)
  x = self.clouds_xy.x - 535
  y = self.clouds_xy.y - 299
  self.clouds_img:draw(self.surface, x, y)

  -- Logo
  self.logo_img:draw(self.surface)

  if self.dx_img then
    self.dx_img:draw(self.surface)
  end
  if self.star_img then
    self.star_img:draw(self.surface)
  end
  if self.show_press_space then
    self.press_space_img_bg:draw(self.surface, 261, 196)
    self.press_space_img:draw(self.surface, 260, 195)
  end
end

function title_screen:on_key_pressed(key)
  local handled = false

  if key == "escape" then
    -- Stop the program.
    sol.main.exit()
    handled = true
  elseif key == "space" or key == "return" then
    handled = self:try_finish_title()
  elseif sol.main.is_debug_enabled() then  --  Debug.
    if key == "left shift" or key == "right shift" then
      self:finish_title()
      handled = true
    end
  end
end

function title_screen:on_joypad_button_pressed(button)
  return self:try_finish_title()
end

-- Ends the title screen (if possible) and
-- starts the savegame selection screen.
function title_screen:try_finish_title()
  local handled = false

  if self.phase == "title"
      and self.allow_skip
      and not self.finished then
    self.finished = true

    self.surface:fade_out(30)
    sol.timer.start(self, 700, function()
      self:finish_title()
    end)

    handled = true
  end

  return handled
end

function title_screen:finish_title()
  sol.audio.stop_music()
  sol.menu.stop(self)
end

return title_screen