--Translator
local S = core.get_translator("shapetester")

--Menu style settings
local shapemenu_style = core.settings:get("shapetester_menu_style") or "shapetester"
local custom_menu_style = core.settings:get("shapetester_custom_menu_style") or "shapetester_menu.png"

local function meta(player)
    local value = player:get_meta()
    return value
end

core.register_on_joinplayer(function(player)
    if meta(player):get("shapetester_fav1") == nil then
        meta(player):set_string("shapetester_fav1", "shapetester:cube")
    end
    if meta(player):get("shapetester_fav2") == nil then
        meta(player):set_string("shapetester_fav2", "shapetester:cube")
    end
    if meta(player):get("shapetester_fav3") == nil then
        meta(player):set_string("shapetester_fav3", "shapetester:cube")
    end
end)

local favourite_form = function(player)
    local formspec = "size[8,4.1]"..
        "field[0.25,0.35;8,0.8;fav1;"..S("Favourite").." 1"..":;"..meta(player):get("shapetester_fav1").."]"..
        "field[0.25,1.35;8,0.8;fav2;"..S("Favourite").." 2"..":;"..meta(player):get("shapetester_fav2").."]"..
        "field[0.25,2.35;8,0.8;fav3;"..S("Favourite").." 3"..":;"..meta(player):get("shapetester_fav3").."]"..
        "button_exit[0.25,4;7,0.1;exit;"..S("Save and exit").."]"

    formspec = formspec .. "button[0.25,3;2,0.5;t1;"..S("Transform!").." (1)".."]"
    formspec = formspec .. "button[2.75,3;2,0.5;t2;"..S("Transform!").." (2)".."]"
    formspec = formspec .. "button[5.25,3;2,0.5;t3;"..S("Transform!").." (3)".."]"

    if shapemenu_style == "none" then
        --Nothing
    elseif shapemenu_style == "shapetester" then
        formspec = formspec .. "background[0,0;0,0;shapetester_menu.png;true]"
    elseif shapemenu_style == "white" then
        formspec = formspec .. "background[0,0;0,0;shapetester_plain_menu.png;true]"
    elseif shapemenu_style == "grey" then
        formspec = formspec .. "background[0,0;0,0;shapetester_grey_menu.png;true]"
    elseif shapemenu_style == "mese" then
        formspec = formspec .. "background[0,0;0,0;shapetester_mese_menu.png;true]"
    elseif shapemenu_style == "custom" then
        formspec = formspec .. "background[0,0;0,0;"..custom_menu_style..";true]"
    end

    return formspec
end

shapetester.show_favourites_form = function(player)
    core.show_formspec(player:get_player_name(), "shapetester:favourites", favourite_form(player))
end

core.register_on_player_receive_fields(function(player, formname, fields)
    if formname ~= "shapetester:favourites" then
        return
    end

    if fields.t1 then
        local shapename = fields.fav1
        local player_name = player:get_player_name()
        shapetester.transform(player_name,shapename)
        for name,def in pairs(shapetester.registered_shapeshifts) do
            if name == shapename then
                core.sound_play(def.transform_noise or default_shapeshift_sound, {pos=player:get_pos(),gain=0.4,max_hear_distance = 16})
                if core.settings:get_bool("shapeshift_sparks") == false then
                    --Nothing
                else 
                    shapetester.sparks(player, def.sparks_textures or default_sparks_texture)
                end        
            end
        end
        meta(player):set_string("shapetester_fav1", fields.fav1)
        meta(player):set_string("shapetester_fav2", fields.fav2)
        meta(player):set_string("shapetester_fav3", fields.fav3)
    end
    if fields.t2 then
        local shapename = fields.fav2
        local player_name = player:get_player_name()
        shapetester.transform(player_name,shapename)
        for name,def in pairs(shapetester.registered_shapeshifts) do
            if name == shapename then
                core.sound_play(def.transform_noise or default_shapeshift_sound, {pos=player:get_pos(),gain=0.4,max_hear_distance = 16})
                if core.settings:get_bool("shapeshift_sparks") == false then
                    --Nothing
                else 
                    shapetester.sparks(player, def.sparks_textures or default_sparks_texture)
                end        
            end
        end
        meta(player):set_string("shapetester_fav1", fields.fav1)
        meta(player):set_string("shapetester_fav2", fields.fav2)
        meta(player):set_string("shapetester_fav3", fields.fav3)
    end
    if fields.t3 then
        local shapename = fields.fav3
        local player_name = player:get_player_name()
        shapetester.transform(player_name,shapename)
        for name,def in pairs(shapetester.registered_shapeshifts) do
            if name == shapename then
                core.sound_play(def.transform_noise or default_shapeshift_sound, {pos=player:get_pos(),gain=0.4,max_hear_distance = 16})
                if core.settings:get_bool("shapeshift_sparks") == false then
                    --Nothing
                else 
                    shapetester.sparks(player, def.sparks_textures or default_sparks_texture)
                end        
            end
        end
        meta(player):set_string("shapetester_fav1", fields.fav1)
        meta(player):set_string("shapetester_fav2", fields.fav2)
        meta(player):set_string("shapetester_fav3", fields.fav3)
    end
    if fields.exit then
        meta(player):set_string("shapetester_fav1", fields.fav1)
        meta(player):set_string("shapetester_fav2", fields.fav2)
        meta(player):set_string("shapetester_fav3", fields.fav3)
    end
end)