local addonName, ZLAUTO = ...

local ZL = ZLAUTO.mainFrame
local ScrollFrame_OnMouseWheel = ZL.ScrollFrame_OnMouseWheel
local CreateButton = ZL.CreateButton
local GameTooltipShow = ZL.GameTooltipShow
local CreateEditBox = ZL.CreateEditBox
local ZLCreateFrame = ZL.ZLCreateFrame
local emptytable = ZL.emptytable

function ZL:CreateImportWCLFrame()
    local scoreWidth, wagesBiliWidth, importWidth = 100, 100, 400
    local lineHeight = 20
    local totalWidth = scoreWidth + wagesBiliWidth + importWidth + 15
    local totalHeight = 325

    ZL.ImportWCLFrame = ZLCreateFrame(nil, ZL, 800, 600, nil, true, true, "WCL规则设置")
    local ImportWCLFrame = ZL.ImportWCLFrame
    ImportWCLFrame:Hide()
    ImportWCLFrame:SetResizeBounds(totalWidth, totalHeight, totalWidth, totalHeight)
    ImportWCLFrame:SetScript("OnMouseUp", function (self)
        self:StopMovingOrSizing()
        if not ZL.glb.ZLImportWCLFrame then ZL.glb.ZLImportWCLFrame = {} end
        ZL.glb.ZLImportWCLFrame.point, _, ZL.glb.ZLImportWCLFrame.relative, ZL.glb.ZLImportWCLFrame.x, ZL.glb.ZLImportWCLFrame.y = self:GetPoint()
    end)
    ImportWCLFrame:SetScript("OnMouseDown", function (self)
        self:StartMoving()
    end)
    ImportWCLFrame:SetScript("OnShow", function (self)
        ImportWCLFrame:Update()
    end)
    ImportWCLFrame.resizeButton:Hide()
    ImportWCLFrame:ClearAllPoints()
    if ZL.glb.ZLImportWCLFrame and ZL.glb.ZLImportWCLFrame.point then
        ImportWCLFrame:SetPoint(ZL.glb.ZLImportWCLFrame.point, UIParent, ZL.glb.ZLImportWCLFrame.relative, ZL.glb.ZLImportWCLFrame.x, ZL.glb.ZLImportWCLFrame.y)
    else
        ImportWCLFrame:SetPoint("CENTER")
    end
     if ZL.glb.ZLImportWCLFrame and ZL.glb.ZLImportWCLFrame.Height then
        ImportWCLFrame:SetSize(totalWidth, min(ZL.db.global.ZLImportWCLFrame.Height, totalHeight))
    else
        ImportWCLFrame:SetSize(totalWidth, totalHeight)
    end

    local btnTexture = [[Interface\ACHIEVEMENTFRAME\UI-Achievement-Parchment-Horizontal]]
    local scoreBtn = CreateButton(nil, ImportWCLFrame, "分数等级", "TOPLEFT", ImportWCLFrame.TitleBottom, "BOTTOMLEFT", 3, -10, scoreWidth)
    scoreBtn:GetFontString():SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
    scoreBtn:SetNormalTexture(btnTexture)
    scoreBtn:ClearHighlightTexture()

    local wagesBiliBtn = CreateButton(nil, ImportWCLFrame, "工资比例(%)", "LEFT", scoreBtn, "RIGHT", 0, 0, wagesBiliWidth)
    wagesBiliBtn:GetFontString():SetFont(GameFontNormal:GetFont(), 14, "OUTLINE")
    wagesBiliBtn:SetNormalTexture(btnTexture)
    wagesBiliBtn:ClearHighlightTexture()

    local sf = CreateFrame("Frame", nil, ImportWCLFrame, "TooltipBackdropTemplate")
    sf:SetPoint("TOPLEFT", wagesBiliBtn, "TOPRIGHT", 5, 0)
    sf:SetSize(400, totalHeight-50)
    local ScrollFrame = CreateFrame("ScrollFrame", nil, sf, "UIPanelScrollFrameTemplate")
    ScrollFrame:SetPoint("TOPLEFT", 10, -8)
    ScrollFrame:SetPoint("BOTTOMRIGHT", -5, 8)
    ScrollFrame:SetClipsChildren(true)
    ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
    ScrollFrame.ScrollBar:ClearAllPoints()
    ScrollFrame.ScrollBar:SetPoint("TOPLEFT", ScrollFrame, "TOPRIGHT", -18, -18)
    ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", ScrollFrame, "BOTTOMRIGHT", 2, 18)
    local ScrollingEditBox = CreateFrame("EditBox", nil, ScrollFrame)
    ScrollingEditBox:SetSize(ScrollFrame:GetWidth()-20, 20)
    ScrollingEditBox:SetMultiLine(true)
    ScrollingEditBox:SetAutoFocus(false)
    ScrollingEditBox:SetFontObject("GameFontHighlight")
    local geshiString = ScrollingEditBox:CreateFontString()
    geshiString:SetPoint("TOPLEFT")
    geshiString:SetWidth(ScrollingEditBox:GetWidth())
    geshiString:SetFont(GameFontNormal:GetFont(), 13)
    geshiString:SetJustifyH("LEFT")
    geshiString:SetText("|cff595959格式：\n角色A,分数;角色B,分数\n注意：角色名不带服务器名\n导出WCL分数网站：点击左侧【复制导出WCL分数网站】按钮\n\n感谢：\n千川魔兽 提供该分配方案\nDreamForge梦工坊 提供导出功能|r")
    ScrollFrame:SetScrollChild(ScrollingEditBox)
    ScrollFrame:SetScript("OnMouseDown", function ()
        ScrollingEditBox:SetFocus()
    end)
    ScrollFrame: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)
    ScrollingEditBox:SetScript("OnTextChanged", function (self)
        local text = self:GetText()or ""
        if #text > 0 then
            geshiString:Hide()
        else
            geshiString:Show()
        end
    end)

    local exportBtn = CreateButton(nil, ImportWCLFrame, "复制导出WCL分数网站", "BOTTOMRIGHT", sf, "BOTTOMLEFT", -5, 5, scoreWidth + wagesBiliWidth, 20, 14, true, 0)
    exportBtn:SetScript("OnClick", function ()
        ChatEdit_ActivateChat(ChatEdit_ChooseBoxForSend())
        ChatEdit_InsertLink("https://dreamforgewow.com/wcl-scores")
        ChatEdit_ChooseBoxForSend():HighlightText()
        local msgStr = "已发送到聊天窗口，按Ctr+C复制"
        ZL.ScrollingMessageFrame:AddMessage(msgStr, 1, 1, 0)
    end)
    local importBtn = CreateButton(nil, ImportWCLFrame, "导入WCL分数",  "BOTTOMLEFT", exportBtn, "TOPLEFT", 0, 5, scoreWidth + wagesBiliWidth, 20, 14, true, 0)
    importBtn:SetScript("OnClick", function ()
        local text = ScrollingEditBox:GetText() or ""
        if #text == 0 then
            ZL.Print("请粘贴需要导入的WCL分数文本", true, nil, 1, 0, 0)
            return
        end
        local textList = strsplittable(";", text)
        tDeleteItem(textList, "")
        if #textList > 0 then
            ZL_CURRENT.WCLSorce = {}
            local groupList
            for _, group in ipairs(textList) do
                groupList = strsplittable(",", group)
                tDeleteItem(groupList, "")
                if #groupList == 2 then
                    ZL_CURRENT.WCLSorce[groupList[1]] = tonumber(groupList[2]) or 0
                end
            end
            ZL.Print("WCL分数导入完成！")
            if ZL.opt.AutoUpdateWagesBiliByWCL then
                ZL.UpdateWagesBiliFromWCL()
            end
        end

    end)
    local updatetBtn = CreateButton(nil, ImportWCLFrame, "更新工资比例", "BOTTOMLEFT", importBtn, "TOPLEFT", 0, 5, scoreWidth + wagesBiliWidth, 20, 14, true, 0)
    updatetBtn:SetScript("OnClick", function ()
        ZL.UpdateWagesBiliFromWCL()
    end)
    local updateCheckBtn = CreateFrame("CheckButton", nil, ImportWCLFrame, "UICheckButtonTemplate")
    updateCheckBtn:SetPoint("BOTTOMLEFT", updatetBtn, "TOPLEFT", 0, 5)
    updateCheckBtn:SetSize(20, 20)
    updateCheckBtn:SetChecked(ZL.opt.AutoUpdateWagesBiliByWCL)
    updateCheckBtn.Text:SetText("自动更新工资比例")
    updateCheckBtn:SetScript("OnClick", function (self)
        ZL.opt.AutoUpdateWagesBiliByWCL = self:GetChecked()
        if ZL.opt.AutoUpdateWagesBiliByWCL then
            ZL.UpdateWagesBiliFromWCL()
        end
    end)

    --创建滚动条
    ImportWCLFrame.ScrollFrame = CreateFrame("ScrollFrame", nil, ImportWCLFrame, "UIPanelScrollFrameTemplate")
    local SearchItemsFrame_ScrollFrame = ImportWCLFrame.ScrollFrame
    SearchItemsFrame_ScrollFrame:SetFrameLevel(ImportWCLFrame:GetFrameLevel() + 1)
    SearchItemsFrame_ScrollFrame:SetPoint("TOPLEFT", scoreBtn, "BOTTOMLEFT", 0, 0)
    SearchItemsFrame_ScrollFrame:SetPoint("BOTTOMRIGHT", ImportWCLFrame, "BOTTOMRIGHT", -2, 3)
    SearchItemsFrame_ScrollFrame:SetClipsChildren(true)
    SearchItemsFrame_ScrollFrame:SetScript("OnMouseWheel", ScrollFrame_OnMouseWheel)
    SearchItemsFrame_ScrollFrame.ScrollBar:ClearAllPoints()
    SearchItemsFrame_ScrollFrame.ScrollBar:SetPoint("TOPLEFT", SearchItemsFrame_ScrollFrame, "TOPRIGHT", -20, -16)
    SearchItemsFrame_ScrollFrame.ScrollBar:SetPoint("BOTTOMRIGHT", SearchItemsFrame_ScrollFrame, "BOTTOMRIGHT", 0, 16)
    SearchItemsFrame_ScrollFrame.ScrollBar:Hide()
    SearchItemsFrame_ScrollFrame.content = CreateFrame("Frame", nil, SearchItemsFrame_ScrollFrame)
    local WCLListContent = SearchItemsFrame_ScrollFrame.content
    WCLListContent:SetPoint("TOPLEFT", SearchItemsFrame_ScrollFrame, "TOPLEFT", 0, 0)
    WCLListContent:SetSize(180, 100)
    SearchItemsFrame_ScrollFrame:SetScrollChild(WCLListContent)
    local function OnEnter(f)
        f:SetNormalTexture("Interface\\FriendsFrame\\UI-FriendsFrame-HighlightBar")
    end
    local function OnLeave(f)
        f:ClearNormalTexture()
    end
    ImportWCLFrame.entries = setmetatable({}, {
        __index = function (t, v)
            local f = CreateFrame("Button", nil, WCLListContent)
            f:Hide()
            f:SetScript("OnEnter", OnEnter)
            f:SetScript("OnLeave", OnLeave)
            f:SetSize(scoreWidth + wagesBiliWidth, lineHeight)
            f:SetPoint("TOPLEFT", WCLListContent, "TOPLEFT", 0, -(v-1)*lineHeight)
            f.Background = f:CreateTexture(nil,"BACKGROUND")
            f.Background:SetPoint("TOPLEFT")
            f.Background:SetPoint("BOTTOMRIGHT")
            if v%2 == 1 then
                f.Background:SetColorTexture(0, 0, 0, .8)
            else
                f.Background:SetColorTexture(0.15, 0.15, 0.15, .8)
            end
            f.scoreLabel = f:CreateFontString(nil, "ARTWORK")
            f.scoreLabel:SetFont(GameFontNormal:GetFont(), 13, "OUTLINE")
            f.scoreLabel:SetSize(scoreWidth, lineHeight)
            f.scoreLabel:SetPoint("LEFT")
            -- f.scoreLabel:SetJustifyH("LEFT")
            f.wagesBiliBox = CreateEditBox(f, wagesBiliWidth, lineHeight-2, 13, "RIGHT", true)
            f.wagesBiliBox:SetPoint("LEFT", f.scoreLabel, "RIGHT", 0, 0)
            f.wagesBiliBox.Background:SetColorTexture(0,0,0,0)
            f.wagesBiliBox:SetScript("OnTextChanged", function (self)
                local wagesBili = self:GetNumber()
                if wagesBili < 0 then
                    self:SetNumber(0)
                end
                ZL.opt.WCLSetting[f.index].wagesBili = self:GetNumber()
            end)
            t[v] = f
            return f
        end
    })

    function ImportWCLFrame:Update()
        for _, f in ipairs(ImportWCLFrame.entries) do f:Hide() end
        for i, data in ipairs(ZL.opt.WCLSetting) do
            local f
            f = ImportWCLFrame.entries[i]
            f.index = i
            f.scoreLabel:SetText(data.sorceText)
            f.wagesBiliBox:SetNumber(data.wagesBili)
            f:Show()
        end
    end
end
