local _,ns = ...
local L = ns.L


local z = ns.Utils.crearFrameConTextura(ns.f,3,215,0,331,1)
z:SetPoint("CENTER", ns.f, "CENTER")
ns.FrameSetup(z,"NoVender")
local scrollFrame
local textArea
local newChanges = true
ns.newChanges = newChanges

local function setTooltip(parentFrame,...)
    local text1,text2,text3,text4=...
    parentFrame:HookScript("OnEnter", function(self)
        GameTooltip:SetOwner( parentFrame, "ANCHOR_NONE" );
        GameTooltip:SetPoint("TOPLEFT", z, "BOTTOMLEFT", -2, -4)
        if text1 then
        --print(HIGHLIGHT_FONT_COLOR_CODE..text1..FONT_COLOR_CODE_CLOSE)
            GameTooltip:AddLine(NORMAL_FONT_COLOR_CODE..text1..FONT_COLOR_CODE_CLOSE,1,0,0,true)
        end
        if text2 then
            GameTooltip:AddLine(HIGHLIGHT_FONT_COLOR_CODE..text2..FONT_COLOR_CODE_CLOSE,1,0,0,true)
        end
        if text3 then
            GameTooltip:AddLine(HIGHLIGHT_FONT_COLOR_CODE..text3..FONT_COLOR_CODE_CLOSE,1,0,0,true)
        end
        if text4 then
            GameTooltip:AddLine(HIGHLIGHT_FONT_COLOR_CODE..text4..FONT_COLOR_CODE_CLOSE,1,0,0,true)
        end
        --AutoScrapTooltip:AddDoubleLine("prob", "lala")
        GameTooltip:Show()
    end)
    parentFrame:HookScript("OnLeave", function(self)
        GameTooltip:Hide()
    end)
end

z.c = CreateFrame("Button", nil, z)
z.c:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
z.c:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
z.c:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
z.c:SetSize(32, 32)
z.c:SetPoint("TOPRIGHT", z, "TOPRIGHT", 0, -3)
z.c:SetScript("OnClick", function() ns.ocultarFrames() end)

local function addItemToWhiteList(itemID, itemLink)
    if ns.isJunkAS(itemID) then
        ns.toggleJunkAS(itemID,itemLink)
    end
    if ns.isJunkBL(itemID) then
        ns.toggleJunkBL(itemID,itemLink)
    end
    AutoScrap_CharSettings.noVenderList[itemID] = itemLink
    ns.newChanges = true
    print (itemLink, L[" Added to WhiteList"])
    ns.updateUI()
    ns.updateUIALLBags()
end
ns.addItemToWhiteList = addItemToWhiteList

local function pickItem (...) --self, button and direct
    local tab, button, directInput = ...
    if directInput and button == "LeftButton" then
        local typeMsg, itemID, itemLink  = GetCursorInfo()
        if (typeMsg == "item")then
            --print("item:", itemID, "Item link:", itemLink)
            local encontrado = false
            for k,v in pairs(AutoScrap_CharSettings.noVenderList) do
                if k==itemID then
                    encontrado = true
                end
            end
            if encontrado then
                --[[ local item = Item:CreateFromItemID(itemID)
                item:ContinueOnItemLoad(function()
                    local name = item:GetItemName()
                    local icon = item:GetItemIcon()
                    print(name, icon)
                end) ]]
                --no hacer nada, que no cambie el cursor.
            else
                ClearCursor();
                addItemToWhiteList(itemID, itemLink)
                ns.limpiarCacheAS()
                --ns.scrapIventory() --por algún motivo se recibe BAG_UPDATE en el cursor, así que no hace falta.
            end
        end
    end
end
local dropFrame = ns.Utils.crearFrameConTextura(z,136,290,388,473,1)
dropFrame:SetPoint("TOP", z, "TOP", 0, -20)

local topIcon = CreateFrame ("Frame",nil,z)
topIcon:SetPoint("CENTER", dropFrame, "CENTER", 0, -3)
topIcon:SetSize(50,50)
--[[ local texBg = topIcon:CreateTexture()
texBg:SetAllPoints(topIcon)
texBg:SetColorTexture(1,1,1 , 0.9); ]]
topIcon:SetScript("OnMouseUp",pickItem)

local backGround = CreateFrame ("Frame",nil,z)
backGround:SetPoint("TOPLEFT", dropFrame, "BOTTOMLEFT", 0, -5)
backGround:SetPoint("BOTTOMRIGHT", z, "BOTTOMRIGHT", -53+25, 65)
local texBg = backGround:CreateTexture()
texBg:SetAllPoints(backGround)
texBg:SetColorTexture(0,0,0 , 0.3); --rgb ]]
backGround:SetScript("OnMouseUp",pickItem)

--[[ local scrollFrame = CreateFrame("ScrollFrame", nil, z, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", dropFrame, "BOTTOMLEFT", 0, -5)
scrollFrame:SetPoint("BOTTOMRIGHT", z, "BOTTOMRIGHT", -53, 65) ]]
scrollFrame = CreateFrame("ScrollFrame", nil, z, "UIPanelScrollFrameTemplate")
scrollFrame:SetPoint("TOPLEFT", dropFrame, "BOTTOMLEFT", 0, -5)
scrollFrame:SetPoint("BOTTOMRIGHT", z, "BOTTOMRIGHT", -53, 65)
--scrollFrame:SetClipsChildren(true) -- poner después de los hijos o no va.

local childScrollFrame = CreateFrame ("Frame",nil,scrollFrame)
childScrollFrame:SetSize(400,12) --adjust
scrollFrame:SetScrollChild(childScrollFrame)

local protectSoulBind = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
protectSoulBind:SetPoint("TOPLEFT", scrollFrame, "BOTTOMLEFT", 11, 0);
protectSoulBind.text:SetText(L["Protect SoulBinds"]);
protectSoulBind:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectSouldBinds = self:GetChecked()
    ns.toggleAutoScrapOff()
end)
local function setProtectSouldBind (value)
    protectSoulBind:SetChecked(value)
end
ns.setProtectSouldBind = setProtectSouldBind
    


local dataFrameBuffer = {}
local function borrarItemDeLaLista(frame, key, value)
    frame:Hide()
    dataFrameBuffer[key] = nil
    local itemLink = AutoScrap_CharSettings.noVenderList[key]
    AutoScrap_CharSettings.noVenderList[key] = nil
    ns.newChanges = true
    ns.limpiarCacheAS()
    print (itemLink, L[" Removed from WhiteList"])
    ns.updateUI()
    ns.scrapIventory()
    ns.updateUIALLBags()
end

function ns.removeItemFromWhiteList(itemID,itemLink) --for outsideof AutoScrapNoVender.lua (keybinding basically)
    for k,v in pairs(AutoScrap_CharSettings.noVenderList) do
        if k == itemID then
            if dataFrameBuffer[k] then
                dataFrameBuffer[k]:Hide()
                dataFrameBuffer[k] = nil
            end
            AutoScrap_CharSettings.noVenderList[itemID] = nil
            ns.newChanges = true
            print (itemLink, L[" Removed from WhiteList"])
            --ns.limpiarCacheAS()
            ns.updateUI()
            --ns.scrapIventory() como se llama desde otra funcion en AutoScrapJunkSystem para no hacerlo 2 veces.
            ns.updateUIALLBags()
            break
        end
    end
end

local function escribirItemsEnScrollPanel()
    if ns.newChanges then
        local previousFrame = childScrollFrame
        local previousNAme = "child"
        for k,v in pairs(dataFrameBuffer) do
            --print(k,v)
            dataFrameBuffer[k]:Hide()
        end
        local count = 0
        local height = 11
        for k,v in pairs(AutoScrap_CharSettings.noVenderList) do
            if not dataFrameBuffer[k] then
                --if previousNAme == "child" then print("child in buffer") end
                dataFrameBuffer[k] = CreateFrame("Frame",nil,childScrollFrame)
                dataFrameBuffer[k].tex = dataFrameBuffer[k]:CreateTexture()
                dataFrameBuffer[k].b = CreateFrame("Button", nil, dataFrameBuffer[k])
                dataFrameBuffer[k].title = dataFrameBuffer[k]:CreateFontString(nil,"OVERLAY","GameFontNormalSmall")
            end
            --print("anclando a ", previousNAme, previousFrame)
            dataFrameBuffer[k]:ClearAllPoints()
            dataFrameBuffer[k].b:ClearAllPoints()
            dataFrameBuffer[k].tex:ClearAllPoints()
            dataFrameBuffer[k].title:ClearAllPoints()
            if previousNAme=="child" then dataFrameBuffer[k]:SetPoint("TOPLEFT", previousFrame, "TOPLEFT", 0, 0) else dataFrameBuffer[k]:SetPoint("TOPLEFT", previousFrame, "BOTTOMLEFT", 0, 0) end
            dataFrameBuffer[k]:SetSize(400,12)
            --dataFrameBuffer[k].b = CreateFrame("Button", nil, dataFrameBuffer[k])
            dataFrameBuffer[k].b:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
            dataFrameBuffer[k].b:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
            dataFrameBuffer[k].b:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
            dataFrameBuffer[k].b:SetSize(12, 12)
            dataFrameBuffer[k].b:SetPoint("TOPLEFT", dataFrameBuffer[k], "TOPLEFT", 0, 0)
            dataFrameBuffer[k].b:SetScript("OnClick", function () borrarItemDeLaLista(dataFrameBuffer[k],k,v) end)

            dataFrameBuffer[k].tex:SetAllPoints(dataFrameBuffer[k])
            if count%2==0 then 
                dataFrameBuffer[k].tex:SetColorTexture(100/255, 100/255, 100/255 , 0); --rgb
            else
                dataFrameBuffer[k].tex:SetColorTexture(255/255, 255/255, 255/255 , 0); --rgb
            end
            --dataFrameBuffer[k].title = dataFrameBuffer[k]:CreateFontString(dataFrameBuffer[k],"OVERLAY","GameFontNormalSmall")
            dataFrameBuffer[k].title:SetPoint("TOPLEFT",dataFrameBuffer[k].b,"TOPRIGHT")
            dataFrameBuffer[k].title:SetJustifyH("LEFT")
            dataFrameBuffer[k].title:SetText(v)
            dataFrameBuffer[k]:SetSize(400,dataFrameBuffer[k].title:GetHeight()+1)
            dataFrameBuffer[k].b:SetSize(dataFrameBuffer[k].title:GetHeight()+1,dataFrameBuffer[k].title:GetHeight()+1)
            dataFrameBuffer[k]:Show()
            dataFrameBuffer[k]:SetScript("OnEnter", function(self)
                GameTooltip:SetOwner( dataFrameBuffer[k], "ANCHOR_NONE" );
                GameTooltip:SetPoint("TOPRIGHT", dataFrameBuffer[k], "TOPLEFT", -10, 0)
                --local uno,dos,calidadEquipo,cuatro,cinco,seis,siete,ocho,slotEquipar,diez,once,classID,subClassID,catorce,quince,diceciseis,diecisiete = GetItemInfo(k)
                GameTooltip:SetHyperlink(v);
                GameTooltip:Show()
            end)
            dataFrameBuffer[k]:HookScript("OnLeave", function(self)
                GameTooltip:Hide()
            end)

            previousFrame = dataFrameBuffer[k]
            previousNAme = k
            count = count + 1;
            height = dataFrameBuffer[k]:GetHeight()
        end
        childScrollFrame:SetSize(400,count*height+5)
        ns.newChanges = false
    end
end
ns.escribirItemsEnScrollPanel = escribirItemsEnScrollPanel
z:Hide()

setTooltip(topIcon,L["White List"],L["All the items in the White List don't be sold EVER. You can put here all your potions, mining pick, special equipment with running abilities, etc.."],L["I recommend you to disable the option Protect Soulbinds if you have a whitelist. I protect you from selling soulbind objects by default, but if you farm inside dungeons you get a lot of souldbind items that you want to sell."],L["If you disable protect soulbinds, i recomend you also to tick iLevel protection to farm new gear for you without taking care to turn AutoScap off at anytime."])
setTooltip(protectSoulBind,L["SouldBind Protection"],L["By default it is enabled to avoid selling bind items on your bags. But if you have a white list with all your Non-Sellable stuff on it, by disable it you gain the ability to sell the crappy bind items on dungeons, bosses etc.."],L["Also I recommend you if you disable SoulBind protection to enable also iLevel protection to forget about if AutoScrap is on when you get new items for you."])

