# 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/>.
#
class_name ItemMessage
extends Control

const PRINTED_MAT := preload("res://gui/resources/materials/printed_material.tres")

@export var enable_rotation := true
@export var enable_grayscale := false

var timeout_remaining := 0.
var timeout_initial := 0.

@onready var item_render: ItemRender = $VBoxContainer/SubViewportContainer/SubViewport/ItemRender
@onready var progress: ProgressBar = $VBoxContainer/ProgressBar
@onready var sub_viewport: SubViewport = $VBoxContainer/SubViewportContainer/SubViewport
@onready var sub_viewport_container: SubViewportContainer = $VBoxContainer/SubViewportContainer
@onready var v_box_container: VBoxContainer = $VBoxContainer

func _ready() -> void:
	Global.configure_viewport_aa(sub_viewport)
	
	if enable_grayscale:
		sub_viewport_container.material = PRINTED_MAT

func set_subviewport_size(sub_viewport_size: Vector2):
	sub_viewport.size = sub_viewport_size
	progress.custom_minimum_size.y = sub_viewport_size.x - sub_viewport_size.y
	v_box_container.size.y = 0

func set_round_corner_radius_progress(radius: int):
	var style_bg: StyleBoxFlat = progress.get_theme_stylebox("background")
	var style_fill: StyleBoxFlat = progress.get_theme_stylebox("fill")
	
	style_bg.corner_radius_bottom_left = radius
	style_bg.corner_radius_bottom_right = radius
	style_fill.corner_radius_bottom_left = radius
	style_fill.corner_radius_bottom_right = radius

func set_item(item_name: String, timeout_initial_: float, timeout_remaining_: float):
	item_render.set_item(item_name, enable_rotation)
	
	timeout_remaining = timeout_remaining_
	timeout_initial = timeout_initial_
	progress.max_value = timeout_initial
	progress.value = timeout_remaining

func remove_item():
	item_render.remove_item()

func _process(delta):
	if item_render.item != null and !Global.game_paused:
		timeout_remaining -= delta
		progress.value = timeout_remaining
		var x: float = timeout_remaining / timeout_initial
		var progress_style: StyleBoxFlat = progress.get_theme_stylebox("fill")
		progress_style.bg_color = Color(min((1-x) * 2, 1), min(x * 2, 1), 0.)
		progress.add_theme_stylebox_override("fill", progress_style)
