-- scoreboards
local storage = wormball.storage

arena_lib.on_celebration('wormball', function(arena, winner_name)

    --reset player textures back to the texture they were... (wormball sets player textures to clear)

    for pl_name,stats in pairs(arena.players) do
        if not(arena_lib.is_player_spectating(pl_name)) then
            wormball.detach(pl_name)
        end
    end

    

    --for now, arena_lib only supports single winners... change this when it supports multiple winners

    if type(winner_name) == 'string' then


        if arena.mode == 'singleplayer' then

            -- leaderboard will be a table with the following format 
            -- {
            --    {score,plname}
            --    {score,plname}
            --   }

            -- it will always be ordered, with the highest score first.

            local new_highscore = false

            local leaderboard = wormball.leaderboard.get_highscores(arena.name) --arena.singleplayer_leaderboard

            local score = arena.players [ winner_name ] .score

            if not leaderboard then leaderboard = {} end

            if # leaderboard == 0 then
                leaderboard [ 1 ] = { score , winner_name }
                new_highscore = true

            else

                local insert = true

                for idx , winner_table in ipairs( leaderboard ) do

                    if winner_table [ 2 ] == winner_name then --if the player already has a score on the board...

                        if score > winner_table[1] then -- new higher score achieved... we will now remove the old score so we can input the new score at the correct location
                            table.remove(leaderboard , idx )
                        else -- the score wasn't higher, so no need to insert a new score
                            insert = false
                        end

                    end

                end

                if insert then

                    for idx , winner_table in ipairs( leaderboard ) do
                        if score > winner_table [ 1 ] then 
                            table.insert( leaderboard , idx , { score , winner_name } )
                            if idx == 1 then
                                new_highscore = true
                            end
                            break
                        end
                    end

                end
            end

            --arena_lib.change_arena_property(winner_name, 'wormball', arena.name, "singleplayer_leaderboard", leaderboard)

            --arena_lib.change_arena_property('wormball' , 'wormball' , arena.name , "singleplayer_leaderboard" , leaderboard )

            --save (update) the highscore data to disk
            arena.singleplayer_leaderboard = leaderboard

                        
            local success, msg = wormball.leaderboard.save_highscores(arena.name,leaderboard)
            --minetest.chat_send_all(tostring(success))
            --minetest.chat_send_all(msg)
                        
            --local serial = minetest.serialize(arena.singleplayer_leaderboard)

            --storage:set_string(arena.name .. "_highscores", serial )






            if new_highscore then
                arena_lib.HUD_send_msg_all("title", arena, 'New High Score!', 2 ,'sumo_win',0xAEAE00)
                minetest.after(5,function()
                    if minetest.get_modpath("aes_xp") then
                        --register some achievements
                        aes_xp.add_achievement("Wormball Master", 50, 40, 0, "Beat the Highscore", "wormball_win.png")
                        aes_xp.achieve(winner_name,"Wormball Master")
                    end
                end)
            else
                arena_lib.HUD_send_msg_all("title", arena, 'Game Over!', 2 ,'sumo_win',0xAEAE00)
                minetest.after(5,function()
                    if minetest.get_modpath("aes_xp") then
                        --register some achievements
                        aes_xp.add_achievement("Worming Along", 10, 3, 0, "Play a round of \nsingleplayer wormball", "magiccompass_wormball.png")
                        aes_xp.achieve(winner_name,"Worming Along")
                    end
                end)
            end

            minetest.after( 3 , function( arena , score , leaderboard ) 

                arena_lib.HUD_send_msg_all("title", arena , 'You had ' .. score .. ' pts!', 3 ,'sumo_win',0xAEAE00)

                

                minetest.after( 3 , function( arena , score , leaderboard ) 

                    for pl_name , stats in pairs(arena.players_and_spectators) do
                        wormball.show_singleplayer_leaderboard( arena.name , pl_name )
                    end
                    
                end , arena , score , leaderboard )

                

            end , arena , score , leaderboard )

        else --arena.mode == 'multiplayer'

            arena_lib.HUD_send_msg_all("title", arena, 'New High Score!', 2 ,'sumo_win',0xAEAE00)

            table.insert( arena.multi_scores , 1 , { arena.players[ winner_name ] .score , winner_name } ) 

            local scores = arena.multi_scores



            if minetest.get_modpath( 'panel_lib' ) then
            
                minetest.after( 2 , function( arena , scores ) 
                    
                    for pl_name , stats in pairs(arena.players_and_spectators) do
                        wormball.show_multi_scores( arena , pl_name , scores )
                    end
                      
                    -- show the scoreboard here
                    
                end , arena , scores )

            end
        end

    end

end)


