--[[
  Copyright Tom Lukeywood 2018
    This file is part of Forest2D

    Forest2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Forest2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Forest2D.  If not, see <http://www.gnu.org/licenses/>.
]]--

TileAnimation = {}

function TileAnimation.New(Renderer, TileAnimationFile) -- Can take a AnimationFile or an Animation in the form of a SDL_Texture.

    local self = {}

    self.OldTicks = 0
    self.CurrentFrame = 0
    self.LastFrame = 0

    if type(TileAnimationFile) == "string" then -- Assume TileAnimationFile is a File

    self.Image, err = Image.load(TileAnimationFile)
    if not self.Image then error(err) end

    self.Texture, err = Renderer:createTextureFromSurface(self.Image)
    if not self.Texture then error(err) end

    else -- Assume TileAnimationFile is an SDL_Texture
        self.Texture = TileAnimationFile
        if not self.Texture then print ("Warning: Failed to create new Animation") return end
    end

    function self.PlayTile(Renderer, x, y, Speed, RunAsLoop, Camera) --Camera is an optional parameter
        local f, a, w, h = self.Texture:query()

        NumberOfFrames = w / World.SizeOfTile
        self.LastFrame = self.CurrentFrame


        if SDL.getTicks() >= self.OldTicks + Speed then
            self.CurrentFrame = self.CurrentFrame + 1
        end

        if self.LastFrame < self.CurrentFrame then --Check if self.CurrentFrame has incremented
            self.OldTicks = SDL.getTicks()
        end

        if self.CurrentFrame >= NumberOfFrames then
            if RunAsLoop == true then self.CurrentFrame = 0
            else self.CurrentFrame = NumberOfFrames - 1 end
        end

        local DestRect = {}
        DestRect.x = (x * World.SizeOfTile) - World.SizeOfTile
        DestRect.y = (y * World.SizeOfTile) - World.SizeOfTile
        DestRect.w = World.SizeOfTile
        DestRect.h = World.SizeOfTile

        self.PlayFrame(Renderer, DestRect, self.CurrentFrame, Camera)
        return
        end

    function self.Play(Renderer, DestRect, Speed, RunAsLoop, Camera) --Camera is an optional parameter

        local f, a, w, h = self.Texture:query()

        NumberOfFrames = w / World.SizeOfTile
        self.LastFrame = self.CurrentFrame


        if SDL.getTicks() >= self.OldTicks + Speed then
            self.CurrentFrame = self.CurrentFrame + 1
        end

        if self.LastFrame < self.CurrentFrame then --Check if self.CurrentFrame has incremented
            self.OldTicks = SDL.getTicks()
        end

        if self.CurrentFrame >= NumberOfFrames then
            if RunAsLoop == true then self.CurrentFrame = 0
            else self.CurrentFrame = NumberOfFrames - 1 end
        end

        self.PlayFrame(Renderer, DestRect, self.CurrentFrame, Camera)
        return
        end

    function self.PlayFrame(Renderer, DestRect, Frame, Camera)

        local f, a, w, h = self.Texture:query()

        NumberOfFrames = w / World.SizeOfTile

        --TileAnimationFile is a string
        if (Frame > NumberOfFrames or Frame < 0) then
            print("Frame: " .. Frame .. " Does not exist in given TileAnimationFile")
            return
        end

        local SrcRect = {}

        if Camera == nil then

            SrcRect.x = Frame * World.SizeOfTile
            SrcRect.y = 0
            SrcRect.w = World.SizeOfTile
            SrcRect.h = World.SizeOfTile
        else
            DestRect.x = DestRect.x - Camera.PosX
            DestRect.y = DestRect.y - Camera.PosY

            SrcRect.x = Frame * World.SizeOfTile
            SrcRect.y = 0
            SrcRect.w = World.SizeOfTile
            SrcRect.h = World.SizeOfTile
        end

        Renderer:copy(self.Texture, SrcRect, DestRect)
        return
    end

return self

end

return TileAnimation
