local SECTION_NAME = "|c6fffffff|rTimerLib Timers";

local function createGUI(name)
    local optionsTable = {
        { -- section 1
            { -- column 1
                {
                    type = "text",
                    text = name,
                },
            },
        },
        { -- section 2
            { -- column 1
                {
                    type = "text",
                    small = true,
                    text = "General",
                },
                {
                    title = "Timer Format",
                    type = "dropDownSelector",
                    tooltipText = "Sets the format of the timer.  The format dictates the major appearance, and several timer settings only apply to one of the formats.",
                    values = {"bar", "icon"};
                    setting = "timerFormat",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Clickable",
                    type = "checkButton",
                    tooltipText = "If checked, the timer will respond to mouse events such as clicking and mousing over.",
                    setting = "clickable",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Tenths",
                    type = "checkButton",
                    tooltipText = "If checked, the time remaining will display tenths of a second when the time remaining is less than 10 seconds.",
                    setting = "tenths",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Sound Alert",
                    type = "checkButton",
                    tooltipText = "If checked, a sound will be played when the timer hits its red point.",
                    setting = "soundAlert",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Ghosts",
                    type = "checkButton",
                    tooltipText = "If checked, the timer will turn into a ghost timer when finished.",
                    setting = "ghosts",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Time Format",
                    type = "dropDownSelector",
                    tooltipText = "If letters, then the timer's time remaining will be displayed in a format like '5s'.  If digital, it will be ':05'.",
                    values = {"letters", "digital"};
                    setting = "timeFormat",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Alpha",
                    type = "slider",
                    tooltipText = "Sets the alpha of the timer.  Certain parts of the timer, such as text, are not affected by this setting.",
                    minValue = 0, 
                    maxValue = 1,
                    valueStep = .05,
                    setting = "alpha",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Scale",
                    type = "slider",
                    tooltipText = "Sets the scale of the timer.",
                    minValue = .5, 
                    maxValue = 2,
                    valueStep = .1,
                    setting = "scale",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Ghost Duration",
                    type = "slider",
                    tooltipText = "Sets the duration that the timer persists after finishing as a ghost.  Zero means that the timer will stay as a ghost until all timers in its group are also ghosts.",
                    minValue = 0, 
                    maxValue = 15,
                    valueStep = 1,
                    setting = "ghostLength",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Red Point",
                    type = "slider",
                    tooltipText = "Sets the point (in # seconds left) at which the timer has faded to its final color (default red).",
                    minValue = 0, 
                    maxValue = 10,
                    valueStep = .5,
                    setting = "redPoint",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Fade In Length",
                    type = "slider",
                    tooltipText = "Sets the amount of time it takes the timer to fade in when it begins.",
                    minValue = 0, 
                    maxValue = 5,
                    valueStep = .5,
                    setting = "fadeInLength",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Fade Out Length",
                    type = "slider",
                    tooltipText = "Sets the amount of time it takes the timer to fade out when it finishes.",
                    minValue = 0, 
                    maxValue = 5,
                    valueStep = .5,
                    setting = "fadeOutLength",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
            },
            { -- column 2
                {
                    type = "text",
                    small = true,
                    text = "Icon Format",
                },
                {
                    title = "Show Cooldown",
                    type = "checkButton",
                    tooltipText = "If checked, a cooldown clock will display on the anchor.",
                    setting = "iconShowCooldown",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Reverse Cooldown",
                    type = "checkButton",
                    tooltipText = "If checked, the cooldown clock will increase darkness instead of increasing lightness.",
                    setting = "iconReverseCooldown",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Flip Text",
                    type = "checkButton",
                    tooltipText = "If checked, the time remaining string, if displayed outside the icon, will flip to the oppsosite side of the icon.",
                    setting = "iconFlipText",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Display Format",
                    type = "dropDownSelector",
                    tooltipText = "Sets where the time remaining will appear: outside the icon, inside the icon, or nowhere.",
                    values = {"inside", "outside", "none"};
                    setting = "iconDisplayFormat",
                    hasToggle = name ~= "Default Timer",
                },
                "BREAK",
                {
                    title = "Preview",
                    type = "button",
                    tooltipText = "Show a preview of timers with this setting.",
                    func = function()
                        local offset = 0;
                        for i = 1, 2 do
                            local g = "Scary Mob " .. i;
                            for j = 1, 3 do
                                TimerLib:NewTimer({duration = 10, time = GetTime() - offset, name = "Cool Timer " .. j}, g, nil, name);
                                offset = offset + .5;
                            end
                        end
                    end,
                },
                "BREAK",
                {
                    type = "text",
                    small = true,
                    text = "Colors",
                },
                {
                    title = "Start",
                    type = "colorSelect",
                    tooltipText = "Sets the initial color of the timer.",
                    setting = "startColor",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Middle",
                    type = "colorSelect",
                    tooltipText = "Sets the color the timer will change to at 50% completion.",
                    setting = "middleColor",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Final",
                    type = "colorSelect",
                    tooltipText = "Sets the color the timer will change to at 'Red Point' seconds left.",
                    setting = "finalColor",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Ghost",
                    type = "colorSelect",
                    tooltipText = "Sets the color of the ghost timer.",
                    setting = "ghostColor",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Bar Text",
                    type = "colorSelect",
                    tooltipText = "Sets the color of the bar text.",
                    setting = "barTextColor",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Bar BG",
                    type = "colorSelect",
                    tooltipText = "Sets the color of the bar background.",
                    setting = "barBackgroundColor",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
            },
            { -- column 3
                {
                    type = "text",
                    small = true,
                    text = "Bar Format",
                },
                {
                    title = "Flip Icon",
                    type = "checkButton",
                    tooltipText = "If checked, the icon will be displayed to the right of the bar instead of the left.",
                    setting = "barFlipIcon",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Show Icon",
                    type = "checkButton",
                    tooltipText = "If unchecked, no icon will be shown next to the bar.",
                    setting = "barShowIcon",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Reversed",
                    type = "checkButton",
                    tooltipText = "If checked, the bar will shrink/grow from the right/top instead of the left/bottom.",
                    setting = "barReversed",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Grow",
                    type = "checkButton",
                    tooltipText = "If checked, the bar will grow instead of shrink.",
                    setting = "barGrow",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Orientation",
                    type = "dropDownSelector",
                    tooltipText = "Sets the orientation of the bar.",
                    values = {"horizontal", "vertical"},
                    setting = "barOrientation",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Texture",
                    type = "dropDownSelector",
                    tooltipText = "Sets the texture for the bar.",
                    values = {"Aluminium", "Banto", "Charcoal", "Elven", "Glaze", "Gloss", "Grunge", "Halycon", "Healbot", "LiteStep", "Moonmaster", "OtraviCB", "Perl", "Rocks", "Runes", "Smooth", "Solid", "Steel", "Stone"},
                    setting = "barTexture",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Scaling",
                    type = "dropDownSelector",
                    tooltipText = "Sets how the bar will change its value.  If standard, it will change linearly.  If absolute, it will not change until it hits the Absolute Scale setting.  If exponential, the bar will change faster when the timer is nearing completion.",
                    values = {"standard", "absolute", "exponential"},
                    setting = "barScaling",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Major Length",
                    type = "slider",
                    tooltipText = "Sets the major length of the bar.  This is its width when horizontal and height when vertical.",
                    minValue = 50, 
                    maxValue = 250,
                    valueStep = 10,
                    setting = "barMajorLength",
                    hasToggle = name ~= "Default Timer",
                },
                {
                    title = "Minor Length",
                    type = "slider",
                    tooltipText = "Sets the minor length of the bar.  This is its height when horizontal and width when vertical.",
                    minValue = 5, 
                    maxValue = 30,
                    valueStep = 1,
                    setting = "barMinorLength",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Font Height",
                    type = "slider",
                    tooltipText = "Sets the height of the font inside the bar.",
                    minValue = 1, 
                    maxValue = 20,
                    valueStep = 1,
                    setting = "barFontHeight",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Absolute Scale",
                    type = "slider",
                    tooltipText = "Sets the absolute scale of the bar.  If the scaling is set to 'absolute', the bar won't change until the timer has this much time remaining, at which point the bar will change as if the timer's duration is this setting.",
                    minValue = 1, 
                    maxValue = 30,
                    valueStep = 1,
                    setting = "barAbsScale",
                    hasToggle = name ~= "Default Timer",
                    notSimple = true,
                },
                {
                    title = "Text Format",
                    type = "editBoxString",
                    tooltipText = "Enter here the format for the text inside the bar.\n\nValid formatting codes:\n\n" .. 
                        "    %s: Name of the timer\n" ..
                        "    %d: Time remaining\n" ..
                        "\nNormally, the text is anchored to the left.  However, if you want to display some or all of the text anchored to the right, this is how you do it:" .. 
                        "\n\nAdd '&' with a ':' on EITHER (NOT BOTH) side of it.  \nThe ':' governs which side is given preference if the \nbar length is too short to display the entire message." .. 
                        "\n\nExample (Default Value): '%s&:%d', on a short bar: |Cool Ti... 0:15|",
                    setting = "barTextFormat",
                    hasToggle = name ~= "Default Timer",
                },
                "BREAK",
            },
        },
    };
    
    if name == "Default Timer" then
        if not AsheylaLib:InSimpleMode() then
            local newButton = {
                title = "Make New Setting!",
                type = "editBoxString",
                tooltipText = "Type in the name of a new timer setting to be customized.",
                scripts = {
                    OnEditFocusGained = function(self) self:SetText(""); self:HighlightText() end,
                    OnEnterPressed = function(self) 
            			local text = self:GetText();
            			if not TimerLib:Get("timerSettings", text) then
                            TimerLib:Set("timerSettings", text, {});
                            createGUI(text);
                        end
                        GUILib:ShowPanel("TimerLib Timers", text == "Default Anchor" and "main" or text);
                        self:ClearFocus();
            		end,
                },
            };
            optionsTable[1][1][1] = {optionsTable[1][1][1], newButton};
        end
    else
        local newButton1 = {
            title = "Delete",
            type = "button",
            tooltipText = "Deletes this setting.",
            func = function() 
                local frame = StaticPopup_Show("TIMERLIBTIMER", name)
                frame.data = name;
            end,
        };
        local newButton2 = {
            title = "Basis Timer Setting",
            type = "dropDown",
            tooltipText = "Select the timer setting you want to have applied for the settings you don't set up below (i.e., ones that don't have their little checkbox checked).",
            func = function()
                local setting = "reference";
                local info;
                for setName, settings in AsheylaLib:Alphabetize(TimerLib:Get("timerSettings")) do
                    if setName ~= "Default Timer" and setName ~= name then
                        info = UIDropDownMenu_CreateInfo();
                        info.text = setName;
                        info.checked = setName == TimerLib:Get("timerSettings", name, setting);
                        info.func = function() 
                            TimerLib:Set("timerSettings", name, setting, setName);
                            CloseDropDownMenus(); 
                            InterfaceOptionsFrame:Hide(); GUILib:ShowPanel("TimerLib Timers", name);
                        end
                        UIDropDownMenu_AddButton(info);
                    end
                end
                
                info = UIDropDownMenu_CreateInfo();
                info.text = "None";
                info.tooltipTitle = "Description";
                info.tooltipText = "If you select 'None', a default value will be found instead.";
                info.checked = TimerLib:Get("timerSettings", name, setting) == nil;
                info.func = function() 
                    TimerLib:Set("timerSettings", name, setting, nil);
                    CloseDropDownMenus(); 
                    InterfaceOptionsFrame:Hide(); GUILib:ShowPanel("TimerLib Timers", name);
                end
                UIDropDownMenu_AddButton(info);
            end,
            setting = "reference",
        };
        optionsTable[1][1][1] = {optionsTable[1][1][1], newButton1, newButton2};
    end
    
    local defaultFunc = function()
        TimerLib:Set("timerSettings", name, {});
        InterfaceOptionsFrame:Hide(); GUILib:ShowPanel("TimerLib Timers", name == "Default Timer" and "main" or name);
    end
    
    local getFunc = function(...)
        local val, isReal;
        local checked = TimerLib:AcquireTable();
        local ref = name;
        while ref and val == nil and not checked[ref] do
            val = TimerLib:Get("timerSettings", ref, ...);
            isReal = val ~= nil and ref == name;
            checked[ref] = true;
            ref = TimerLib:Get("timerSettings", ref, "reference");
        end
        TimerLib:ReleaseTable(checked);
        if val == nil then
            val = TimerLib:Get("timerSettings", "Default Timer", ...);
        end
        return val, isReal;
    end
    
    local setFunc = function(...)
        TimerLib:Set("timerSettings", name, ...);
    end
    
    local toggleTooltip = "If this checkbutton is unchecked, the setting does not apply and a default value will be used instead.  The default value will be found first by looking at the value in the anchor's default timer setting, then at the value in Default Timer.";

    GUILib:SetGUIPanel(SECTION_NAME, name == "Default Timer" and "main" or name, TimerLib, optionsTable, {default = defaultFunc, get = getFunc, set = setFunc, toggleTooltip = toggleTooltip, allSimple = true});
end

StaticPopupDialogs["TIMERLIBTIMER"] = {
    text = "You are about to delete the timer setting |cff00ff00%s|r.  Do you wish to proceed?",
    button1 = "Delete",
    button2 = "Cancel",
    OnAccept = function(self, name) 
        TimerLib:Set("timerSettings", name, nil);
        local cats = INTERFACEOPTIONS_ADDONCATEGORIES;
        for i = 1, #cats do
            if cats[i].name == name then
                table.remove(cats, i);
                break;
            end
        end
        for i = 1, #cats do
            if cats[i].name == SECTION_NAME then
                if not (cats[i + 1] and cats[i + 1].parent == cats[i].name) then
                    cats[i].hasChildren = false;
                    cats[i].collapsed = nil;
                end
                break;
            end
        end
        InterfaceOptionsFrame:Hide(); GUILib:ShowPanel("TimerLib Timers", "main");
    end,
    whileDead = 1,
    hideOnEscape = 1,
    timeout = 0,
}

local function createTimerGUI()
    if AsheylaLib:InSimpleMode() then
        SECTION_NAME = "|c7fffffff|rTimer Settings";
        createGUI("Default Timer");
    else
        SECTION_NAME = "|c7fffffff|rTimerLib Timers";
        createGUI("Default Timer");
        for name in AsheylaLib:Alphabetize(TimerLib:Get("timerSettings")) do
            if name ~= "Default Timer" then
                createGUI(name);
            end
        end
    end
end
createTimerGUI();
GUILib:AddGUICreationScript(createTimerGUI);

local existing = {};
GUILib:AddProfileChangeScript(function()
    table.wipe(existing);
    local cats = INTERFACEOPTIONS_ADDONCATEGORIES;
    local parent;
    for i = #cats, 1, -1 do
        if cats[i].parent == SECTION_NAME then
            if TimerLib:Get("timerSettings", cats[i].name) then
                table.insert(existing, cats[i].name);
                existing[cats[i].name] = true;
            else
                table.remove(cats, i);
            end
        elseif cats[i].name == SECTION_NAME then
            parent = cats[i];
        end
    end
    if #existing == 0 then
        parent.hasChildren = false;
        parent.collapsed = nil;
    end
    
    for name in AsheylaLib:Alphabetize(TimerLib:Get("timerSettings")) do
        if name ~= "Default Timer" and not existing[name] then
            createGUI(name);
        end
    end
end);
