
require "ISUI/ISPanel"

ISDisplayBarPresetsNamer = ISPanel:derive("ISDisplayBarPresetsNamer");

local FONT_HGT_SMALL = getTextManager():getFontHeight(UIFont.Small)
local FONT_HGT_MEDIUM = getTextManager():getFontHeight(UIFont.Medium)
local FONT_HGT_LARGE = getTextManager():getFontHeight(UIFont.Large)

--************************************************************************--
--** ISPanel:initialise
--**
--************************************************************************--


function ISDisplayBarPresetsNamer:initialise()
    ISPanel.initialise(self);
    self:create();
end


function ISDisplayBarPresetsNamer:setVisible(visible)
    --    self.parent:setVisible(visible);
    self.javaObject:setVisible(visible);
end

function ISDisplayBarPresetsNamer:render()
    local y = 10;
    
    local titleText = getText("ContextMenu_MinimalDisplayBars_Enter_Preset_Name") 
    self:drawText(
        titleText, 
        self.width/2 - (getTextManager():MeasureStringX(UIFont.Medium, titleText) / 2), 
        y, 
        1, 1, 1, 1, UIFont.Medium);
    
    y = y + 30;
    
    self:updateButtons();

end

function GenerateNewPresetName()
	local i=0
	local savename = ""
	repeat i=i+1
		local valid = true
		for i2,v in pairs(MinimalDisplayBars.PersistantData.PresetNames) do
			if v == "Preset "..i then
				valid = false
			end
		end
		if valid then savename = "Preset "..i end
	until valid or i > 25
	return savename
end

function ISDisplayBarPresetsNamer:create()
    local btnWid = 100
    local btnHgt = math.max(25, FONT_HGT_SMALL + 3 * 2)
    
    local xSpacing = 4
    local padBottom = 10
    
    local y = 10 + 25;
    
    local labelName = "Name" .. ": "
    
    ------------------------------------
    -- 
    self.textEntry = 
        ISTextEntryBoxMDB:new(
            "StringInput", 
            GenerateNewPresetName(), 
            25, 
            y, 
            self.width - 20, 
            FONT_HGT_SMALL + 4, 
            self.displayBar)
	self.textEntry.BypassNumValidate = true
    self.textEntry:initialise();
    self.textEntry:instantiate();
    --self.textEntry:setTooltip("x");
    self.textEntry:setOnlyNumbers(false);
    self:addChild(self.textEntry);
    
    --[[self.label = 
        ISLabel:new(
            self.textEntry:getX(), 
            y, 
            FONT_HGT_SMALL + 4, 
            "", 
            1, 
            1, 
            1, 
            1, 
            UIFont.Small, 
            true)
    self.label:setTranslation( labelName )
    self.label:setX(self.label:getX() - self.label:getWidth())
    self.label:initialise();
    self.label:instantiate();
    self:addChild(self.label);]]--
    y = y + self.textEntry:getHeight() + 40;
   
    self:setHeight(y)
    ------------------------------------
    
    local x = 0;
    
    self.cancelBtn = 
        ISButton:new(
            padBottom + x, 
            self:getHeight() - padBottom - btnHgt, 
            btnWid, 
            btnHgt, 
            getText("UI_btn_cancel"), 
            self, 
            ISDisplayBarPresetsNamer.onOptionMouseDown);
    self.cancelBtn.internal = "CANCEL";
    self.cancelBtn:initialise();
    self.cancelBtn:instantiate();
    self.cancelBtn.borderColor = self.buttonBorderColor;
    self:addChild(self.cancelBtn);
    x = x + xSpacing + btnWid;
    
    self.acceptBtn = 
        ISButton:new(
            padBottom + x, 
            self:getHeight() - padBottom - btnHgt, 
            btnWid, 
            btnHgt, 
            getText("UI_btn_accept"), 
            self, 
            ISDisplayBarPresetsNamer.onOptionMouseDown);
    self.acceptBtn.internal = "ACCEPT";
    self.acceptBtn:initialise();
    self.acceptBtn:instantiate();
    self.acceptBtn.borderColor = self.buttonBorderColor;
    self:addChild(self.acceptBtn);
    x = x + xSpacing + btnWid;
    
    --[[
    self.applyBtn = 
        ISButton:new(
            padBottom + btnWid*2 + x, 
            self:getHeight() - padBottom - btnHgt, 
            btnWid, 
            btnHgt, 
            getText("UI_btn_apply"), 
            self, 
            ISDisplayBarPresetsNamer.onOptionMouseDown);
    self.applyBtn.internal = "APPLY";
    self.applyBtn:initialise();
    self.applyBtn:instantiate();
    self.applyBtn.borderColor = self.buttonBorderColor;
    self:addChild(self.applyBtn);
    x = x + xSpacing + btnWid;
    --]]
    
    --
    local width = 0
    width = width + self.cancelBtn:getWidth()
    width = width + self.acceptBtn:getWidth()
    --width = width + self.applyBtn:getWidth()
    
    --[[
    for _,child in pairs(self:getChildren()) do
        width = math.max(width, child:getWidth())
    end
    for _,child in pairs(self:getChildren()) do
        child:setWidth(width)
    end
    
    --]]
    
    self:setWidth(10 + x + 10)
    self:setHeight(y)
    
    -- FORCE CENTER
    local core = getCore()
    self:setX( core:getScreenWidth()/2 - self:getWidth()/2 )
    self:setY( core:getScreenHeight()/2 - self:getHeight()/2 )
end

function ISDisplayBarPresetsNamer:updateButtons()
    
end

function ISDisplayBarPresetsNamer:onOptionMouseDown(button, x, y)
    if button.internal == "ACCEPT" then
        if self.onApply then 
            self.onApply() end
        if self.textEntry then
			MDBSavePreset(self.displayBar,self.textEntry:getText())
			--print(self.textEntry:getText())
			--print(self.displayBar.playerIndex)
		else
			print("Failed")
		end
        self:close()
        
    elseif button.internal == "APPLY" then
        if self.onApply then 
            self.onApply() end
        
        
    elseif button.internal == "RESET" then
        if self.onReset then 
            self.onReset() end
        
    elseif button.internal == "CANCEL" then
        if self.onCancel then 
            self.onCancel() end
        
        self:close()
    end
end

function ISDisplayBarPresetsNamer:close()
    self:setVisible(false)
    
    self:removeFromUIManager()
end

function ISDisplayBarPresetsNamer:new(x, y, displayBar)
    local o = {};
    local width, height = 200, 200
    
    o = ISPanel:new(x, y, width, height);
    setmetatable(o, self);
    self.__index = self;
    --self.player = player;
    o.variableColor={r=0.9, g=0.55, b=0.1, a=1};
    o.borderColor = {r=0.4, g=0.4, b=0.4, a=1};
    o.backgroundColor = {r=0, g=0, b=0, a=0.9};
    o.buttonBorderColor = {r=0.7, g=0.7, b=0.7, a=0.5};
    o.zOffsetSmallFont = 25;
    o.moveWithMouse = true;
    ISDisplayBarPresetsNamer.instance = o
    
    o.displayBar = displayBar
    
    if o.displayBar.parent then
        o.parentOldX = o.displayBar.parent:getX()
        o.parentOldY = o.displayBar.parent:getY()
    end
   
    
    return o;
end
