images = nil
text = nil

close1 = nil
close2 = nil
close3 = nil
closeWIN = nil
closeLOSE = nil
open6 = nil

text_fill = nil
text_line = nil

menu_click = E.load_sound(media_dir .. "menu_click.ogg")
close_cricket = E.load_sound(media_dir .. "CRICKET.ogg")
close_music = E.load_sound(media_dir .. "Cold_Lazarus.ogg")
close_music_playing = false

time_accumulator = 0

function destroy_game_closing()
	E.delete_entity(game_closing_pivot)
	E.free_sound(menu_click)
    E.free_sound(close_cricket)
    E.free_sound(close_music)
    E.enable_timer(false)
    E.delete_entity(world)
end

function centered_string_game_close(text, line, fill)
        local string = E.create_string(text)
        local pivot = E.create_pivot()
        
        --local x0, y0, z0, x1, y1, z1 = E.get_entity_bound(string)
        
        E.set_string_line(string, line)
        E.set_string_fill(string, fill)
        E.parent_entity(string, pivot)
        
        --E.set_entity_position(string, -(x1 - x0)/2, -0.5, 0)
        E.set_entity_position(string, 0, -0.5, viewport.depth_bisect-1)
        
        return pivot, viewport.width, viewport.height--(x1 - x0), (y1 - y0)
end

local function init_game_closing()
	E.delete_entity(camera_ortho)
	E.free_sound(theme)
	E.set_entity_flags(world, E.entity_flag_hidden, true)

    camera = E.create_camera(E.camera_type_perspective)
    
	game_closing_pivot = E.create_pivot()

    local images = E.create_pivot()
    local text = E.create_pivot()
	
	E.parent_entity(game_closing_pivot, camera)
    E.parent_entity(images, game_closing_pivot )
    E.parent_entity(text, images )

    E.set_background(0.0, 0.0, 0.0)
    
    close1 = make_opaque_image_billboard(media_dir .. "close1.jpg")
    E.set_entity_alpha(close1, 0.0) -- hidden
    fit_to_view(close1, 0.8, 0.563, 0, 0)
    E.parent_entity(close1, images)
    
    close2 = make_opaque_image_billboard(media_dir .. "close2.jpg")
    E.set_entity_alpha(close2, 0.0) -- hidden
    fit_to_view(close2, 0.617, 0.8, 0, 0)
    E.parent_entity(close2, images)

    closeWIN = make_opaque_image_billboard(media_dir .. "closeWIN.jpg")
    E.set_entity_alpha(closeWIN, 0.0) -- hidden
    fit_to_view(closeWIN, 0.888, 0.6, 0, 0.15)
    E.parent_entity(closeWIN, images)
    
    closeLOSE = make_opaque_image_billboard(media_dir .. "closeLOSE.jpg")
    E.set_entity_alpha(closeLOSE, 0.0) -- hidden
    fit_to_view(closeLOSE, 0.75, 0.621, 0, 0.1)
    E.parent_entity(closeLOSE, images)

    --E.set_typeface(media_dir .. "VeraBD.ttf", 0.001, 0.0)
    E.set_typeface(media_dir .. "Nou.ttf", 0.001, 0.03)
    
    text_fill = E.create_brush()
    E.set_brush_color(text_fill, 1.0, 0.0, 0.0, 0.8)
    E.set_brush_flags(text_fill, E.brush_flag_unlit, true)

    text_line = E.create_brush()
    E.set_brush_color(text_line, 1.0, 0.0, 0.0, 0.8)
    E.set_brush_flags(text_line, E.brush_flag_unlit, true)
    
    -- close_text
    --"You wait until it's dark and the museum is closed."
    close_text, w, h = centered_string_game_close("You wait until it's dark and the museum is closed.", text_line, text_fill)
    E.set_entity_alpha(close_text, 0.0) -- hidden
    E.set_entity_flags(close_text, E.entity_flag_hidden, true)
    E.set_entity_scale(close_text, 0.065, 0.065, 1)
    E.set_entity_position(close_text, -0.6, 0, 0)
    E.parent_entity(close_text, text)

    -- close1_text
    --"Ok it's now or never, no one's around."
    close1_text, w, h = centered_string_game_close("Ok it's now or never, no one's around.", text_line, text_fill)
    E.set_entity_alpha(close1_text, 0.0) -- hidden
    E.set_entity_flags(close1_text, E.entity_flag_hidden, true)
    E.set_entity_scale(close1_text, 0.065, 0.065, 1)
    E.set_entity_position(close1_text, -0.45, -h + 0.3, 0)
    E.parent_entity(close1_text, text)
    
    -- close2_text
    --"\"I hope this is it.\""
    close2_text, w, h = centered_string_game_close("\"I hope this is it.\"", text_line, text_fill)
    E.set_entity_alpha(close2_text, 0.0) -- hidden
    E.set_entity_flags(close2_text, E.entity_flag_hidden, true)
    E.set_entity_scale(close2_text, 0.065, 0.065, 1)
    E.set_entity_position(close2_text, 0 , -h + 0.50, 0)
    E.parent_entity(close2_text, text)

    -- closeWIN_text
    --"Beauty. Nice selection. Your black market connection"
    --"hooked you up and then some. Chillin' Caribbean style"
    --"is what you're all about now. You stole the painting."
    --"You got the cash. All you have to do now is order"
    --"another Strawberry Daquiri - Oh and maybe swing by the"
    --"local art museum and use some of your hard-earned art"
    --"knowledge for a little art appreciation."
    closeWIN_text1, w, h = centered_string_game_close("Beauty. Nice selection. Your black market connection", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text1, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text1, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text1, -0.7 , -h + 0.5, 0)
    E.parent_entity(closeWIN_text1, text)

    closeWIN_text2, w, h = centered_string_game_close("hooked you up and then some. Chillin' Caribbean style", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text2, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text2, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text2, -0.7 , -h + 0.45, 0)
    E.parent_entity(closeWIN_text2, text)

    closeWIN_text3, w, h = centered_string_game_close("is what you're all about now. You stole the painting.", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text3, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text3, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text3, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text3, -0.7 , -h + 0.4, 0)
    E.parent_entity(closeWIN_text3, text)

    closeWIN_text4, w, h = centered_string_game_close("You got the cash. All you have to do now is order", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text4, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text4, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text4, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text4, -0.7 , -h + 0.35, 0)
    E.parent_entity(closeWIN_text4, text)

    closeWIN_text5, w, h = centered_string_game_close("another Strawberry Daquiri - Oh and maybe swing by the", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text5, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text5, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text5, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text5, -0.7 , -h + 0.3, 0)
    E.parent_entity(closeWIN_text5, text)

    closeWIN_text6, w, h = centered_string_game_close("local art museum and use some of your hard-earned art", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text6, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text6, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text6, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text6, -0.7 , -h + 0.25, 0)
    E.parent_entity(closeWIN_text6, text)

    closeWIN_text7, w, h = centered_string_game_close("knowledge for a little art appreciation.", text_line, text_fill)
    E.set_entity_alpha(closeWIN_text7, 0.0) -- hidden
    E.set_entity_flags(closeWIN_text7, E.entity_flag_hidden, true)
    E.set_entity_scale(closeWIN_text7, 0.065, 0.065, 1)
    E.set_entity_position(closeWIN_text7, -0.7 , -h + 0.2, 0)
    E.parent_entity(closeWIN_text7, text)

    -- closeLOSE_text
    --"You dumb schmuck. You stole the wrong painting."
    --"Your black market connection blew you off. Now the"
    --"only secluded island you're going to is Alcatraz."
    closeLOSE_text1, w, h = centered_string_game_close("You dumb schmuck. You stole the wrong painting.", text_line, text_fill)
    E.set_entity_alpha(closeLOSE_text1, 0.0) -- hidden
    E.set_entity_flags(closeLOSE_text1, E.entity_flag_hidden, true)
    E.set_entity_scale(closeLOSE_text1, 0.065, 0.065, 1)
    E.set_entity_position(closeLOSE_text1, -0.67, -h + 0.45, 0)
    E.parent_entity(closeLOSE_text1, text)

    closeLOSE_text2, w, h = centered_string_game_close("Your black market connection blew you off. Now the", text_line, text_fill)
    E.set_entity_alpha(closeLOSE_text2, 0.0) -- hidden
    E.set_entity_flags(closeLOSE_text2, E.entity_flag_hidden, true)
    E.set_entity_scale(closeLOSE_text2, 0.065, 0.065, 1)
    E.set_entity_position(closeLOSE_text2, -0.67, -h + 0.40, 0)
    E.parent_entity(closeLOSE_text2, text)
    
    closeLOSE_text3, w, h = centered_string_game_close("only secluded island you're going to is Alcatraz.", text_line, text_fill)
    E.set_entity_alpha(closeLOSE_text3, 0.0) -- hidden
    E.set_entity_flags(closeLOSE_text3, E.entity_flag_hidden, true)
    E.set_entity_scale(closeLOSE_text3, 0.065, 0.065, 1)
    E.set_entity_position(closeLOSE_text3, -0.67, -h + 0.35, 0)
    E.parent_entity(closeLOSE_text3, text)
    
    the_end, w, h = centered_string_game_close("The End", text_line, text_fill)
    E.set_entity_alpha(the_end, 0.0) -- hidden
    E.set_entity_flags(the_end, E.entity_flag_hidden, true)
    E.set_entity_scale(the_end, 0.085, 0.085, 1)
    E.set_entity_position(the_end, -0.1, 0, 0)
    E.parent_entity(the_end, text)

    press_enter, w, h = centered_string_game_close("Press Enter to Continue", text_line, text_fill)
    E.set_entity_alpha(press_enter, 0.0) -- hidden
    E.set_entity_scale(press_enter, 0.045, 0.045, 1)
    E.set_entity_position(press_enter, (w + 0.50)/4, -h + 0.05, 0)
    E.parent_entity(press_enter, text)
    
    game_made, w, h = centered_string_game_close("Game Made By:", text_line, text_fill)
    E.set_entity_alpha(game_made, 0.0) -- hidden
    E.set_entity_flags(game_made, E.entity_flag_hidden, true)
    E.set_entity_scale(game_made, 0.065, 0.065, 1)
    E.set_entity_position(game_made, (-w + 0.05), h - 0.05, 0)
    E.parent_entity(game_made, text)

    ben_dombek, w, h = centered_string_game_close("Ben Dombek", text_line, text_fill)
    E.set_entity_alpha(ben_dombek, 0.0) -- hidden
    E.set_entity_flags(ben_dombek, E.entity_flag_hidden, true)
    E.set_entity_scale(ben_dombek, 0.065, 0.065, 1)
    E.set_entity_position(ben_dombek, (-w + 0.06), h - 0.15, 0)
    E.parent_entity(ben_dombek, text)

    jason_gorski, w, h = centered_string_game_close("Jason Gorski", text_line, text_fill)
    E.set_entity_alpha(jason_gorski, 0.0) -- hidden
    E.set_entity_flags(jason_gorski, E.entity_flag_hidden, true)
    E.set_entity_scale(jason_gorski, 0.065, 0.065, 1)
    E.set_entity_position(jason_gorski, (-w + 0.06), h - 0.25, 0)
    E.parent_entity(jason_gorski, text)

    jon_kinkley, w, h = centered_string_game_close("Jon Kinkley", text_line, text_fill)
    E.set_entity_alpha(jon_kinkley, 0.0) -- hidden
    E.set_entity_flags(jon_kinkley, E.entity_flag_hidden, true)
    E.set_entity_scale(jon_kinkley, 0.065, 0.065, 1)
    E.set_entity_position(jon_kinkley, (-w + 0.06), h - 0.35, 0)
    E.parent_entity(jon_kinkley, text)
    
    --E.play_sound(close_cricket)
    E.loop_sound(close_cricket)
    E.set_sound_amplitude(close_cricket, 0.5)
    
    game_state = game_closing
end

init_game_closing()