require "..utils.ui"

menu = {
    hidden = true,
    pos_y = 0,
    selected = 1,
} local me = menu

function me.draw()
    actions = {
        function() editor.play() me.close() end,
        function() rooms.save() editor.modified=false me.close() end,
        function() editor.quit() me.close() end,
        function() editor.layer=LAYER_FOREGROUND me.close() end,
        function() editor.layer=LAYER_BACKGROUND me.close() end,
        function() editor.layer=LAYER_ITEMS me.close() end,
        function() rooms.toggle_visibility(LAYER_FOREGROUND) end,
        function() rooms.toggle_visibility(LAYER_BACKGROUND) end,
        function() rooms.toggle_visibility(LAYER_SHADOWS) end,
        function() rooms.toggle_visibility(LAYER_ITEMS) end
    }

    local sel = me.selected
    view.origin(0,me.pos_y)
    surf.clip()
    local y = me.pos_y
    draw.rrectf(1,0,8,8,1,21)
    draw.rrectf(1,-1,8,8,1,6)
    draw.hline(3,1,6,21)
    draw.hline(3,3,6,21)
    draw.hline(3,5,6,21)
    if editor.modified then draw.rectf(6,0,2,2,8) end

    if y<=0 then return end

    view.origin(0,me.pos_y-24)
    draw.rectf(0,0,160,24,6)

    ui.pushbutton("PLAY",1,3,16,13,10,actions[1], sel==1)
    ui.pushbutton("SAVE",22,3,7,22,21,actions[2], sel==2)
    if editor.modified then draw.rectf(37,4,2,2,8) end
    ui.pushbutton("QUIT",22,12,7,12,5,actions[3], sel==3)

    draw.rrect(44,6,29,14,3,21)
    draw.rectf(48,6,17,1,6)
    draw.text("EDIT",49,3,21)
    ui.togglebutton("F",44+3,6+3,7,14,9,15,7,editor.layer==LAYER_FOREGROUND,actions[4], sel==4)
    ui.togglebutton("B",52+3,6+3,7,14,9,15,7,editor.layer==LAYER_BACKGROUND,actions[5], sel==5)
    ui.togglebutton("I",60+3,6+3,7,14,9,15,7,editor.layer==LAYER_ITEMS,actions[6], sel==6)

    draw.rrect(75,6,37,14,3,21)
    draw.rectf(79,6,17,1,6)
    draw.text("SHOW",80,3,21)

    ui.togglebutton("F",75+3,6+3,7,14,9,15,7,rooms.is_visible(LAYER_FOREGROUND),actions[7], sel==7)
    ui.togglebutton("B",83+3,6+3,7,14,9,15,7,rooms.is_visible(LAYER_BACKGROUND),actions[8], sel==8)
    ui.togglebutton("S",91+3,6+3,7,14,9,15,7,rooms.is_visible(LAYER_SHADOWS),actions[9], sel==9)
    ui.togglebutton("I",99+3,6+3,7,14,9,15,7,rooms.is_visible(LAYER_ITEMS),actions[10], sel==10)

    local tab = key.press(key.TAB)
    if key.press(key.ESCAPE) then
        me.toggle()
    elseif key.press(key.LEFT) or (tab and key.down(key.LSHIFT)) then
        me.selected=me.selected-1
        if me.selected < 1 then me.selected = 10 end
    elseif key.press(key.RIGHT) or (tab and not key.down(key.LSHIFT)) then
        me.selected=me.selected+1
        if me.selected > 10 then me.selected = 1 end
    elseif key.press(key.RETURN) or key.press(key.SPACE) then
        actions[me.selected]()
    end
end

function me.toggle()
    if me.hidden then
        me.hidden = false
        me.selected = 1
        tweening.add(0,24,0.25,easing.easeOutBounce,function(value,n,finished)me.pos_y=value end)
    else
        me.hidden = true
        tweening.add(24,0,0.25,easing.easeOutBounce,function(value,n,finished)me.pos_y=value end)
    end
end

function me.close()
    if not me.hidden then
        me.hidden = true
        tweening.add(24,0,0.25,easing.easeOutBounce,function(value,n,finished)me.pos_y=value end)
    end
end
