require("util")

require("bots")

function play_roll()
    local res = {}
    for i=1,5 do
        local v = math.random(1,6)
        res[i] = v
    end
    return res
end

function roll2map(roll)
    local res = {0,0,0,0,0,0}
    for _,v in pairs(roll) do
        res[v] = (res[v] or 0) + 1
    end
    return res
end

function play_bots() 
    local res = {}
    res[1] = {name='bot1',image='monster1',code=bot_basic}
    return res
end


function play_ready()
    if GAME.play then return end
    play_init()
end

function play_init()
    GAME.play = {}
    GAME.play.posts = {}
    GAME.play.score = 0
    -- add_post({type='tip',value="Each day brag about how much"})
    -- add_post({type='tip',value="the monsters ate on beastbook."})
    -- add_post({type='tip',value="Like truth. Report lies."})
    -- add_post({type='tip',value="The top shows what you know."})

    add_post({type='tip',value="Each day brag about how much\n"
        .."the monsters ate on beastbook.\n"
        .."Like truth. Report lies.\n"..
        "The top shows what you know."})


    play_next_day()
end

function play_next_day()
    GAME.play.rolls = { play_roll(), play_roll() }
    GAME.play.players = { 0, 1 }
    shuffle(GAME.play.players)

    GAME.play.count = 0
    GAME.play.turn = 1
    GAME.play.cur = 1
    GAME.play._item = 0
    GAME.play._count = 0
    GAME.play.bets = {}
    GAME.play.phase = 2

    local play = GAME.play
    if play._item == 0 then
        local map = roll2map(play.rolls[1])
        local best = 0 ; local value = 0
        for k,v in pairs(map) do
            if v > value then best = k ; value = v end
        end
        play._item = best
        play._count = value
    end

    if GAME.play.players[GAME.play.cur] > 0 then
        play_respond()
    end
end

function add_post(post)
    local play = GAME.play
    play.posts[#play.posts+1] = post
end


function play_append(player,item,count)
    local play = GAME.play
    local image = 'player'
    local bots = play_bots()
    if play.players[player] > 0 then image = bots[play.players[player]].image end
    GAME.play.posts[#GAME.play.posts+1] = { player = player, item = item, count = count, image=image }
    GAME.play.bets[#GAME.play.bets+1] = { player = player, item = item, count = count }

    GAME.play.count = count

    play_next()
    play.turn = play.turn + 1
    play.cur = (play.turn-1)%#play.players + 1
    play.phase = 1
end

function play_liar()
    local play = GAME.play
    local bet = play.bets[#play.bets]
    local totals = {0,0,0,0,0,0}
    for _,roll in pairs(play.rolls) do
        for _,v in pairs(roll) do
            totals[v] = totals[v] + 1
        end
    end

    if totals[bet.item] < bet.count then -- LIAR
        if play.players[play.cur] == 0 then play.score = play.score + 1 end
        add_post({type='tip',value="Lie detected.  Next day."})
        play_next_day()
    else -- TRUTH
        if play.players[play.cur] ~= 0 then play.score = play.score + 1 end
        add_post({type='tip',value="False accusation.  Next day."})
        play_next_day()
    end
end

function play_next()
    local play = GAME.play
    play.phase = 2
end

function play_respond()
    local play = GAME.play
    local uid = play.players[play.cur]
    if uid > 0 then
        local bots = play_bots()
        local res = bots[uid].code(play.rolls[play.cur],play.bets)
        if res then
            play_next()
            play_append(play.cur, res.item, res.count)
        else
            play_liar()
        end
    end
end

--------------------------------------------------------------------------------
function menu_init()
--------------------------------------------------------------------------------
GAME.modules.menu = GAME.modules.menu or {}
local obj = GAME.modules.menu
    
function obj:init()
    self.t = 0
    
    self.gui = GUI.Container(Rect(0,0,GAME.SW,GAME.SH))
    local gui = self.gui
    
    local yinc = 32
    local y = (GAME.SH-yinc * 1)/2 - 4
    local w = 90
    local x = (GAME.SW - (w)) / 2
    local h = 24
    local bg = 0x5555ff
    

    local play = GAME.play

    if play._count > (play.count + 6) then
        play._count = play.count
    end
    play._count = math.max(play.count+1,play._count)

    local y = 22
    if play.players[play.cur] == 0 and play.phase == 2 then
        local rect = Rect(2,y,GAME.SW-4,50)
        gui:add(GUI.Div(),rect)
        gui:add(GUI.Image('player',Rect(20,20)),Rect(rect.x,rect.y,25,25))
        gui:add(GUI.Label(GAME.font,"What did we eat today?"),Rect(rect.x+20,rect.y,rect.w-20,25))

        gui:add(GUI.Button(GUI.Label(GAME.bfont,""..play._count.."x"),function () event({type='count'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-120,rect.y+25,20,20))

        gui:add(GUI.Button(GUI.Image("item"..play._item,Rect(16,16)),function () event({type='item'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-120+25,rect.y+25,20,20))

        gui:add(GUI.Button(GUI.Label(GAME.bfont,"Post"),function () event({type='post'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-40,rect.y+25,35,20))

        y = y + rect.h
    end

    for n=1,12 do
        local post = play.posts[#play.posts+1-n]
        if post ~= nil and post.type == 'tip' then
            local ww,hh = GAME.font:size(post.value)
            hh = hh + 10
            local rect = Rect(2,y,GAME.SW-4,hh)

            gui:add(GUI.Div(),rect)
            gui:add(GUI.Label(GAME.font,post.value),Rect(rect.x+0,rect.y,rect.w-0,hh))

            y = y + rect.h

        elseif post ~= nil then
            local rect = Rect(2,y,GAME.SW-4,25)
            if n == 1 and play.phase == 1 then rect.h = rect.h + 25 end

            gui:add(GUI.Div(),rect)
            gui:add(GUI.Image(post.image,Rect(20,20)),Rect(rect.x,rect.y,25,25))
            gui:add(GUI.Label(GAME.font,"We ate "..post.count.."x",1),Rect(rect.x+20,rect.y,80,25))
            gui:add(GUI.Image('item'..post.item,Rect(12,12)),Rect(rect.x+100,rect.y-0.5,25,25))

            if n == 1 and play.phase == 1 then

                gui:add(GUI.Button(GUI.Label(GAME.bfont,"Like"),function () event({type='like'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-(40*3),rect.y+25,35,20))

                gui:add(GUI.Button(GUI.Label(GAME.bfont,"Report"),function () event({type='report'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-(40*2),rect.y+25,35,20))

                -- gui:add(GUI.Button(GUI.Label(GAME.bfont,"Share"),function () event({type='share'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2)),Rect(rect.x+rect.w-40,rect.y+25,35,20))
            end

            y = y + rect.h
        end
    end


    -- self.start = GUI.Button(GUI.Label(GAME.bfont,"Play"),function () event({type='start'}) end,GUI.Background("white",GAME.button_b1,GAME.button_b2))
    -- self.gui:add(self.start,Rect(x,y,w,h))
    -- y = y + yinc




end


function obj:event(e)

    if e.type == 'keydown' and e.text == '`' then
        cuz.stream_stop("theme")
        GAME.engine:next(GAME.modules.edit)
    end

    if e.type == 'count' then
        GAME.play._count = GAME.play._count + 1
        self:init()
    end
    
    if e.type == 'report' then
        play_liar()
        self:init()
    end

    if e.type == 'like' then
        play_next()
        self:init()
    end

    if e.type == 'item' then
        GAME.play._item = (GAME.play._item % 6) + 1
        self:init()
    end

    if e.type == 'post' then
        play_append(GAME.play.cur,GAME.play._item,GAME.play._count)
        play_respond()
        self:init()
    end

    if e.type == 'start' then
        if GAME.high > 0 then
            GAME.engine:next(GAME.modules.menu_shield)
            return
        end

        cuz.sample_play("sfx-next")
        GAME.engine:next(GAME.modules.level_play)

    end

    if e.type == 'keydown' and e.name == 'AC Back' then
        cuz_ext_call('exit')
    end

    self.gui:event(e)
end
function obj:loop(t)

    self.gui:loop(t)

end
function obj:paint()
    cuz.view_set(0,0,GAME.SW,GAME.SH)

    cuz.rect_draw(0xeeeeee,0,0,GAME.SW,GAME.SH)
    cuz.rect_draw(0x446699,0,0,GAME.SW,20)

    -- local z = 2
    -- cuz.rect_draw(0xdddddd,0+z,20+z,GAME.SW-z*2,GAME.SH-(20+z*2))
    -- local z = 3
    -- cuz.rect_draw(0xffffff,0+z,20+z,GAME.SW-z*2,GAME.SH-(20+z*2))


    local z = 2
    cuz.image_draw("logo",z,z,20-z*2,20-z*2)

    cuz.image_draw("player",GAME.SW-20+z,z,20-z*2,20-z*2)

    GAME.bfont:draw(""..GAME.play.score,GAME.SW-35,5)

    local roll = GAME.play.rolls[1]
    for i=1,5 do
        local x = (GAME.SW - 14 * 5)/2 + (i-1) * 15 - 2
        cuz.image_draw("item"..roll[i],x,3,14,14)
    end

    preload()

    self.gui:draw()



end

--------------------------------------------------------------------------------
end