------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------

local make_class  = require 'src.utils.classes'
local GameState   = require 'src.gstates.gstate'
local Sprite      = require 'src.graphics.sprite'
local Fader       = require 'src.graphics.fader'
local SoundEffect = require 'src.sound.sfx'

------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------

local Intro = make_class(GameState)


------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------

function Intro:_init(name, index)
    GameState._init(self, name, index)
    self.skip  = false
    self.fade  = Fader()
    self.stage = 0
    self.timer = 0

    self.splash = Sprite('imgs/splash.png')
    self.author = Sprite('imgs/wally.png')
    self.title  = Sprite('imgs/title.png')

    self.splash_snd = SoundEffect('snd/jachiev.wav')
    self.author_snd = SoundEffect('snd/alang.wav')
    self.title_snd  = SoundEffect('snd/ablhole.wav')
end


function Intro:load()
    -- Load all assets.
    self.splash:load()
    self.author:load()
    self.title:load()
    self.splash_snd:load()
    self.author_snd:load()
    self.title_snd:load()

    -- Play the initial sound effect.
    self.splash_snd:play()
end


function Intro:update(dt)
    local total_time

    -- Set time to wait for each intro stage.
    if self.stage == 0 then
        total_time = 6.5
    elseif self.stage == 1 then
        total_time = 5.0
    else
        total_time = 8.0
    end

    -- Start counting the stage time when the fade in is done.
    if self.fade.done then
        self.timer = self.timer + dt
    end

    -- Check if it's time to change stage.
    if self.timer >= total_time then
        self.fade:fade_out(
            function ()
                -- Change the stage and play the corresponding sound.
                self.stage = self.stage + 1

                if self.stage == 1 then
                    self.author_snd:play()
                elseif self.stage == 2 then
                    self.title_snd:play()
                end

                -- Request a fade in.
                self.fade:fade_in()
            end
        )

        -- Reset the stage timer.
        self.timer = 0.0
    end

    -- Update the fader.
    self.fade:update(dt)

    -- Move on to the next game state if the user skipped the intro or all stages are complete.
    if not self.skip and self.stage < 3 then return self.index else return 2 end
end


function Intro:draw()
    -- If there is data to draw, then draw the current stage's backdrop.
    if     self.stage == 0 then self.splash:draw()
    elseif self.stage == 1 then self.author:draw()
    elseif self.stage == 2 then self.title:draw() end

    -- Draw the fader if needed.
    self.fade:draw()
end


function Intro:unload()
    -- Null all assets so they can be claimed by the GC.
    self.splash:unload()
    self.author:unload()
    self.title:unload()
    self.splash_snd:unload()
    self.author_snd:unload()
    self.title_snd:unload()
end


function Intro:keypressed(_)
    -- Skip the intro on any key press.
    self.skip = true
end


------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------

return Intro
