CTagsUI = ISCollapsableWindow:derive("CTagsUI");

function CTagsUI:initialise()
	ISCollapsableWindow.initialise(self)
end

function CTagsUI:createChildren()
	ISCollapsableWindow.createChildren(self);
	local titleH = self:titleBarHeight();
	local inputH = 28;

	self.textField = ISTextBox:new(0.001, titleH, self.width, inputH, "", self.text);
	self.textField.anchorRight = true;
	self.textField.anchorLeft = false;
	self.textField:initialise();
	self.textField:enableColorPicker();
	self.textField.yes:setVisible(false);
	self.textField.no:setVisible(false);
	self.textField:onPickedColor(self.color);
	self.textField:setVisible(true);
	self:addChild(self.textField);

	self.applyButton = ISButton:new(0, titleH + inputH, self.width, self.height - (inputH + titleH), "Apply", self, function () CTagsUI:apply(self) end);
	self.applyButton.anchorRight = true;
	self.applyButton.anchorLeft = false;
	self.applyButton:initialise();
	self.applyButton.borderColor.a = 0.0;
	self.applyButton.backgroundColor.a = 1;
	self.applyButton.backgroundColorMouseOver.a = 0.5;
	self.applyButton:setVisible(true);
	self:addChild(self.applyButton);
end

function CTagsUI:loop() 

	-- Loop
	CTags:sleepDo(0.1, CTagsUI.loop);
end

function CTagsUI:apply(ui)
	local square = ui.square;
	local squareID = ui.squareID;
	local textField = ui.textField;
	local text = textField.entry:getText();
	local color = textField.colorBtn.backgroundColor;
	local tagData = ModData.getOrCreate("ContainerTagData");
	if not tagData.tags then tagData.tags = {} end;
	local tags = tagData.tags;

	if ui.maxChars and textField.entry:getInternalText():len() > ui.maxChars then
		ui.applyButton.tooltip = getText("IGUI_TextBox_TooManyChar", ui.maxChars);
		return
	end

	if textField.entry:getInternalText():len() == 0 then
		tagData.tags[squareID] = {};
	else 
		tagData.tags[squareID] = { ['square'] = {square:getX(), square:getY(), square:getZ()}, ['tag'] = text, ['color'] = {color.r, color.g, color.b} };
	end
	
	CTagsUI:removeWindow();
end 

-- Entry point
function CTagsUI.makeWindow(x, y, square, squareID)
	-- Don't store it, regenerate completely
	if CTagsUI.window then CTagsUI:removeWindow() end

	local window;
	local sw = getCore():getScreenWidth();
	local sh = getCore():getScreenHeight();
	local Hmax = 1;
	local Wmax = 1; 
	local w = (sw / 5) * Wmax <= sw and (sw / 5) * Wmax or sw
	local h = (sh / 12) * Wmax <= sh and (sh / 12) * Hmax or sh

	window = CTagsUI:new(x, y, w, h, square, squareID);
	window:setVisible(true)
	window:setResizable(false);
	window:addToUIManager()
	window.pin = true;
	CTagsUI.window = window;

	-- Conditionals
	CTags:sleepDo(0, CTagsUI.loop);
end

function CTagsUI.removeWindow()
	local window = CTagsUI.window;
	if not window then return end;
	window:setVisible(false)
	window:removeFromUIManager()
end

function CTagsUI:new(x, y, width, height, square, squareID)
	local o = {};
	o = ISCollapsableWindow:new(x, y, width, height);
	setmetatable(o, self);
	self.__index = self;
	o.title = "Container Tags";
	o.x = x
	o.y = y
	o.width = width
	o.height = height
	o.square = square
	o.squareID = squareID
	o.clearStentil = false;
	o.maxChars = 32;

	-- Find data
	local tagData = ModData.getOrCreate("ContainerTagData");
	if not tagData.tags then tagData.tags = {} end;
	local tags = tagData.tags;

	if tags[squareID] then
		local tag = tags[squareID];
		local text = tag.tag or "";
		local color = tag.color or { 1, 1, 1 };
		o.color = { r = color[1], g = color[2], b = color[3]};
		o.text = text
	else
		o.color = { r = 1, g = 1, b = 1 }
		o.text = "";
	end 

	return o;
end
