extends PopupPanel

var text1
var text2
var label
var unlock_font = preload("res://fonts/unlock_font.tres")
var unlock_font_small = preload("res://fonts/unlock_font_small.tres")
var centerPos
var dims
var textPos

func _init(text_param1, text_param2, centerPos, dims, textPos):
  text1 = text_param1
  text2 = text_param2
  self.centerPos = centerPos
  self.dims = dims
  self.textPos = textPos

func _ready():
  var strSize1 = unlock_font.get_string_size(text1 + "\n")
  var strSize2 = unlock_font_small.get_string_size(text2)
  var strSize = Vector2(max(strSize1.x, strSize2.x), strSize1.y + strSize2.y)

  if self.dims != null:
    set_size(self.dims) #Vector2(800, 856))
  else:
    set_size(1.5 * strSize) # strSize is buggy, for now specify panel properties by hand
  set_position(Vector2(centerPos) - get_size() / 2)
  var margin_size = 100
  label = RichTextLabel.new()
  label.bbcode_enabled = true
  label.append_bbcode('[font=res://fonts/unlock_font.tres]'
      + text1 + '\n'
      + '[/font]')
  label.append_bbcode('[font=res://fonts/unlock_font_small.tres]'
      + text2
      + '[/font]')
  label.push_font(unlock_font)
  label.set_size(self.get_rect().size)
  label.rect_position = (self.get_rect().size - strSize) / 2
  if self.textPos != null:
    label.rect_position = self.textPos

  add_child(label)
  show()

func _unhandled_key_input(event):
  if event.is_action("restart"):
    global.mutton = 0
    global.lambs = 5
    global.deaths = 0
    get_tree().reload_current_scene()
  if event.is_action("continueGame"):
    var player = $"/root/World/ys/player"    
    var global = get_node("/root/global")
    global.mutton = player.mutton
    global.lambs = max(player.lambs, 3)
    global.deaths += 1
    call_deferred("_deferred_reload_scene")
#    get_tree().reload_current_scene()

func _deferred_reload_scene():
  get_tree().reload_current_scene()
  
#  if event.is_action("setControls"):
#    get_tree().change_scene("res://ControlsMenu.tscn")

