# Hurry Curry! - a game about cooking
# Copyright (C) 2026 Hurry Curry! Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
extends MarginContainer
class_name SmartMarginContainer
# A smart margin container which automatically adjusts the margin such that it doesn't cover notch on phones

func _ready() -> void:
	var os := OS.get_name()
	if os == "iOS" or os == "Android":
		update_margins()

func update_margins() -> void:
	var screen_safe_rect := Rect2(DisplayServer.get_display_safe_area())
	var viewport_transform := get_viewport().get_final_transform()
	var viewport_safe_rect := screen_safe_rect * viewport_transform.affine_inverse()
	var viewport_full_rect := get_viewport().get_visible_rect()
	var left := viewport_safe_rect.position.x - viewport_full_rect.position.x
	var top := viewport_safe_rect.position.y - viewport_full_rect.position.y
	var right := viewport_full_rect.end.x - viewport_safe_rect.end.x
	var bottom := viewport_full_rect.end.y - viewport_safe_rect.end.y
	# print("OLD: ", get_theme_constant("margin_left"))
	begin_bulk_theme_override()
	add_theme_constant_override('margin_left', roundi(left) + get_theme_constant("margin_left"))
	add_theme_constant_override('margin_top', roundi(top) + get_theme_constant("margin_top"))
	add_theme_constant_override('margin_right', roundi(right) + get_theme_constant("margin_right"))
	add_theme_constant_override('margin_bottom', roundi(bottom) + get_theme_constant("margin_bottom"))
	end_bulk_theme_override()
	# print("NEW: ", get_theme_constant("margin_left"))
