require ":utils.palfade"
require ":utils.console"
require ":utils.tweening"

function test()
    tweening.add(40, 100, 1, easing.easeOutCubic, function(value, progress, finished) sprites.hero.light = value end)
    tweening.add(0, 1, 1, easing.easeOutCubic, function(value, progress, finished) palfade.fade_reddish(value) end)
end

cheats = {}

game = {
    chg_adv = {x=0, y=0},
    chg_step = 0,
    back_buf = 0,
    circ_buf = 0,

    water_pal = {6,6,21,21,6,21,6,22,22,22,22,22,22,22,22,21,21,22,22,22,22,21,22,22,21,22,22,22,22,6,6,6},
    fade_pal = {1,1,3,1,1,3,29,29,3,3,3,3,3,6,6,29,29,3,3,3,3,29,6,29,29,6,29,6,6,1,1,1},

    light_strobe_value = 0,
    light_strobe_dir = 1,
    water_counter = 0,
    fade=0,

    enable = function()
        if game.back_buf == 0 then
            game.back_buf = surf.new(160,104)
            game.circ_buf = surf.new(160,104)
        end
        app.update = game.update
        sys.beat(2)
        palfade.init()
        score.init()
        --palfade.fade_reddish(0.5)
        --shader.enable();
    end,

    restart = function()
        --rooms.reload()
        ia.hero_reset()
        sprites.add_from_room(rooms.pos.x, rooms.pos.y)
        pal.set(palfade.original)
        sprites.hero.light = 0
        tweening.add(0,100,1,easing.linear,function(value,n,finished)sprites.hero.light = value end)
        sprites.lights_in()
        score.init(true)
    end,

    update = function()
        game.water_counter = game.water_counter + 0.05

        if game.fade>0 then
            palfade.fade_red(game.fade)
            game.fade = game.fade - 0.1
        elseif game.fade<0 then
            palfade.fade_red(0)
            game.fade = 0
        end

        view.origin(0,0)
        surf.target(0)
        surf.clip()

        surf.target(game.back_buf)

        -- Pintar el mapa i sprites
        rooms.draw()
		sprites.update()

        game.apply_water()
        game.apply_light()

        score.draw()

        view.origin(0,0)
        draw.text("safe: "..sprites.last_safe_room,1,96,28)
        --draw.text(sprites.hero.pos.x ..","..sprites.hero.pos.y,14,89,28)

        if key.press(key.ESCAPE) or key.press(key.F9) then
            rooms.reload()
            sprites.add_from_room(rooms.pos.x, rooms.pos.y)
            ia.hero_reset()
            editor.enable()
        elseif key.press(key.GRAVE) then
            console.enable()
        end

        draw.text(sys.fps(),2,20,8)
    end,

    change_room = function(x,y)
        game.chg_adv.x = x*0.25
        game.chg_adv.y = y*0.25
        game.chg_step = 8*4
        sprites.pause_ia = true
        -- [TODO] Crear els sprites per als items de l'habitació a la que entrem
        sprites.add_from_room(rooms.pos.x+x*20, rooms.pos.y+y*13)
        app.push(game.update_change_room)
        --sys.beat(10)
    end,

    update_change_room = function()
        --if not sys.beat() then return end
        view.origin(0,0)
        surf.target(0)
        surf.clip()

        surf.target(game.back_buf)
        --view.clip()

        -- Pintar el mapa i sprites
        rooms.pos.x = rooms.pos.x + (game.chg_adv.x*2.5)
        rooms.pos.y = rooms.pos.y + (game.chg_adv.y*1.625)
        sprites.hero.pos.x = sprites.hero.pos.x + game.chg_adv.x
        sprites.hero.pos.y = sprites.hero.pos.y + game.chg_adv.y

        rooms.draw()
		sprites.update()
        game.apply_water()
        game.apply_light()

        score.draw()

        game.chg_step = game.chg_step - 1
        if game.chg_step == 0 then
            sprites.pause_ia = false
            app.pop()
            --sys.beat(2)
        end
    end,

    draw_light = function(x,y,size)
        surf.target(game.circ_buf)
        local s = size+(game.light_strobe_value/2)
        draw.mode(draw.OR)
        draw.circf(x,y,s,1)
        draw.circf(x,y,2*(s/3),2)
        draw.mode(draw.NORMAL)
        surf.target(game.back_buf)
    end,

    apply_water = function()
        view.origin(0,0)
        if rooms.pos.y == 91 then
            surf.target(game.back_buf)
            surf.source(game.back_buf)
            for x=0,159 do
                local water_level = math.sin(game.water_counter)*2
                for y=88+water_level,104 do
                    local pixel = draw.pixel(x,y)
                    draw.pixel(x,y,game.water_pal[pixel+1])
                end
            end
        end
    end,

    apply_light = function()
        game.light_strobe_value = game.light_strobe_value + game.light_strobe_dir
        if math.abs(game.light_strobe_value)==2 then
            game.light_strobe_dir = -game.light_strobe_dir
        end

        surf.target(0)
        for y=0,103 do
            for x=0,159 do
                surf.source(game.back_buf)
                local pixel = draw.pixel(x,y)
                surf.source(game.circ_buf)
                local light = draw.pixel(x,y)
                if light==0 then
                    draw.pixel(x,y,1)
                elseif (light&2)==2 then
                    draw.pixel(x,y,pixel)
                else
                    draw.pixel(x,y,game.fade_pal[pixel+1])
                end
            end
        end
    end


}