local CURRENT_ANCHOR, CURRENT_TIMER = "", "";
local ANCHOR_FRAMES = {};

local optionsTable = {
    { -- section 1
        { -- column 1
            {
                title = "Enabled",
                type = "checkButton",
                tooltipText = "Enables or disables the addon.",
                setting = "enabled",
                simple = true,
            },
            {
                title = "Announce",
                type = "checkButton",
                tooltipText = "If unchecked, attempting to announce a timer will result in no action.",
                setting = "isAnnouncing",
                simple = true,
            },
            {
                title = "Timer Setting",
                type = "dropDown",
                tooltipText = "Select the timer setting you want to apply to the timers.",
                func = function()
                    local setting = "standardSetting";
                    local info;
                    for name, settings in pairs(TimerLib:Get("timerSettings")) do
                        info = UIDropDownMenu_CreateInfo();
                        info.text = name;
                        info.checked = name == Cooldowns:Get(setting);
                        info.func = function() 
                            Cooldowns:Set(setting, name);
                            CloseDropDownMenus(); 
                        end
                        UIDropDownMenu_AddButton(info);
                    end
                    
                    info = UIDropDownMenu_CreateInfo();
                    info.text = "None";
                    info.tooltipTitle = "Description";
                    info.tooltipText = "If you select 'None', the default for the anchor will be used instead.";
                    info.checked = Cooldowns:Get(setting) == nil;
                    info.func = function() 
                        Cooldowns:Set(setting, nil);
                        CloseDropDownMenus(); 
                    end
                    UIDropDownMenu_AddButton(info);
                end,
            },
        },
        { -- column 2
            {
                title = "Key Bindings",
                type = "dropDown",
                tooltipText = "Allows you to change the key bindings for various timer-related functions.",
                func = function() Cooldowns:KBDropDown() end,
            },
            {
                title = "Min Duration",
                type = "slider",
                tooltipText = "Sets the minimum duration, in seconds, that a cooldown must have in order to track it.  Zero means no restriction.",
                minValue = 0, 
                maxValue = 20,
                valueStep = 1,
                setting = "minCooldown",
                simple = true,
            },
            {
                title = "Max Duration",
                type = "slider",
                tooltipText = "Sets the maximum duration, in minutes, that a cooldown can have in order to track it.  Zero means no restriction.",
                minValue = 0, 
                maxValue = 30,
                valueStep = 1,
                setting = "maxCooldown",
                simple = true,
            },
        },
        { -- column 3
            {
                title = "Standard Announce",
                type = "editBoxString",
                tooltipText = [[Sets the message that will be sent when you announce a timer.  You can use the following replacements:

    %s: Name of the timer
    %d: Time remaining]]
                ,
                setting = "standardAnnounce",
                simple = true,
            },
            {
                title = "Display Name",
                type = "editBoxString",
                tooltipText = "Sets the text to be displayed as the name of the group.  If you set this to a value another group is also using (in the same anchor), the two groups will merge into one.",
                setting = "displayName",
            },
            {
                title = "Blocked Timers",
                type = "editBoxDropDown",
                tooltipText = "Add here the names of timers you do not wish to see onscreen.  The timer name much match exactly (case insensitive) to be blocked.",
                setting = "blocked",
                simple = true,
            },
        },
    },
    { -- section 2
        { -- column 1
            {
                type = "text",
                text = "Communication:",
                small = true,
            },
        }
    },
    { -- section 3
        { -- column 1
            {
                title = "Sending",
                type = "checkButton",
                tooltipText = "If unchecked, you will not communicate your cooldowns.",
                setting = "sending",
                module = CooldownsComm,
            },
            {
                title = "Timer Setting",
                type = "dropDown",
                tooltipText = "Select the timer setting you want to apply to communicated timers.",
                func = function()
                    local info;
                    for name, settings in pairs(TimerLib:Get("timerSettings")) do
                        info = UIDropDownMenu_CreateInfo();
                        info.text = name;
                        info.checked = name == CooldownsComm:Get("timerSetting");
                        info.func = function() 
                            CooldownsComm:Set("timerSetting", name);
                            CloseDropDownMenus(); 
                        end
                        UIDropDownMenu_AddButton(info);
                    end
                    
                    info = UIDropDownMenu_CreateInfo();
                    info.text = "None";
                    info.tooltipTitle = "Description";
                    info.tooltipText = "If you select 'None', the default for the anchor will be used instead.";
                    info.checked = CooldownsComm:Get("timerSetting") == nil;
                    info.func = function() 
                        CooldownsComm:Set("timerSetting", nil);
                        CloseDropDownMenus(); 
                    end
                    UIDropDownMenu_AddButton(info);
                end,
            },
        },
        { -- column 2
            {
                title = "Receiving",
                type = "editBoxDropDown",
                tooltipText = "Add here the names of cooldowns you want to receive from others.  The timer name much match exactly (case insensitive).",
                setting = "receiving",
                module = CooldownsComm,
            },
            {
                title = "Standard Announce",
                type = "editBoxString",
                tooltipText = [[Sets the message that will be sent when you announce a communicated timer.  You can use the following replacements:

    %s: Name of the timer
    %d: Time remaining
    %p: Person who sent it]]
                ,
                setting = "standardAnnounce",
                module = CooldownsComm,
            },
        },
    },
    { -- section 4
        { -- column 1
            { -- row 1
                {
                    type = "text",
                    text = "Select Anchor to Customize: ",
                },
                {
                    title = "(Select from DropDown Menu)",
                    type = "dropDown",
                    tooltipText = "Select the anchor you want to customize.",
                    func = function(self)
                        local info;
                        for name, settings in pairs(TimerLib:Get("anchorSettings")) do
                            if name ~= "Default Anchor" and name ~= "DoTimer Mouseover" and name ~= "Notifications Anchor" then
                                info = UIDropDownMenu_CreateInfo();
                                info.text = name;
                                info.checked = name == CURRENT_ANCHOR;
                                info.func = function() 
                                    CURRENT_ANCHOR = name;
                                    getglobal(self:GetParent():GetName() .. "Text"):SetText(name);
                                    for frame in pairs(ANCHOR_FRAMES) do
                                        frame:Update();
                                    end
                                    CloseDropDownMenus(); 
                                end
                                UIDropDownMenu_AddButton(info);
                            end
                        end
                    end,
                    scripts = {
                        OnShow = function(self)
                            for name, settings in pairs(Cooldowns:Get("userAnchors")) do
                                if settings.default then
                                    CURRENT_ANCHOR = name;
                                    getglobal(self:GetName() .. "Text"):SetText(name);
                                    for frame in pairs(ANCHOR_FRAMES) do
                                        frame:Update();
                                    end
                                end
                            end
                        end,
                    },
                },
            },
        },
    },
    { -- section 3
        { -- column 1
            {
                title = "Make this the default anchor",
                type = "checkButton",
                tooltipText = "If checked, Cooldowns will be moved to this anchor.",
                scripts = {
                    OnShow = function(self)
                        if not self.loaded then
                            self.loaded = true;
                            self.Update = function(self)
                                self:SetChecked(Cooldowns:Get("userAnchors", CURRENT_ANCHOR, "default"));
                            end
                            ANCHOR_FRAMES[self] = true;
                        end
                        self:Update();
                    end,
                    OnClick = function(self)
                        if self:GetChecked() then
                            for name, settings in pairs(Cooldowns:Get("userAnchors")) do
                                if name ~= CURRENT_ANCHOR then settings.default = nil; end
                            end
                            Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "default", 1);
                        else
                            self:SetChecked(true);
                            AsheylaLib:AlertUser("You cannot uncheck this setting.  You must check it in another anchor to change it.");
                        end
                    end,
                },
            },
            {
                title = "Move timers from Communication here",
                type = "checkButton",
                tooltipText = "If checked, cooldowns from Communications will be moved into this anchor.",
                scripts = {
                    OnShow = function(self)
                        if not self.loaded then
                            self.loaded = true;
                            self.Update = function(self)
                                self:SetChecked(Cooldowns:Get("userAnchors", CURRENT_ANCHOR, "externalLoc"));
                            end
                            ANCHOR_FRAMES[self] = true;
                        end
                        self:Update();
                    end,
                    OnClick = function(self)
                        if self:GetChecked() then
                            for name, settings in pairs(Cooldowns:Get("userAnchors")) do
                                if name ~= CURRENT_ANCHOR then settings.externalLoc = nil; end
                            end
                            Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "externalLoc", 1);
                        else
                            Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "externalLoc", nil);
                        end
                    end,
                },
            },
            { -- row
                {
                    type = "text",
                    small = true,
                    text = "Move these timers:",
                },
                {
                    title = "<Timer Name>",
                    type = "editBoxDropDown",
                    tooltipText = "Type in here the names of timers you want moved to this anchor.  The timer name must match only a fragment (case insensitive) to apply.",
                    scripts = {
                		OnEnterPressed = function(self) 
                			local text = self:GetText();
                			Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "timers", string.lower(text), 1);
                            self:ClearFocus();
                		end,
                    },
                    func = function()
                        local timers = Cooldowns:Get("userAnchors", CURRENT_ANCHOR, "timers");
                        if timers then
                            local info;
                            for name in pairs(timers) do
                                info = UIDropDownMenu_CreateInfo();
                                info.text = name;
                                info.notCheckable = 1;
                                info.func = function() 
                                    Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "timers", name, nil);
                                    CloseDropDownMenus(); 
                                end
                                UIDropDownMenu_AddButton(info);
                            end
                        end
                    end,
                },
                {
                    type = "text",
                    small = true,
                    text = "here.",
                },
            },
            { -- row
                {
                    type = "text",
                    small = true,
                    text = "Apply this timer setting:",
                },
                {
                    title = "",
                    type = "dropDown",
                    tooltipText = "Select the timer setting you want to apply to specific timers.",
                    func = function()
                        local info;
                        for name, settings in pairs(TimerLib:Get("timerSettings")) do
                            info = UIDropDownMenu_CreateInfo();
                            info.text = name;
                            info.checked = name == CURRENT_TIMER;
                            info.func = function() 
                                CURRENT_TIMER = name;
                                CloseDropDownMenus(); 
                            end
                            UIDropDownMenu_AddButton(info);
                        end
                    end,
                    scripts = {
                        OnShow = function(self)
                            CURRENT_TIMER = "Default Timer";
                        end,
                    },
                },
                {
                    type = "text",
                    small = true,
                    text = "to these timers:",
                },
                {
                    title = "<Timer Name>",
                    type = "editBoxDropDown",
                    tooltipText = "Type in here the names of timers you want to apply to the timer setting.  The timer name must match only a fragment (case insensitive) to apply.",
                    scripts = {
                		OnEnterPressed = function(self) 
                			local text = self:GetText();
                			Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "timerSettings", CURRENT_TIMER, string.lower(text), 1);
                            self:ClearFocus();
                		end,
                    },
                    func = function()
                        local timers = Cooldowns:Get("userAnchors", CURRENT_ANCHOR, "timerSettings", CURRENT_TIMER);
                        if timers then
                            local info;
                            for name in pairs(timers) do
                                info = UIDropDownMenu_CreateInfo();
                                info.text = name;
                                info.notCheckable = 1;
                                info.func = function() 
                                    Cooldowns:Set("userAnchors", CURRENT_ANCHOR, "timerSettings", CURRENT_TIMER, name, nil);
                                    CloseDropDownMenus(); 
                                end
                                UIDropDownMenu_AddButton(info);
                            end
                        end
                    end,
                },
            },
        },
    },
};

local function createCGUI()
    GUILib:SetGUIPanel("|c2fffffff|rCooldowns", "main", Cooldowns, optionsTable);
end
createCGUI();
GUILib:AddGUICreationScript(createCGUI);
