<Ui>
    <Frame name="Template_MenuItem" virtual="true">
        <Size>
            <AbsDimension x="32" y="32"/>
        </Size>
        <Backdrop>
            <BackgroundColor a="0.5" r="0" g="0" b="0"/>
            <EdgeColor       a="0.5" r="1" g="1" b="1"/>
            <EdgeSize>
                <AbsValue x="4"/>
            </EdgeSize>
        </Backdrop>
        <Layers>
            <Layer>
                <FontString name="$parentText" font="Fonts/Pixel.ttf" fontHeight="12" outline="NORMAL" setAllPoints="true"/>
            </Layer>
        </Layers>
    </Frame>
    
    <Frame name="ZoneManager" parent="UIParent" hidden="true">
        <Size>
            <AbsDimension x="200" y="112"/>
        </Size>
        <Anchors>
            <Anchor point="TOPLEFT"/>
        </Anchors>
        <Frames>
            <Frame name="$parentSelector" hidden="true">
	            <Backdrop>
	                <EdgeColor r="1" g="1" b="1"/>
	                <EdgeSize>
	                    <AbsValue x="4"/>
	                </EdgeSize>
	            </Backdrop>
            </Frame>
            <Frame name="$parentNew" inherits="Template_MenuItem">
                <Anchors>
                    <Anchor point="LEFT">
                        <Offset>
                            <AbsDimension x="4"/>
                        </Offset>
                    </Anchor>
                    <Anchor point="RIGHT">
                        <Offset>
                            <AbsDimension x="-4"/>
                        </Offset>
                    </Anchor>
                    <Anchor point="TOP">
                        <Offset>
                            <AbsDimension y="4"/>
                        </Offset>
                    </Anchor>
                </Anchors>
            </Frame>
            <Frame name="$parentOpen" inherits="Template_MenuItem">
                <Anchors>
                    <Anchor point="LEFT"  relativeTo="$parentNew"/>
                    <Anchor point="RIGHT" relativeTo="$parentNew"/>
                    <Anchor point="TOP"   relativeTo="$parentNew" relativePoint="BOTTOM">
                        <Offset>
                            <AbsDimension y="4"/>
                        </Offset>
                    </Anchor>
                </Anchors>
            </Frame>
            <Frame name="$parentSave" inherits="Template_MenuItem">
                <Anchors>
                    <Anchor point="LEFT"  relativeTo="$parentOpen"/>
                    <Anchor point="RIGHT" relativeTo="$parentOpen"/>
                    <Anchor point="TOP"   relativeTo="$parentOpen" relativePoint="BOTTOM">
                        <Offset>
                            <AbsDimension y="4"/>
                        </Offset>
                    </Anchor>
                </Anchors>
            </Frame>
        </Frames>
    </Frame>
    
    <Frame name="FileDialog" parent="UIParent" topLevel="true" hidden="true" frameStrata="TOOLTIP">
        <Size>
            <AbsDimension x="250" y="70"/>
        </Size>
        <Anchors>
            <Anchor point="CENTER"/>
        </Anchors>
        <Backdrop>
            <BackgroundColor r="0" g="0" b="0" a="0.5"/>
            <EdgeColor       r="1" g="1" b="1"/>
            <EdgeSize>
                <AbsValue x="4"/>
            </EdgeSize>
        </Backdrop>
        <Frames>
            <Frame name="$parentTitle">
                <Size>
                    <AbsDimension x="20" y="20"/>
                </Size>
                <Anchors>
                    <Anchor point="LEFT"/>
                    <Anchor point="RIGHT"/>
                    <Anchor point="TOP"/>
                </Anchors>
                <Backdrop>
                    <BackgroundColor r="0.5" g="0.5" b="0.5" a="0.5"/>
                    <BackgroundInsets>
                        <AbsInset left="4" right="4" top="4" bottom="-4"/>
                    </BackgroundInsets>
                </Backdrop>
                <Layers>
                    <Layer>
                        <FontString name="$parentText" font="Fonts/Pixel.ttf" fontHeight="12" outline="NORMAL" setAllPoints="true"/>
                    </Layer>
                </Layers>
            </Frame>
            <EditBox name="$parentEditBox" autoFocus="true">
                <Anchors>
                    <Anchor point="LEFT">
                        <Offset>
                            <AbsDimension x="12"/>
                        </Offset>
                    </Anchor>
                    <Anchor point="RIGHT">
                        <Offset>
                            <AbsDimension x="-12"/>
                        </Offset>
                    </Anchor>
                    <Anchor point="TOP" relativeTo="$parentTitle" relativePoint="BOTTOM">
                        <Offset>
                            <AbsDimension y="12"/>
                        </Offset>
                    </Anchor>
                    <Anchor point="BOTTOM">
                        <Offset>
                            <AbsDimension y="-12"/>
                        </Offset>
                    </Anchor>
                </Anchors>
                <Backdrop>
                    <EdgeColor r="1" g="1" b="1"/>
                    <EdgeSize>
                        <AbsValue x="1"/>
                    </EdgeSize>
                </Backdrop>
                <FontString font="Fonts/Pixel.ttf" fontHeight="12" justifyH="LEFT" justifyV="MIDDLE"/>
                <TextInsets left="4" right="4" top="2" bottom="2"/>
                <Scripts>
                    <OnEscapePressed>
                        this:ClearFocus();
                    </OnEscapePressed>
                    <OnEnterPressed>
                        if (FileDialog.func) then
                            FileDialog.func(this:GetText());
                        end
                        
                        this:SetText("");
                        
                        FileDialog:Hide();
                    </OnEnterPressed>
                </Scripts>
            </EditBox>
        </Frames>
        <Scripts>
            <OnLoad>
                this.func = nil;
                this.title = "";
            </OnLoad>
            <OnShow>
                this:Raise();
                this.Title.Text:SetText(this.title);
                this.EditBox:SetFocus();
            </OnShow>
        </Scripts>
    </Frame>
    
    <Frame name="ZoneManager_Input" parent="UIParent" enableKeyboard="true">
        <Scripts>
            <OnLoad>
                ZoneManager.selected = -1;
                ZoneManager.New.Text:SetText("New zone");
                ZoneManager.Open.Text:SetText("Open zone");
                ZoneManager.Save.Text:SetText("Save zone");
            </OnLoad>
            <OnKeyUp>
                if (arg1 == KEY_F1) then
                    if (not ZoneManager:IsShown()) then
                        ZoneManager:Show();
                    else
                        ZoneManager.selected = ZoneManager.selected + 1;
                        if (ZoneManager.selected == 3) then
                            ZoneManager.selected = -1;
                            ZoneManager:Hide();
                            
                            ZoneManager.Selector:Hide();
                            
                            ZoneManager.selectedItem:SetBackdropColor(0,0,0,0.5);
                            ZoneManager.selectedItem = nil;
                            
                            return;
                        end
                        
                        if (ZoneManager.selectedItem) then
                            ZoneManager.selectedItem:SetBackdropColor(0,0,0,0.5);
                        end
                        
                        if (ZoneManager.selected == 0) then
                            ZoneManager.selectedItem = ZoneManager.New;
                        elseif (ZoneManager.selected == 1) then
                            ZoneManager.selectedItem = ZoneManager.Open;
                        else
                            ZoneManager.selectedItem = ZoneManager.Save;
                        end
                        
                        ZoneManager.selectedItem:SetBackdropColor(1,1,1,0.5);
                        
                        ZoneManager.Selector:SetAllPoints(ZoneManager.selectedItem);
                        ZoneManager.Selector:Show();   
                    end
                    
                elseif (arg1 == KEY_RETURN) then
                    if (ZoneManager:IsShown()) then
                        if (ZoneManager.selected == -1) then
                            return;
                            
                        elseif (ZoneManager.selected == 0) then
                            NewWorld();
                            
                            ZoneManager.selected = -1;
                            ZoneManager:Hide();
                            
                            ZoneManager.Selector:Hide();
                            
                            ZoneManager.selectedItem:SetBackdropColor(0,0,0,0.5);
                            ZoneManager.selectedItem = nil;
                            
                        elseif (ZoneManager.selected == 1) then
                            FileDialog.func = OpenWorld;
                            FileDialog.title = "Open world";
                            FileDialog:Show();
                            
                            ZoneManager.selected = -1;
                            ZoneManager:Hide();
                            
                            ZoneManager.Selector:Hide();
                            
                            ZoneManager.selectedItem:SetBackdropColor(0,0,0,0.5);
                            ZoneManager.selectedItem = nil;
                            
                        else
                            FileDialog.func = SaveWorld;
                            FileDialog.title = "Save world";
                            FileDialog:Show();
                            
                            ZoneManager.selected = -1;
                            ZoneManager:Hide();
                            
                            ZoneManager.Selector:Hide();
                            
                            ZoneManager.selectedItem:SetBackdropColor(0,0,0,0.5);
                            ZoneManager.selectedItem = nil;
                        end
                    end
                end
            </OnKeyUp>
        </Scripts>
    </Frame>
</Ui>
