local _, PUP = ...

function PUP:CreateAuraImportElement(parent)
    -- Outer frame
    local frame = CreateFrame("Frame", nil, parent)

    frame.height = 40

    frame:SetHeight(frame.height)

    -- Icon
    frame.icon = CreateFrame("Frame", nil, frame)

    frame.icon:SetSize(24, 24)
    frame.icon:Hide()
    frame.icon:SetPoint("LEFT", frame, "LEFT", 8, 0)

    frame.icon.tex = frame.icon:CreateTexture(nil, "BACKGROUND")
    frame.icon.tex:SetAllPoints(frame.icon)
    frame.icon.tex:SetTexCoord(0.08, 0.92, 0.08, 0.92)

    -- Display name
    frame.displayName = frame:CreateFontString(nil, "OVERLAY")

    frame.displayName:SetFontObject(PUFont17)
    frame.displayName:SetPoint("LEFT", frame, "LEFT", 8, 0)

    function frame:SetDisplayName(displayName)
        frame.displayName:SetText(string.format("|cff%s%s|r", PUP.gs.visual.colorStrings.white, displayName))

        -- If this element shows an addon update instead of an aura update, don't add an update button script
        -- Icon is also hardcoded, rather than taken from aura data (there is no aura)
        if displayName == "Prepull Updater" then
            frame.icon.tex:SetTexture("Interface\\Addons\\PrepullUpdater\\Media\\Textures\\Prepull.tga")
            frame.icon:Show()
            frame.displayName:SetPoint("LEFT", frame, "LEFT", 38, 0)
        else
            local auraData = PrepullUpdaterSaved.WeakAuras[displayName]

            frame.importButton:SetScript(
                "OnClick",
                function()
                    -- This should only be necessary if the user manually imported a version of the aura with a different UID, after logging in
                    -- Do it anyway just to be sure
                    PUP:MatchInstalledUID(auraData.d)

                    -- Apply existing "load: never" settings to the aura data before importing
                    local modifiedAuraData = CopyTable(auraData)
                    local installedAuraData = PUP:GetInstalledAuraDataByUID(modifiedAuraData.uid)

                    PUP:ApplyLoadSettings(modifiedAuraData.d, installedAuraData)

                    if modifiedAuraData.c then
                        for _, childAuraData in pairs(modifiedAuraData.c) do
                            local installedChildAuraData = PUP:GetInstalledAuraDataByUID(childAuraData.uid)

                            PUP:ApplyLoadSettings(childAuraData, installedChildAuraData)
                            PUP:ApplySoundSettings(childAuraData, installedChildAuraData)
                        end
                    end

                    -- Loop through children and save IDs of auras that have custom code on init
                    -- [aura_id] = custom_code (string)
                    local customOnInit = {}

                    for _, childData in ipairs(auraData.c or {auraData.d}) do
                        local doCustom = childData.actions and childData.actions.init and childData.actions.init.do_custom
                        local customCode = doCustom and childData.actions.init.custom

                        if doCustom and customCode and customCode ~= "" then
                            customOnInit[childData.id] = customCode
                        end
                    end

                    WeakAuras.Import(
                        modifiedAuraData,
                        nil,
                        function(success, id)
                            if not success then return end

                            local data = WeakAuras.GetData(id)
                            local version = PrepullUpdaterSaved.WeakAuras[displayName].d.prepullVersion

                            data.preferToUpdate = true
                            data.ignoreWagoUpdate = true
                            data.prepullVersion = version

                            PUP:ForceUpdateOnInit(customOnInit)
                            PUP:OnUpdateAura()
                        end
                    )
                end
            )

            local icon = auraData.d.groupIcon

            if icon then
                frame.icon.tex:SetTexture(icon)
            end

            frame.icon:SetShown(icon)
            frame.displayName:SetPoint("LEFT", frame, "LEFT", icon and 38 or 8, 0)
        end
    end

    -- Version count
    frame.versionCount = frame:CreateFontString(nil, "OVERLAY")

    frame.versionCount:SetFontObject(PUFont17)
    frame.versionCount:SetPoint("CENTER", frame, "CENTER")
    
    function frame:SetVersionsBehind(count)
        frame.versionCount:SetText(string.format("|cff%s%d Version(en)|r", PUP.gs.visual.colorStrings.red, count))
    end

    -- Import button
    frame.importButton = PUP:CreateButton(frame, "Update", function() end)

    frame.importButton :SetNormalFontObject(PUFont15)
    frame.importButton :SetHighlightFontObject(PUFont15)
    frame.importButton :SetDisabledFontObject(PUFont15)

    frame.importButton:SetPoint("RIGHT", frame, "RIGHT", -8, 0)

    -- Requires addon update text
    frame.requiresUpdateText = frame:CreateFontString(nil, "OVERLAY")

    frame.requiresUpdateText:SetFontObject(PUFont17)
    frame.requiresUpdateText:SetPoint("RIGHT", frame, "RIGHT", -8, 0)
    frame.requiresUpdateText:SetText(string.format("|cff%sAddon aktualisieren!|r", PUP.gs.visual.colorStrings.red))
    frame.requiresUpdateText:Hide()

    PUP:AddTooltip(frame.requiresUpdateText, "Eine neuere Version dieser Aura ist verfügbar. Aktualisiere das Addon um es zu erhalten.")

    -- Border
    PUP:AddBorder(frame)

    local borderColor = PUP.gs.visual.borderColor

    frame:SetBorderColor(borderColor.r, borderColor.g, borderColor.b)

    function frame:SetRequiresAddOnUpdate(requiresUpdate)
        frame.importButton:SetShown(not requiresUpdate)
        frame.requiresUpdateText:SetShown(requiresUpdate)
    end

    return frame
end