--[[
  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/>.
]]--

--This file is mainly for testing the engine

local Forest2D = require "Forest2D"

Forest2D:Init()

Forest2D.WINDOW_WIDTH = 640
Forest2D.WINDOW_HEIGHT = 480
local FULLSCREEN = false

--Command Line Parameters
if (arg[1] == "FULLSCREEN") then FULLSCREEN = true end

window = Forest2D:CreateSDLWindow("Forest2d", "../Assets/Graphics/Textures/Soil.bmp")

if FULLSCREEN == true then
ret, error = window:setFullscreen(SDL.window.Fullscreen)
if not ret then error(error) end
end
Renderer = Forest2D:CreateSDLRenderer(window)

local EventHandler = Forest2D.EventHandler.New(2) --2 milisecond delay between events
local Anim1 = Forest2D.TileAnimation.New(Renderer, "../Assets/Graphics/Tilesets/Tileset_Template.bmp") -- using the Tileset_Template as a convenient test animation
local RainyDay = Forest2D.Audio.New ("../Assets/Audio/RainyDay.ogg", 1, 44100, 2)

RainyDay:Play()

local TileAnimationset = Forest2D.World.TileAnimationset.New(Renderer, "../Assets/Graphics/TileAnimationsets/TileAnimationset_Template.bmp", "../Assets/Graphics/TileAnimationsets/TileAnimationset_Template.cfg")
local Tileset = Forest2D.World.Tileset.New(Renderer, "../Assets/Graphics/Tilesets/Tileset_Template.bmp")
local GameWorld = Forest2D.World.New (Renderer, Tileset, TileAnimationset, 32, 1, 6, 48, 64)

local TitleBar = {color = 0x0000FF, alpha = 210, text = "Window of glory", textcolor = 0xFFFFFF, selectable = true}

local Window1 = Forest2D.GUI.NewWindow (Renderer, {x = 150, y = 128, w = 320, h = 200}, 0x444444, 96, TitleBar)
local Label1 = Forest2D.GUI.NewLabel(Window1, Renderer, {x = 0, y = 0, w = 256, h = 24}, "./unifont-10.0.06.ttf", 64, "Hello World i am text", 0xFF0000)
local Label2 = Forest2D.GUI.NewLabel(Window1, Renderer, {x = 0, y = 48, w = 320, h = 24}, "./unifont-10.0.06.ttf", 64, "Label2 is the grandest Label", 0x000000)
local Button1 = Forest2D.GUI.NewButton(Window1, Renderer, {x = 0, y = 96, w = 64, h = 24}, "Press me!", 0x000000, 0xFFFFFF)
local GUI_Image = Forest2D.GUI.NewImage(Window1, Renderer, {x = 275, y = 200, w = 100, h = 100}, "../Assets/Graphics/Textures/Door.bmp")

local Item1 = Forest2D.GUI.NewButton(nil, Renderer, nil, "item1", 0x000000, 0xFFFFFF)
local Item2 = Forest2D.GUI.NewButton(nil, Renderer, nil, "item2", 0x000000, 0xFFFFFF)
local Item3 = Forest2D.GUI.NewButton(nil, Renderer, nil, "item3", 0x000000, 0xFFFFFF)

local Menu1 = Forest2D.GUI.NewMenu(Renderer, {Item1, Item2, Item3}, {x = 0, y = 0, w = 100, h = 30}, "MyMenu", 0x000000, 0xFFFFFF, 255)

Window1:Add(Label1)
Window1:Add(Label2)
Window1:Add(Button1)
Window1:Add(GUI_Image)

    --just some random tiles
    GameWorld:SetTile(1,6,1,1,4,"tile_id")
    GameWorld:SetTile(1,6,2,2,true,"animated")
    GameWorld:SetTile(1,6,2,2,2,"animation_id")
    GameWorld:SetTile(1,6,4,4,true,"animated")
    GameWorld:SetTile(1,6,4,4,3,"animation_id")
    GameWorld:SetTile(1,6,1,64,1,"tile_id")
    GameWorld:SetTile(1,6,48,1,1,"tile_id")


    Forest2D.TextureList:Load(Renderer, "../Assets/Graphics/Textures")

    AnimationTableTest = {}
    AnimationTableTest.LoopStatus = true
    AnimationTableTest.Speed = 1000
    AnimationTableTest.Indicies = {1,2,3,4}

    TestSprite = Forest2D.Sprite.New(100,100,100,100, 6, true, AnimationTableTest)
    GameWorld.SpriteList.Push(TestSprite)

    GameWorld:Save("../Data/Worldtest.world", "/tmp/test.data")--testing
    GameWorld:Load("../Data/Worldtest.world", "/tmp/test.data")--testing

Renderer:setDrawColor(0xFFFFFF)
while true do

    if FULLSCREEN == true then
        ret, err = window:setFullscreen(SDL.window.Fullscreen)
            if not ret then
                error(er)
            end
    else
        ret, err = window:setFullscreen(SDL.window.OpenGL)
            if not ret then
                error(err)
            end
    end

    --Manage Events
    if EventHandler.EventStatus[EVENT_SDLQUIT].Active or EventHandler.EventStatus[EVENT_ESCKEY_PRESSED].Active then print "Exiting..." goto QUIT end
    if EventHandler.EventStatus[EVENT_FKEY_PRESSED].Active then if FULLSCREEN == false then FULLSCREEN = true else FULLSCREEN = false end EventHandler.DisableEvent(EVENT_FKEY_PRESSED, 500) end

    if EventHandler.EventStatus[EVENT_SKEY_PRESSED].Active then GameWorld:Save("../Data/Worldtest.world", "/tmp/world.data") print "Saved World" EventHandler.DisableEvent(EVENT_SKEY_PRESSED, 500) end
    if EventHandler.EventStatus[EVENT_LKEY_PRESSED].Active then GameWorld:Load("../Data/Worldtest.world", "/tmp/world.data") print "Loaded World" EventHandler.DisableEvent(EVENT_LKEY_PRESSED, 500) end

    if EventHandler.EventStatus[EVENT_UPKEY_PRESSED].Active then GameWorld.Camera.PosY = GameWorld.Camera.PosY - 4 end
    if EventHandler.EventStatus[EVENT_DOWNKEY_PRESSED].Active then GameWorld.Camera.PosY = GameWorld.Camera.PosY + 4 end
    if EventHandler.EventStatus[EVENT_LEFTKEY_PRESSED].Active then GameWorld.Camera.PosX = GameWorld.Camera.PosX - 4 end
    if EventHandler.EventStatus[EVENT_RIGHTKEY_PRESSED].Active then GameWorld.Camera.PosX = GameWorld.Camera.PosX + 4 end

    if EventHandler.EventStatus[EVENT_RKEY_PRESSED].Active then GameWorld.Camera.PosX = 0 GameWorld.Camera.PosY = 0 end -- Reset Camera
    if Button1:Clicked(EventHandler) then
      print("Thanks!")
    end

    EventHandler.GetActiveEvents()

    --[[
    Code here just testing
    ]]--

    --[[
    Code here just testing
    ]]--

    for I=1, GameWorld.SizeZ do GameWorld:Render(1,I) end

    Window1:Render(EventHandler)
    Menu1:Render(EventHandler)

    Renderer:present()

    Renderer:clear()

end

::QUIT::

GameWorld:Free()
Forest2D:Quit()
