--------------------------------------------------
-- Thron's Auto Sell
-- Config.lua
--------------------------------------------------

--------------------------------------------------
-- Main Frame
--------------------------------------------------

local Frame = CreateFrame(
    "Frame",
    "ThronsAutoSellFrame",
    UIParent,
    "BackdropTemplate"
)

Frame:SetSize(550, 500)
Frame:SetPoint("CENTER")

Frame:SetMovable(true)
Frame:SetResizable(true)
Frame:SetClampedToScreen(true)

Frame:SetResizeBounds(350, 250, 900, 700)

Frame:EnableMouse(true)

Frame:RegisterForDrag("LeftButton")

Frame:SetScript("OnDragStart", function(self)
    self:StartMoving()
end)

Frame:SetScript("OnDragStop", function(self)
    self:StopMovingOrSizing()
end)

Frame:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8X8",
})

Frame:SetBackdropColor(0, 0, 0, 0.75)

--------------------------------------------------
-- Addon Logo
--------------------------------------------------

local Logo = Frame:CreateTexture(
    nil,
    "ARTWORK"
)

Logo:SetTexture(
    "Interface\\AddOns\\ThronsAutoSell\\Media\\ThronsAutoSell.blp"
)

Logo:SetSize(
    128,
    128
)

Logo:SetPoint(
    "TOPRIGHT",
    -25,
    -50
)

Logo:SetAlpha(0.75)

--------------------------------------------------
-- Allow ESC Key To Close Window
--------------------------------------------------

Frame:SetToplevel(true)

table.insert(
    UISpecialFrames,
    "ThronsAutoSellFrame"
)

Frame:Hide()

--------------------------------------------------
-- Title Bar
--------------------------------------------------

local TitleBar = CreateFrame(
    "Frame",
    nil,
    Frame,
    "BackdropTemplate"
)

TitleBar:SetHeight(30)
TitleBar:SetPoint("TOPLEFT")
TitleBar:SetPoint("TOPRIGHT")

TitleBar:SetBackdrop({
    bgFile = "Interface\\Buttons\\WHITE8X8",
})

TitleBar:SetBackdropColor(
    0.1,
    0.1,
    0.1,
    0.8
)

local Title =
    TitleBar:CreateFontString(
        nil,
        "OVERLAY",
        "GameFontNormalLarge"
    )

Title:SetPoint("CENTER")

Title:SetText(
    "|cffCC5500Thron's Auto Sell|r"
)

--------------------------------------------------
-- Close Button
--------------------------------------------------

local CloseButton = CreateFrame(
    "Button",
    nil,
    Frame,
    "UIPanelButtonTemplate"
)

CloseButton:SetSize(24, 24)

CloseButton:SetPoint(
    "TOPRIGHT",
    -5,
    -3
)

CloseButton:SetText("X")

CloseButton:SetNormalTexture(
    "Interface\\Buttons\\WHITE8X8"
)

CloseButton:GetNormalTexture():SetVertexColor(
    1,
    0,
    0
)

CloseButton:SetScript(
    "OnClick",
    function()
        Frame:Hide()
    end
)

CloseButton:SetScript(
    "OnEnter",
    function(self)

        GameTooltip:SetOwner(
            self,
            "ANCHOR_RIGHT"
        )

        GameTooltip:AddLine(
            "Close Window"
        )

        GameTooltip:Show()

    end
)

CloseButton:SetScript(
    "OnLeave",
    function()
        GameTooltip:Hide()
    end
)

--------------------------------------------------
-- Auto Sell Checkbox
--------------------------------------------------

local AutoSellCB = CreateFrame(
    "CheckButton",
    nil,
    Frame,
    "UICheckButtonTemplate"
)

AutoSellCB:SetPoint(
    "TOPLEFT",
    20,
    -60
)

AutoSellCB.text:SetText(
    "Enable Auto Sell"
)
--------------------------------------------------
-- Repair Checkbox
--------------------------------------------------

local RepairCB = CreateFrame(
    "CheckButton",
    nil,
    Frame,
    "UICheckButtonTemplate"
)

RepairCB:SetPoint(
    "TOPLEFT",
    20,
    -100
)

RepairCB.text:SetText(
    "Automatically Repair Equipment"
)
--------------------------------------------------
-- Soulbound Checkbox
--------------------------------------------------

local SoulboundCB = CreateFrame(
    "CheckButton",
    nil,
    Frame,
    "UICheckButtonTemplate"
)

SoulboundCB:SetPoint(
    "TOPLEFT",
    20,
    -140
)

SoulboundCB.text:SetText(
    "Do Not Sell Soulbound Items"
)
--------------------------------------------------
-- Green Item Checkbox
--------------------------------------------------

local GreenItemsCB = CreateFrame(
    "CheckButton",
    nil,
    Frame,
    "UICheckButtonTemplate"
)

GreenItemsCB:SetPoint(
    "TOPLEFT",
    20,
    -180
)

GreenItemsCB.text:SetText(
    "Sell Green Items"
)

--------------------------------------------------
-- Blue Item Checkbox
--------------------------------------------------

local BlueItemsCB = CreateFrame(
    "CheckButton",
    nil,
    Frame,
    "UICheckButtonTemplate"
)

BlueItemsCB:SetPoint(
    "TOPLEFT",
    20,
    -220
)

BlueItemsCB.text:SetText(
    "Sell Blue Items"
)
--------------------------------------------------
-- Profile Label
--------------------------------------------------

local ProfileLabel =
    Frame:CreateFontString(
        nil,
        "OVERLAY",
        "GameFontNormal"
    )

ProfileLabel:SetPoint(
    "TOPLEFT",
    20,
    -270
)

ProfileLabel:SetText(
    "Profile"
)

--------------------------------------------------
-- Profile Dropdown
--------------------------------------------------

local ProfileDropdown = CreateFrame(
    "Frame",
    "ThronsAutoSellProfileDropdown",
    Frame,
    "UIDropDownMenuTemplate"
)

ProfileDropdown:SetPoint(
    "TOPLEFT",
    70,
    -257
)

--------------------------------------------------
-- New Profile Button
--------------------------------------------------

local NewProfileButton =
    CreateFrame(
        "Button",
        nil,
        Frame,
        "UIPanelButtonTemplate"
    )

NewProfileButton:SetSize(
    80,
    24
)

NewProfileButton:SetPoint(
    "TOPLEFT",
    20,
    -310
)

NewProfileButton:SetText(
    "New"
)

--------------------------------------------------
-- Delete Profile Button
--------------------------------------------------

local DeleteProfileButton =
    CreateFrame(
        "Button",
        nil,
        Frame,
        "UIPanelButtonTemplate"
    )

DeleteProfileButton:SetSize(
    80,
    24
)

DeleteProfileButton:SetPoint(
    "LEFT",
    NewProfileButton,
    "RIGHT",
    10,
    0
)

DeleteProfileButton:SetText(
    "Delete"
)

--------------------------------------------------
-- Sell Limit Label
--------------------------------------------------

local LimitLabel =
    Frame:CreateFontString(
        nil,
        "OVERLAY",
        "GameFontNormal"
    )

LimitLabel:SetPoint(
    "TOPLEFT",
    20,
    -360
)

LimitLabel:SetText(
    "Grey Item Sell Limit"
)

--------------------------------------------------
-- Sell Limit Box
--------------------------------------------------

local LimitBox = CreateFrame(
    "EditBox",
    nil,
    Frame,
    "InputBoxTemplate"
)

LimitBox:SetSize(
    120,
    25
)

LimitBox:SetPoint(
    "LEFT",
    LimitLabel,
    "RIGHT",
    15,
    0
)

LimitBox:SetAutoFocus(false)

LimitBox:SetNumeric(true)

--------------------------------------------------
-- Save Button
--------------------------------------------------

local SaveButton = CreateFrame(
    "Button",
    nil,
    Frame,
    "UIPanelButtonTemplate"
)

SaveButton:SetSize(
    100,
    25
)

SaveButton:SetPoint(
    "BOTTOMRIGHT",
    -150,
    10
)

SaveButton:SetText(
    "Save"
)

SaveButton:SetScript(
    "OnClick",
    function()

        RefreshControls()

        print(
            "|cff00FF00Thron's Auto Sell: Settings saved.|r"
        )

        Frame:Hide()

    end
)

--------------------------------------------------
-- Resize Handle
--------------------------------------------------

local ResizeButton =
    CreateFrame(
        "Button",
        nil,
        Frame
    )

ResizeButton:SetPoint(
    "BOTTOMRIGHT"
)

ResizeButton:SetSize(
    20,
    20
)

ResizeButton:SetNormalTexture(
    "Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up"
)

ResizeButton:SetHighlightTexture(
    "Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Highlight"
)

ResizeButton:SetPushedTexture(
    "Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Down"
)

ResizeButton:SetScript(
    "OnMouseDown",
    function()

        Frame:StartSizing(
            "BOTTOMRIGHT"
        )

    end
)

ResizeButton:SetScript(
    "OnMouseUp",
    function()

        Frame:StopMovingOrSizing()

    end
)

--------------------------------------------------
-- Create Profile Popup
--------------------------------------------------

StaticPopupDialogs[
    "THRONS_AUTO_SELL_NEW_PROFILE"
] = {

    text = "Enter Profile Name",

    button1 = "Create",

    button2 = "Cancel",

    hasEditBox = true,

    timeout = 0,

    whileDead = true,

    hideOnEscape = true,

    preferredIndex = 3,

   OnAccept = function(self)

    local profileName =
        self.EditBox:GetText()

        if profileName and
            profileName ~= "" then

            TAS_CreateProfile(
                profileName
            )
            RefreshControls()
        end
    end,
}

--------------------------------------------------
-- Profile Dropdown
--------------------------------------------------

local function InitializeProfileDropdown()

    UIDropDownMenu_Initialize(
        ProfileDropdown,

        function(self, level)

            local profiles =
                TAS_GetProfileList()

            for _,
                profileName
            in ipairs(profiles) do

                local info =
                    UIDropDownMenu_CreateInfo()

                info.text =
                    profileName

                info.checked =
                    profileName ==
                    TAS_GetCurrentProfileName()

                info.func =
                    function()

                        TAS_SetCurrentProfile(
                            profileName
                        )

                        RefreshControls()

                    end

                UIDropDownMenu_AddButton(
                    info
                )

            end

        end
    )

    UIDropDownMenu_SetWidth(
        ProfileDropdown,
        160
    )
end

--------------------------------------------------
-- Refresh UI
--------------------------------------------------

function RefreshControls()

    local db =
        TAS_GetSettings()

    AutoSellCB:SetChecked(
        db.autoSell
    )
    RepairCB:SetChecked(
        db.repairItems
    )
    SoulboundCB:SetChecked(
        db.skipSoulbound
    )

    GreenItemsCB:SetChecked(
        db.sellGreenItems
    )

    BlueItemsCB:SetChecked(
        db.sellBlueItems
    )

    LimitBox:SetText(
        db.sellLimit or ""
    )

    UIDropDownMenu_SetText(
        ProfileDropdown,
        TAS_GetCurrentProfileName()
    )
end

--------------------------------------------------
-- Save Controls
--------------------------------------------------

AutoSellCB:SetScript(
    "OnClick",

    function(self)

        local db =
            TAS_GetSettings()

        db.autoSell =
            self:GetChecked()

    end
)
RepairCB:SetScript(
    "OnClick",
    function(self)

        local db =
            TAS_GetSettings()

        db.repairItems =
            self:GetChecked()

    end
)
SoulboundCB:SetScript(
    "OnClick",

    function(self)

        local db =
            TAS_GetSettings()

        db.skipSoulbound =
            self:GetChecked()

    end
)
GreenItemsCB:SetScript(
    "OnClick",
    function(self)

        local db =
            TAS_GetSettings()

        db.sellGreenItems =
            self:GetChecked()

    end
)

BlueItemsCB:SetScript(
    "OnClick",
    function(self)

        local db =
            TAS_GetSettings()

        db.sellBlueItems =
            self:GetChecked()

    end
)

LimitBox:SetScript(
    "OnTextChanged",

    function(self)

        local db =
            TAS_GetSettings()

        db.sellLimit =
            self:GetText()

    end
)

--------------------------------------------------
-- Profile Buttons
--------------------------------------------------

NewProfileButton:SetScript(
    "OnClick",

    function()

        StaticPopup_Show(
            "THRONS_AUTO_SELL_NEW_PROFILE"
        )

    end
)

DeleteProfileButton:SetScript(
    "OnClick",

    function()

        local profile =
            TAS_GetCurrentProfileName()

        if profile ==
            "Default" then

            print(
                "|cffff0000Default profile cannot be deleted.|r"
            )

            return
        end

        TAS_DeleteProfile(
            profile
        )

        RefreshControls()

    end
)

--------------------------------------------------
-- Window Show Event
--------------------------------------------------

Frame:SetScript(
    "OnShow",

    function()

        InitializeProfileDropdown()

        RefreshControls()

    end
)