local addonName, ZLAUTO= ...

local ZL = ZLAUTO.mainFrame
local CreateButton = ZL.CreateButton
local ScrollFrame_OnMouseWheel = ZL.ScrollFrame_OnMouseWheel
local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")

function ZL.CreateExportProfilesFrame()
    ZL.ExportProfilesFrame = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
    ExportProfilesFrame = ZL.ExportProfilesFrame
    ExportProfilesFrame:Hide()
    ExportProfilesFrame:SetSize(800, 500)
    ExportProfilesFrame:SetPoint("CENTER")
    ExportProfilesFrame:SetFrameStrata("DIALOG")
    ExportProfilesFrame:SetToplevel(true)
    ExportProfilesFrame:SetMovable(true)
    ExportProfilesFrame:SetBackdrop({
        bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-Dark",
        edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
        tile = true,
        tileSize = 32,
        edgeSize = 32,
        insets = { left = 11, right = 12, top = 12, bottom = 11 }
    })
    ExportProfilesFrame:SetScript("OnMouseUp", function (self)
        self:StopMovingOrSizing()
    end)
    ExportProfilesFrame:SetScript("OnMouseDown", function (self)
        self:StartMoving()
    end)
    
    local colseBtn = CreateFrame("Button", nil, ExportProfilesFrame, "UIPanelCloseButton")
    colseBtn:SetSize(30, 30)
    colseBtn:SetPoint("TOPRIGHT", -10, -10)
    local lable1 = ExportProfilesFrame:CreateFontString(nil, "ARTWORK")
    lable1:SetSize(ExportProfilesFrame:GetWidth()-30, 20)
    lable1:SetFont(GameFontNormal:GetFont(), 18)
    lable1:SetPoint("TOP", 0, -20)
    lable1:SetText("导出配置")
    lable1:SetTextColor(1, 0.82, 0)

    local importBtn = CreateButton(nil, ExportProfilesFrame, "导出", "TOPLEFT", ExportProfilesFrame, "TOPLEFT", 15, -lable1:GetHeight()-25, 60, 20, 14, true, 0)
    local sf1 = CreateFrame("Frame", nil, ExportProfilesFrame, "TooltipBackdropTemplate")
    sf1:SetPoint("TOPLEFT", importBtn, "BOTTOMLEFT", 0, -5)
    sf1:SetPoint("BOTTOMLEFT", ExportProfilesFrame, "BOTTOMLEFT", 15, 15)
    sf1:SetWidth(200)

    local allCheckBtn = CreateFrame("CheckButton", nil, sf1, "UICheckButtonTemplate")
    allCheckBtn:SetPoint("TOPLEFT", 10, -8)
    allCheckBtn:SetSize(24, 24)
    allCheckBtn.Text:SetText("全选")
    allCheckBtn:SetScript("OnClick", function (self)
        if #ExportProfilesFrame.entries1 > 0 then
            for _, f in ipairs(ExportProfilesFrame.entries1) do
                if f:IsShown() then
                    f:SetChecked(self:GetChecked())
                end
            end
        end
    end)

    local ScrollFrame1 = CreateFrame("ScrollFrame", nil, sf1, "UIPanelScrollFrameTemplate")
    ScrollFrame1:SetPoint("TOPLEFT", 10, -30)
    ScrollFrame1:SetPoint("BOTTOMRIGHT", -5, 8)
    ScrollFrame1:SetClipsChildren(true)
    ScrollFrame1:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
    ScrollFrame1.ScrollBar:ClearAllPoints()
    ScrollFrame1.ScrollBar:SetPoint("TOPLEFT", ScrollFrame1, "TOPRIGHT", -18, -18)
    ScrollFrame1.ScrollBar:SetPoint("BOTTOMRIGHT", ScrollFrame1, "BOTTOMRIGHT", 2, 18)
    ScrollFrame1.content = CreateFrame("Frame", nil, ScrollFrame1)
    local sfContent1 = ScrollFrame1.content
    sfContent1:SetPoint("TOPLEFT", ScrollFrame1, "TOPLEFT", 0, 0)
    sfContent1:SetSize(180, 50)

    ExportProfilesFrame.entries1 = setmetatable({}, {
        __index = function (t, v)
            local f = CreateFrame("CheckButton", nil, sfContent1, "UICheckButtonTemplate")
            f.Text:SetFontObject(GameFontHighlightSmall)
            f:SetSize(20, 20)
            f:SetPoint("TOPLEFT", 0, -(v-1)*20)
            t[v] = f
            return f
        end
    })

    function ExportProfilesFrame:Update()
        for _, f in ipairs(ExportProfilesFrame.entries1) do f:Hide() end
        local tempList = {}
        for key in pairs(ZL_Profile.profiles) do
            tinsert(tempList, key)
        end
        if #tempList > 0 then
            sort(tempList, function (a, b)
                if a == "Default" or "默认" then
                    return true
                elseif b == "Default" or "默认" then
                    return false
                end
                return a < b
            end)
            local index = 1
            local f
            for _, key in ipairs(tempList) do
                f = ExportProfilesFrame.entries1[index]
                index = index + 1
                f.Text:SetText(key)
                f:Show()
            end
        end
        wipe(tempList)
        tempList = nil
    end

    local sf2 = CreateFrame("Frame", nil, ExportProfilesFrame, "TooltipBackdropTemplate")
    sf2:SetPoint("TOPLEFT", sf1, "TOPRIGHT", 5, 0)
    sf2:SetPoint("BOTTOMRIGHT", ExportProfilesFrame, "BOTTOMRIGHT", -15, 15)
    local ScrollFrame2 = CreateFrame("ScrollFrame", nil, sf2, "UIPanelScrollFrameTemplate")
    ScrollFrame2:SetPoint("TOPLEFT", 10, -8)
    ScrollFrame2:SetPoint("BOTTOMRIGHT", -5, 8)
    ScrollFrame2:SetClipsChildren(true)
    ScrollFrame2:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
    ScrollFrame2.ScrollBar:ClearAllPoints()
    ScrollFrame2.ScrollBar:SetPoint("TOPLEFT", ScrollFrame2, "TOPRIGHT", -18, -18)
    ScrollFrame2.ScrollBar:SetPoint("BOTTOMRIGHT", ScrollFrame2, "BOTTOMRIGHT", 2, 18)
    local ScrollingEditBox = CreateFrame("EditBox", nil, ScrollFrame2)
    ExportProfilesFrame.ScrollingEditBox = ScrollingEditBox
    ScrollingEditBox:SetSize(ScrollFrame2:GetWidth()-20, 20)
    ScrollingEditBox:SetMultiLine(true)
    ScrollingEditBox:SetAutoFocus(false)
    ScrollingEditBox:SetFontObject("GameFontHighlightSmall")
    ScrollFrame2:SetScrollChild(ScrollingEditBox)
    ScrollFrame2:SetScript("OnMouseDown", function ()
        ScrollingEditBox:SetFocus()
    end)
    ScrollFrame2:SetScript("OnMouseUp", function ()
        ScrollingEditBox:HighlightText()
    end)
    ScrollingEditBox:SetScript("OnEscapePressed",function (self)
        self:ClearFocus()
    end)
    ScrollingEditBox:SetScript("OnEditFocusLost",function (self)
        self:HighlightText(0, 0)
    end)
    ScrollingEditBox:SetScript("OnEditFocusGained",function (self)
        self:HighlightText()
    end)
    ScrollingEditBox:SetScript("OnMouseUp", function (self)
        self:HighlightText()
    end)

    ExportProfilesFrame:SetScript("OnShow", function (self)
        if ExportProfilesFrame.type == "export" then
            importBtn:SetText("导出")
            sf1:Show()
            sf2:ClearAllPoints()
            sf2:SetPoint("TOPLEFT", sf1, "TOPRIGHT", 5, 0)
            sf2:SetPoint("BOTTOMRIGHT", ExportProfilesFrame, "BOTTOMRIGHT", -15, 15)
            ExportProfilesFrame:Update()
        else
            importBtn:SetText("导入")
            sf1:Hide()
            sf2:ClearAllPoints()
            sf2:SetPoint("TOPLEFT", importBtn, "BOTTOMLEFT", 0, -5)
            sf2:SetPoint("BOTTOMRIGHT", ExportProfilesFrame, "BOTTOMRIGHT", -15, 15)
        end
        ScrollingEditBox:SetText("")
    end)
    importBtn:SetScript("OnClick", function (self)
        if ExportProfilesFrame.type == "export" then
            local exportData = {}
            local groupName, txt
            for _, f in ipairs(ExportProfilesFrame.entries1) do
                if f:IsShown() and f:GetChecked() then
                    groupName = f.Text:GetText()
                    exportData[groupName] = ZL_Profile.profiles[groupName]
                end
            end
            if next(exportData) then
                --字符串
                local serialized = LibSerialize:Serialize(exportData)
                local compressed = LibDeflate:CompressDeflate(serialized)
                txt = "!AutoAuction:PROFILES!" .. LibDeflate:EncodeForPrint(compressed)
                ScrollingEditBox:SetText(txt)
                ScrollingEditBox:HighlightText()
                ScrollingEditBox:SetFocus()
            else
                ZL.Print("没有需要导出的内容！", true, true)
                ScrollingEditBox:SetText("")
                ScrollingEditBox:ClearFocus()
            end
            exportData = nil
            txt = nil
        else
            local txt = ScrollingEditBox:GetText() or ""
            if #txt > 0 then
                --字符串
                if txt:match("^!AutoAuction:PROFILES!.+") then
                    txt = txt:gsub("!AutoAuction:PROFILES!", "", 1)
                    local decodedData = LibDeflate:DecodeForPrint(txt)
                    local decompressedData = LibDeflate:DecompressDeflate(decodedData)
                    local success, myReceivedData = LibSerialize:Deserialize(decompressedData)
                    if success then
                        StaticPopup_Show(
                            "ZL_IMPORTPROFILESCONFIRM",
                            "请选择导入方式：", nil,
                            myReceivedData
                        )
                    else
                        ZL.Print("导入字符串解析失败！", true, true)
                        return
                    end
                else
                    ZL.Print("导入字符串不符合要求！", true, true)
                    return
                end
            else
                ZL.Print("没有需要导入的内容", true, true)
            end
        end
    end)
end