print("init WinKeyDialog begin");

pi = 3.1415926535897932384626433832795

-- animation const's
local fadeInDelay = .2
local fadeInTime = 0.1
local fadeOutTime = 0.001

local fadeInBG = 0.1
local fadeOutBG = 0.1
local fadeInBtnsDelay = 0.1
local fadeInBtns = 0.2
local fadeOutBtns = 0.2


local menu = game:getSceneManager():getWinKeyDialog()
local vault = resourceManager:getVault(ResourceManager.COMMON)


-- Создание бакграунда
-- Должен добавляться раньше всех. Ибо в противном случае он накрывает
-- остальные контролы и не дает им мышь


windowLeft = 1024 / 2 - 598 / 2;
windowTop = 768 / 2 - 284 / 2 - 60;


-- menu:getRoot():SetPos(windowLeft, windowTop, -1)

-- local sprite = KrSprite(vault:GetSpriteResource("hatch"))
local sprite = KrSprite(vault:GetSpriteResource("hatch_black"))
setNodeScale(sprite, 256, 192);
-- aniFadeInFadeOut(sprite,0,.3,.3,60)
aniFadeInFadeOut(sprite,0,.3,.3,127)
menu:setBackground(sprite)

--[[
local sprite = KrSprite(vault:GetSpriteResource("hatch"))
sprite:SetColor(KrColorTransform(KrRGBA(0, 0, 0, 96)), -1);
-- aniFadeInFadeOut(sprite,0,.3,.2,96)
-- sprite:SetPos(-windowLeft, -windowTop, -1)
menu:setBackground(sprite)
menu:setBackgroundSize(game:screenWidth(), game:screenHeight())
]]--


local windowBack = KrSprite(vault:GetSpriteResource("hatch_black"));

local pageBG = KrSprite(vault:GetSpriteResource("Pergament_XL"))
setNodeScale(pageBG, 597 / 344, 284 / 220)
addNodeAdopt(pageBG, windowBack)


-- aniPoolMesageWindow(windowBack);
aniFadeInFadeOut(windowBack,0,fadeInBG,fadeOutBG,255)
--aniPoolMesageWindow(windowBack, nil,nil,nil,6,4,900,900);

windowBack:SetPos(windowLeft, windowTop, -1)
-- menu:setImageFrame(windowBack)
menu:setImageFrame(windowBack)

local startBorderX = 0;
local startBorderY = 0;

local standB = KrSprite(vault:GetSpriteResource("mbox_lu"))
standB:SetPos(startBorderX, startBorderY, -1);
addNodeAdopt(standB, windowBack)

startBorderX = startBorderX + 44;

for i = 0, 2 do

	standB = KrSprite(vault:GetSpriteResource("mbox_u"))
	standB:SetPos(startBorderX, startBorderY, -1);
	addNodeAdopt(standB, windowBack)

	startBorderX = startBorderX + 64;

end

standB = KrSprite(vault:GetSpriteResource("mbox_u_gem"))
standB:SetPos(startBorderX, startBorderY, -1);
addNodeAdopt(standB, windowBack)


startBorderX = startBorderX + 128;

for i = 0, 2 do

	standB = KrSprite(vault:GetSpriteResource("mbox_u"))
	standB:SetPos(startBorderX, startBorderY, -1);
	addNodeAdopt(standB, windowBack)

	startBorderX = startBorderX + 64;

end

standB = KrSprite(vault:GetSpriteResource("mbox_ru"))
standB:SetPos(startBorderX, startBorderY, -1);
addNodeAdopt(standB, windowBack)


startBorderX = startBorderX + 41;
startBorderY = startBorderY + 46;

for i = 0, 2 do
	standB = KrSprite(vault:GetSpriteResource("mbox_r"))
	standB:SetPos(startBorderX, startBorderY, -1);
	addNodeAdopt(standB, windowBack)

	startBorderY = startBorderY + 64;
end

standB = KrSprite(vault:GetSpriteResource("mbox_rd"))
standB:SetPos(startBorderX, startBorderY, -1);
addNodeAdopt(standB, windowBack)

startBorderX = startBorderX - 41 - 128;
startBorderY = startBorderY + 46;

for i = 0, 3 do

	standB = KrSprite(vault:GetSpriteResource("mbox_d"))
	standB:SetPos(startBorderX, startBorderY, -1);
	addNodeAdopt(standB, windowBack)


	startBorderX = startBorderX - 128;

end

startBorderX = startBorderX + 128 - 44;
startBorderY = startBorderY - 46;

standB = KrSprite(vault:GetSpriteResource("mbox_ld"))
standB:SetPos(startBorderX, startBorderY, -1);
addNodeAdopt(standB, windowBack)

startBorderY = startBorderY - 64;

for i = 0, 2 do
	standB = KrSprite(vault:GetSpriteResource("mbox_l"))
	standB:SetPos(startBorderX, startBorderY, -1);
	addNodeAdopt(standB, windowBack)

	startBorderY = startBorderY - 64;
end


-----------------------------------------------
print("Create left button");


	local button = XButton();

	local width = 97
	local height = 40
	local sensX = -48
	local sensY = -21
	local textY = sensY + 10;

	local rText = 245;
	local gText = 203;
	local bText = 99;
	local aText = 216;

	local rHText = 255;
	local gHText = 255;
	local bHText = 255;
	local aHText = 216;

	local btnShift = 66;
		
	initXBtn(button, "Brn_XX", windowLeft + 254 + 119, windowTop + 170 + 67, sensX, sensY, width, height, vault)
	initXButtonText(button, "BACK",
						sensX, textY, 
						width, height, 
						"occ24bg", KrTextBox.CENTER)

	button:setTextColor(rText, gText, bText, aText);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, rHText, gHText, bHText, aHText);
	button:setTextColorBType(XButton.NORMAL_PRESSED, rHText, gHText, bHText, aHText);

	aniFadeInFadeOut(button:getRoot(), fadeInBtnsDelay, fadeInBtns, fadeOutBtns)


	button:setAccelerator(XButton.ESCAPE);
	menu:setButton2(button);

-----------------------------------------------


print("Create right button");

	local button = XButton();

	initXBtn(button, "Brn_XX", windowLeft + 88 + 119, windowTop + 170 + 67, sensX, sensY, width, height, vault)
	initXButtonText(button, "OK",
						sensX, textY, 
						width, height, 
						"occ24bg", KrTextBox.CENTER)

	button:setTextColor(rText, gText, bText, aText);
	button:setTextColorBType(XButton.NORMAL_HIGHLIGHTED, rHText, gHText, bHText, aHText);
	button:setTextColorBType(XButton.NORMAL_PRESSED, rHText, gHText, bHText, aHText);

	aniFadeInFadeOut(button:getRoot(), fadeInBtnsDelay, fadeInBtns, fadeOutBtns)

	button:setAccelerator(XButton.ENTER);
	menu:setButton(button);


-- create and init textBox


local pageTextBox = KrTextBox(game:getFontResource("occ24b"), 577, 220, 4, KrTextBox.LEFT);
pageTextBox:SetTextMultiLineColored( getText("MESSAGES.WIN_MESSAGE_KEY") );
pageTextBox:SetPos(24, 20, -1);
addNodeAdopt(pageTextBox, windowBack)


local width = 280;
local height= 174;
local textBox = KrTextBox(game:getFontResource("occ24b"), 1, 1, 2, KrTextBox.CENTER);
aniFadeInFadeOut(textBox, fadeInBtnsDelay, fadeInBtns, fadeOutBtns)
textBox:SetPos(2024, 2024, -1);
-- setTextColor(textBox, 255, 249, 177, 255);
textBox:SetText(" ", 0)

local pageSpr = KrSprite(vault:GetSpriteResource("message_key"))
pageSpr:SetPos(701 - 2024, 318 - 2024, -1);
addNodeAdopt(pageSpr, textBox)


menu:setTextBox(textBox)


-- end crate text box
print("init Message Window end");



