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,"Protected")
local scrollFrame
local textArea

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 titleFrame = ns.Utils.crearFrameConTextura(z,275,430,327,405,1)
titleFrame:SetPoint("TOP", z, "TOP", -3, -29)

local ilevel = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
--ilevel:SetPoint("TOPLEFT", z, "TOPLEFT", 58, -100) --antes de quitar el botón de limpiar todo
ilevel:SetPoint("TOPLEFT", z, "TOPLEFT", 58, -100)
local itemLevelText = L["ItemLevel: "]
ilevel.text:SetText(itemLevelText..AutoScrap_CharSettings.iLevel);
ilevel:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedIlevel = self:GetChecked()
    ns.toggleAutoScrapOff()
end)
local function setProtectIlevel (value)
    ilevel:SetChecked(value)
end
ns.setProtectIlevel = setProtectIlevel

local dialog
ilevel.bSet = CreateFrame("Button", nil, z, "UIPanelButtonTemplate")
ilevel.bSet:SetPoint("TOPRIGHT",ilevel,"TOPLEFT",0,-5)
ilevel.bSet:SetSize(30, 18)
ilevel.bSet:SetText("Set")
ilevel.bSet:SetScript("OnClick", function(self, button)
	if(button == "LeftButton") then
        dialog:Show()
    end
end)

local ratio = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
ratio:SetPoint("TOPLEFT", ilevel, "TOPLEFT", 0, -22);
local sellRatioText = L["Sell Rate: "]
ratio.text:SetText(sellRatioText..AutoScrap_CharSettings.sellRatio);
ratio:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedRatio = self:GetChecked()
    ns.toggleAutoScrapOff()
end)
local function setProtectRatio (value)
    ratio:SetChecked(value)
end
ns.setProtectRatio = setProtectRatio

local dialog2
ratio.bSet = CreateFrame("Button", nil, z, "UIPanelButtonTemplate")
ratio.bSet:SetPoint("TOPRIGHT",ratio,"TOPLEFT",0,-5)
ratio.bSet:SetSize(30, 18)
ratio.bSet:SetText("Set")
local checkAdv
local setAdvOptions
ratio.bSet:SetScript("OnClick", function(self, button)
	if(button == "LeftButton") then
        if not dialog2:IsShown() then
            if checkAdv:GetChecked() then
                setAdvOptions(true)
            else
                setAdvOptions(false)
            end
            dialog2:Show()
        else
            dialog2:Hide()
        end
    end
end)

local consumables = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
--consumables:SetPoint("TOPLEFT", ratio, "TOPLEFT", -29, -28); --antes de quitar el botón de limpiar todo
consumables:SetPoint("TOPLEFT", ratio, "TOPLEFT", -29, -30);
consumables.text:SetText(L["Consumables"]);
consumables:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedConsumables = self:GetChecked()
    ns.changeItemClass("Consumables",not AutoScrap_CharSettings.protectedConsumables)
    ns.toggleAutoScrapOff()
end)
local function setConsumables (value)
    consumables:SetChecked(value)
end
ns.setConsumables = setConsumables

------------
local recipes = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
recipes:SetPoint("TOPLEFT", consumables, "TOPLEFT", 0, -22);
recipes.text:SetText(L["Recipes"]);
recipes:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedRecipes = self:GetChecked()
    ns.changeItemClass("Recipes",not AutoScrap_CharSettings.protectedRecipes)
    ns.toggleAutoScrapOff()
end)
local function setRecipes (value)
    recipes:SetChecked(value)
end
ns.setRecipes = setRecipes

local cooking = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
cooking:SetPoint("TOPLEFT", recipes, "TOPLEFT", 0, -22);
cooking.text:SetText(L["Cooking"]);
cooking:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedCooking = self:GetChecked()
    ns.changeItemClass("Cooking",not AutoScrap_CharSettings.protectedCooking)
    ns.toggleAutoScrapOff()
end)
local function setCooking (value)
    cooking:SetChecked(value)
end
ns.setCooking = setCooking

local leather = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
leather:SetPoint("TOPLEFT", cooking, "TOPLEFT", 0, -22);
leather.text:SetText(L["Leather"]);
leather:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedLeather = self:GetChecked()
    ns.changeItemClass("Leather",not AutoScrap_CharSettings.protectedLeather)
    ns.toggleAutoScrapOff()
end)
local function setLeather (value)
    leather:SetChecked(value)
end
ns.setLeather = setLeather

local cloths = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
cloths:SetPoint("TOPLEFT", recipes, "TOPRIGHT", 45, 0);
cloths.text:SetText(L["Cloths"]);
cloths:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedCloths = self:GetChecked()
    ns.changeItemClass("Cloths",not AutoScrap_CharSettings.protectedCloths)
    ns.toggleAutoScrapOff()
end)
local function setCloths (value)
    cloths:SetChecked(value)
end
ns.setCloths = setCloths

local herbs = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
herbs:SetPoint("TOPLEFT", cooking, "TOPRIGHT", 45, 0);
herbs.text:SetText(L["Herbs"]);
herbs:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedHerbs = self:GetChecked()
    ns.changeItemClass("Herbs",not AutoScrap_CharSettings.protectedHerbs)
    ns.toggleAutoScrapOff()
end)
local function setHerbs (value)
    herbs:SetChecked(value)
end
ns.setHerbs = setHerbs

local mining = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
mining:SetPoint("TOPLEFT", leather, "TOPRIGHT", 45, 0);
mining.text:SetText(L["Mining"]);
mining:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedMining = self:GetChecked()
    ns.changeItemClass("Mining",not AutoScrap_CharSettings.protectedMining)
    ns.toggleAutoScrapOff()
end)
local function setMining (value)
    mining:SetChecked(value)
end
ns.setMining = setMining

local jewelry = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
jewelry:SetPoint("TOPLEFT", leather, "TOPLEFT", 0, -22);
jewelry.text:SetText(L["Jewelry"]);
jewelry:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedJewelry = self:GetChecked()
    ns.changeItemClass("Jewelry",not AutoScrap_CharSettings.protectedJewelry)
    ns.toggleAutoScrapOff()
end)
local function setJewelry (value)
    jewelry:SetChecked(value)
end
ns.setJewelry = setJewelry

local inscription = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
inscription:SetPoint("TOPLEFT", jewelry, "TOPRIGHT", 45, 0);
inscription.text:SetText(L["Inscript."]);
inscription:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedInscription = self:GetChecked()
    ns.changeItemClass("Inscription",not AutoScrap_CharSettings.protectedInscription)
    ns.toggleAutoScrapOff()
end)
local function setInscription (value)
    inscription:SetChecked(value)
end
ns.setInscription = setInscription

local boxes = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
boxes:SetPoint("TOPLEFT", jewelry, "TOPLEFT", 20, -27);
boxes.text:SetText(L["Locked Boxes"]);
boxes:SetScript("OnClick", function (self)
    AutoScrap_CharSettings.protectedBoxes = self:GetChecked()
    --ns.changeItemClass("Mining",not AutoScrap_CharSettings.protectedMining) no hace falta porque el tipo de boxes es 15,0 y se vende siempre. (miscelaneous, junk general)
    ns.toggleAutoScrapOff()
end)
local function setBoxes (value)
    boxes:SetChecked(value)
end
ns.setBoxes = setBoxes


------------


--[[ local limpiarTODO = CreateFrame("Button", nil, z, "UIPanelButtonTemplate")
limpiarTODO:SetPoint("CENTER",z,"TOP",0,-272)
limpiarTODO:SetSize(130, 40)
limpiarTODO:SetText(L["Empty the entire\nScrap list"])
limpiarTODO:SetScript("OnClick", function(self, button)
	if(button == "LeftButton") then
        --ns.toggleAutoScrapOff()
        if Scrap_Sets and Scrap_Sets.list then
            Scrap_Sets["list"] = {}
        end
        if Scrap_CharSets and Scrap_CharSets.list then
            Scrap_CharSets["list"] = {}
        end
        PlaySound(148283)
        ReloadUI()
    end
end) ]]


local function setIlevel()
    ilevel.text:SetText(itemLevelText..AutoScrap_CharSettings.iLevel);
end
ns.setIlevel = setIlevel

local function setSellRatio()
    if not AutoScrap_CharSettings.protectedAdvancedOptions then
        ratio.text:SetText(sellRatioText..AutoScrap_CharSettings.sellRatio);
    else
        ratio.text:SetText(sellRatioText.."Adv");
    end
end
ns.setSellRatio = setSellRatio

--[[ local applyRatio = CreateFrame("CheckButton", nil, z, "UICheckButtonTemplate");
applyRatio:SetPoint("TOPLEFT",ratio, "TOPLEFT", 0, -22);
applyRatio.text:SetText("");
applyRatio.text:SetJustifyH("LEFT") ]]



--diálogo ilevel
dialog = CreateFrame("Frame", nil, UIParent, "BackdropTemplate")
dialog:SetPoint("CENTER",0,-50)
dialog:SetSize(150, 40)
dialog:SetFrameStrata("DIALOG")
dialog:SetClipsChildren(true)
dialog:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
    edgeFile = "Interface\\PVPFrame\\UI-Character-PVP-Highlight",
    edgeSize = 16,
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
dialog:SetBackdropBorderColor(225/255, 143/256, 60/255  , 0.7)
--:SetVertexColor(r, g, b [, a])

local function comprobarNumeros(value)
    local err = tonumber(value)
    if(err and err%1==0 and err>=1 and err<=999)then
        AutoScrap_CharSettings.iLevel=err
        ns.limpiarCacheAS()
        ns.updateUI()
        ns.toggleAutoScrapOff()
    end
end

local eb = CreateFrame("EditBox", nil, dialog)
eb:SetSize(100, 40)
eb:SetMaxLetters(7)
eb:SetPoint("LEFT", dialog, "LEFT", 10, 0)
eb:SetMultiLine(false)
eb:SetAutoFocus(true) -- probar false
eb:SetFontObject("ChatFontNormal")
eb:SetText("380")
eb:HighlightText(0)
eb:SetScript("OnEscapePressed",function() dialog:Hide() end)
eb:SetScript("OnEnterPressed",function() dialog:Hide() comprobarNumeros(eb:GetText()) end)

local dialogButton = CreateFrame("Button", nil, dialog, "UIPanelButtonTemplate")
--dialogButton:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
--dialogButton:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
--dialogButton:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
dialogButton:SetSize(30, 22)
dialogButton:SetText("Set")
dialogButton:SetPoint("RIGHT", dialog, "RIGHT", -8, 0)
dialogButton:SetScript("OnClick", function() dialog:Hide() comprobarNumeros(eb:GetText()) end)

dialog:Hide()
--fin diálogo ilevel

--diálogo ratio
dialog2 = CreateFrame("Frame", "AutoScrapDialog2-Rates", UIParent, "BackdropTemplate")
dialog2:SetPoint("CENTER",0,-100)
dialog2:SetSize(468, 300)
dialog2:SetFrameStrata("DIALOG")
--dialog2:SetClipsChildren(true)
dialog2:SetMovable(true)
dialog2:EnableMouse(true)
dialog2:SetClampedToScreen(true)
dialog2:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
    edgeFile = "Interface\\PVPFrame\\UI-Character-PVP-Highlight",
    edgeSize = 16,
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
local closeEditOpened
dialog2:SetBackdropBorderColor(225/255, 143/256, 60/255  , 0.7)
dialog2:SetScript("OnMouseDown",function (self,button) if button=="LeftButton" then dialog2:StartMoving() end end); --StartSizing()
dialog2:SetScript("OnMouseUp",function (self,button) if button=="LeftButton" then dialog2:StopMovingOrSizing() end end);
dialog2:SetScript("OnHide",function (self) closeEditOpened() end);
dialog2:SetToplevel(true)
--:SetVertexColor(r, g, b [, a])
tinsert(UISpecialFrames, "AutoScrapDialog2-Rates")

local function comprobarNumerosRate(value)
    value=value:gsub("%,", ".")
    local err = tonumber(value)
    if(err and err>=0.001 and err<=1)then
        err = math.floor(err * 1000+ 0.5) / 1000
        AutoScrap_CharSettings.sellRatio=err
        ns.limpiarCacheAS()
        ns.updateUI()
        ns.toggleAutoScrapOff()
    end
end

--[[ local title = CreateFrame("Frame", nil, dialog2, "BackdropTemplate")
title:SetSize(180, 32)
title:SetPoint("TOP",dialog2,"TOP",0,15) --7
title:SetBackdrop({
    bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background-dark",
    edgeFile = "Interface\\PVPFrame\\UI-Character-PVP-Highlight",
    edgeSize = 16,
    insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
title:SetBackdropBorderColor(225/255, 143/256, 60/255  , 0.7)

local titleText = title:CreateFontString (nil, "OVERLAY", "GameFontHighlight")
titleText:SetPoint("CENTER",title,"CENTER")
titleText:SetText(L["Sell Rate: "]) ]]

local descText = dialog2:CreateFontString (nil, "OVERLAY", "GameFontNormal")
descText:SetPoint("TOPLEFT",dialog2,"TOPLEFT",15,-15)
descText:SetJustifyH("LEFT")
descText:SetText(L["Sell Rate: "])

local eb2backFrame = CreateFrame("Frame", nil, dialog2)
eb2backFrame:SetPoint("LEFT", descText, "RIGHT", 5, 0)
eb2backFrame:SetSize(50, 18)
eb2backFrame.tex = eb2backFrame:CreateTexture()
eb2backFrame.tex:SetAllPoints(eb2backFrame)
eb2backFrame.tex:SetColorTexture(255/255, 20/255, 147/255 , 0.2); --rgb

local eb2 = CreateFrame("EditBox", nil, dialog2)
eb2:SetSize(50, 18)
eb2:SetMaxLetters(5)
eb2:SetPoint("LEFT", eb2backFrame, "LEFT", 0, 0)
eb2:SetMultiLine(false)
eb2:SetAutoFocus(true) -- probar false
eb2:SetFontObject("ChatFontNormal")
eb2:SetText("0.2")
eb2:HighlightText(0)
eb2:SetScript("OnEscapePressed",function() dialog2:Hide() end)
eb2:SetScript("OnEnterPressed",function() dialog2:Hide() comprobarNumerosRate(eb2:GetText()) end)

local dialog2Button = CreateFrame("Button", nil, dialog2, "UIPanelButtonTemplate")
dialog2Button:SetSize(30, 22)
dialog2Button:SetText("Set")
dialog2Button:SetPoint("LEFT", eb2backFrame, "RIGHT", 5, 0)
dialog2Button:SetScript("OnClick", function() dialog2:Hide() comprobarNumerosRate(eb2:GetText()) end)

checkAdv = CreateFrame("CheckButton", nil, dialog2, "UICheckButtonTemplate");
checkAdv:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -2);
checkAdv.text:SetText(L["Advance Options"]);
checkAdv:SetScript("OnClick", function (self)
    if self:GetChecked() then
        setAdvOptions(true)
        AutoScrap_CharSettings.protectedAdvancedOptions = true
    else
        setAdvOptions(false)
        AutoScrap_CharSettings.protectedAdvancedOptions = false
    end
    ns.updateUI()
    ns.toggleAutoScrapOff()
end)


local dialog2ButtonClose = ns.Utils.crearBoton(dialog2,65,22,L["Close"],function () dialog2:Hide() end)
dialog2ButtonClose:SetPoint("BOTTOM", dialog2, "BOTTOM", 0, 15)

dialog2:SetHeight(68+28)
dialog2ButtonClose:Hide()

local dataFrames = {} --AutoScrap_CharSettings.ratioData
--[[ table.insert(data,{gold=400,rate=0.08})
table.insert(data,{gold=800,rate=0.06})
table.insert(data,{gold=1000,rate=0.05})
table.insert(data,{gold=2000,rate=0.04})
table.insert(data,{gold=nil,rate=0.02}) ]]

local rowHeight = 30

closeEditOpened = function ()
    for k,v in ipairs(dataFrames)do
        if v["editFrame"] then
            v["editFrame"]:Hide()
        end
    end
end

local function hideAllButtons(flag)
    if flag then
        for k,v in ipairs(dataFrames)do
            if v["frame"].c then
                v["frame"].c:Hide()
            end
            if v["frame"].edit then
                v["frame"].edit:Hide()
            end
            if v["frame"].add then
                v["frame"].add:Hide()
            end
        end
    else
        for k,v in ipairs(dataFrames)do
            if v["frame"].c then
                v["frame"].c:Show()
            end
            if v["frame"].edit then
                v["frame"].edit:Show()
            end
            if v["frame"].add then
                v["frame"].add:Show()
                v["frame"].c:Hide()
            end
        end
    end
end

local insertFrameRow
local function numberAndRate(gold, rate, key)
    --print (gold,rate,key)
    rate=rate:gsub("%,", ".")
    local err = tonumber(rate)
    local isRate, isGold = false, false
    if(err and err>=0.001 and err<=1)then
        err = math.floor(err * 1000+ 0.5) / 1000
        --[[ ns.limpiarCacheAS()
        ns.updateUI() ]]
        isRate = true
    end
    local goldNumber = tonumber(gold)
    if gold then
        if(goldNumber and goldNumber%1==0 and goldNumber>=1 and goldNumber<10000000)then
            isGold = true
        end
    else
        isGold = true
    end
    if isGold and isRate then
        local skip = false
        if goldNumber and AutoScrap_CharSettings.ratioData[key+1] and AutoScrap_CharSettings.ratioData[key+1]["gold"] and goldNumber>=AutoScrap_CharSettings.ratioData[key+1]["gold"] then
            skip = true
        end
        if goldNumber and AutoScrap_CharSettings.ratioData[key-1] and goldNumber<=AutoScrap_CharSettings.ratioData[key-1]["gold"] then
            skip = true
        end
        if not skip then
            if AutoScrap_CharSettings.ratioData[key+1] then
                AutoScrap_CharSettings.ratioData[key]["gold"] = goldNumber
            end
            AutoScrap_CharSettings.ratioData[key]["rate"] = err
            insertFrameRow()
            ns.toggleAutoScrapOff()
            --[[ ns.limpiarCacheAS()
            ns.updateUI() ]]
        end
    end
end

local function removeKey(k,v)
    if AutoScrap_CharSettings.ratioData[k+1] and (AutoScrap_CharSettings.ratioData[k-1] or AutoScrap_CharSettings.ratioData[k+1]["gold"])then
        dataFrames[k]["frame"]:Hide()
        dataFrames[k]["editFrame"]:Hide()
        table.remove(dataFrames,k)
        table.remove(AutoScrap_CharSettings.ratioData,k)
        insertFrameRow()
        ns.toggleAutoScrapOff()
    end
end

local function addData()
    local lastRow = 0
    for k,v in ipairs(AutoScrap_CharSettings.ratioData) do
            lastRow = k
    end
    if lastRow>1 and lastRow<20 and AutoScrap_CharSettings.ratioData[lastRow-1]["gold"]<9999999 then
        table.insert(AutoScrap_CharSettings.ratioData,lastRow,{gold=AutoScrap_CharSettings.ratioData[lastRow-1]["gold"]+1,rate=0.20})
        table.insert(dataFrames,lastRow,{})
        insertFrameRow()
        ns.toggleAutoScrapOff()
    end
end

insertFrameRow =  function ()
    local count = 0
    for k,v in ipairs(dataFrames) do
        count = count +1
        if not v["frame"] then
            v["frame"] = CreateFrame("Frame",nil,dialog2)
            v["frame"].c = CreateFrame("Button", nil, v["frame"])
            v["frame"].edit = CreateFrame("Button", nil, v["frame"], "UIPanelButtonTemplate")
            v["editFrame"] = CreateFrame("Frame",nil,v["frame"])
            v["editFrame"].eb = CreateFrame("EditBox", nil, v["editFrame"])
            v["editFrame"].eb2backFrame = CreateFrame("Frame", nil, v["editFrame"])
            v["editFrame"].eb2 = CreateFrame("EditBox", nil, v["editFrame"])
            v["editFrame"].eb2backFrame2 = CreateFrame("Frame", nil, v["editFrame"])
            v["editFrame"].save = CreateFrame("Button", nil, v["editFrame"], "UIPanelButtonTemplate")
            v["frame"].tex = v["frame"]:CreateTexture()
            v["editFrame"].tex = v["editFrame"]:CreateTexture()
            v["editFrame"].text = v["editFrame"]:CreateFontString (nil, "OVERLAY", "GameFontNormal")
            v["frame"].text = v["frame"]:CreateFontString (nil, "OVERLAY", "GameFontNormal")
            v["editFrame"].eb2backFrame.tex = v["editFrame"].eb2backFrame:CreateTexture()
            v["editFrame"].eb2backFrame2.tex = v["editFrame"].eb2backFrame2:CreateTexture()
            v["editFrame"].text2 = v["editFrame"]:CreateFontString (nil, "OVERLAY", "GameFontNormal")
            v["editFrame"].text3 = v["editFrame"]:CreateFontString (nil, "OVERLAY", "GameFontNormal")
        else
            v["frame"]:Show()
            v["frame"].c:Show()
            v["frame"].edit:Show()
        end

        v["frame"]:ClearAllPoints()
        v["frame"].c:ClearAllPoints()
        v["frame"].edit:ClearAllPoints()
        v["editFrame"]:ClearAllPoints()
        v["editFrame"].eb:ClearAllPoints()
        v["editFrame"].eb2backFrame:ClearAllPoints()
        v["editFrame"].eb2:ClearAllPoints()
        v["editFrame"].eb2backFrame2:ClearAllPoints()
        v["editFrame"].save:ClearAllPoints()
        v["frame"]:SetSize(370+68,rowHeight)
        v["frame"].tex:SetAllPoints(v["frame"])
        --v["frame"].tex:SetColorTexture(math.random(0,255)/255, math.random(0,255)/255, math.random(0,255)/255 , 0.1)
        v["frame"].tex:SetColorTexture(0/255, 0/255, 0/255 , 0.3)
        v["frame"].c:SetNormalTexture(130832) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Up"
        v["frame"].c:SetPushedTexture(130830) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Down"
        v["frame"].c:SetHighlightTexture(130831) --"Interface\\Buttons\\UI-Panel-MinimizeButton-Highlight"
        v["frame"].c:SetSize(32, 32)
        v["frame"].c:SetPoint("RIGHT", v["frame"], "RIGHT", 7, 0)
        v["frame"].c:SetScript("OnClick", function() local value, key = AutoScrap_CharSettings.ratioData[k],k removeKey(key,value) ns.toggleAutoScrapOff() end)
        v["frame"].edit:SetSize(40, 22)
        v["frame"].edit:SetText("Edit")
        v["frame"].edit:SetPoint("RIGHT", v["frame"].c, "LEFT", 5, 0)
        v["frame"].edit:SetScript("OnClick", function() if AutoScrap_CharSettings.ratioData[k]["gold"] then v["editFrame"].eb:SetText(AutoScrap_CharSettings.ratioData[k]["gold"]) end v["editFrame"].eb2:SetText(AutoScrap_CharSettings.ratioData[k]["rate"]) v["editFrame"]:Show() hideAllButtons(true) ns.toggleAutoScrapOff() end)
        v["editFrame"]:SetSize(378,rowHeight)
        v["editFrame"].tex:SetAllPoints(v["editFrame"])
        v["editFrame"].tex:SetColorTexture(0,0,0, 0.7)
        v["editFrame"]:SetPoint("LEFT",v["frame"],"RIGHT",10,0)
        v["editFrame"].text:SetPoint("LEFT",v["editFrame"],"LEFT",5,0)
        v["editFrame"].eb2backFrame:SetPoint("LEFT", v["editFrame"].text, "RIGHT", 5, 0)
        v["editFrame"].eb2backFrame:SetSize(63, 18)
        v["editFrame"].eb2backFrame.tex:SetAllPoints(v["editFrame"].eb2backFrame)
        v["editFrame"].eb2backFrame.tex:SetColorTexture(255/255, 20/255, 147/255 , 0.2)
        v["editFrame"].eb:SetSize(63, 18)
        v["editFrame"].eb:SetMaxLetters(7)
        v["editFrame"].eb:SetPoint("LEFT", v["editFrame"].eb2backFrame, "LEFT", 0, 0)
        v["editFrame"].eb:SetMultiLine(false)
        v["editFrame"].eb:SetAutoFocus(true) -- probar false
        v["editFrame"].eb:SetFontObject("ChatFontNormal")
        v["editFrame"].eb:HighlightText(0)
        v["editFrame"].eb:SetScript("OnEscapePressed",function() dialog2:Hide() end)
        --v["editFrame"].eb:SetScript("OnEnterPressed",function() dialog2:Hide() comprobarNumerosRate(v["editFrame"].eb:GetText()) end)
        v["editFrame"].text2:SetPoint("LEFT",v["editFrame"].eb,"RIGHT",5,0)
        v["editFrame"].eb2backFrame2:SetPoint("LEFT", v["editFrame"].text2, "RIGHT", 5, 0)
        v["editFrame"].eb2backFrame2:SetSize(40, 18)
        v["editFrame"].eb2backFrame2.tex:SetAllPoints(v["editFrame"].eb2backFrame2)
        v["editFrame"].eb2backFrame2.tex:SetColorTexture(255/255, 20/255, 147/255 , 0.2)
        v["editFrame"].eb2:SetSize(40, 18)
        v["editFrame"].eb2:SetMaxLetters(7)
        v["editFrame"].eb2:SetPoint("LEFT", v["editFrame"].eb2backFrame2, "LEFT", 0, 0)
        v["editFrame"].eb2:SetMultiLine(false)
        v["editFrame"].eb2:SetAutoFocus(true) -- probar false
        v["editFrame"].eb2:SetFontObject("ChatFontNormal")
        v["editFrame"].eb2:HighlightText(0)
        v["editFrame"].save:SetSize(40, 22)
        v["editFrame"].save:SetText("Save")
        v["editFrame"].save:SetPoint("LEFT", v["editFrame"].eb2, "RIGHT", 5, 0)
        v["editFrame"].save:SetScript("OnClick", function(self) local value,key = AutoScrap_CharSettings.ratioData[k],k self:GetParent():Hide() hideAllButtons(false) local tg if value["gold"] then tg=v["editFrame"].eb:GetText() else tg=nil end  numberAndRate(tg,v["editFrame"].eb2:GetText(),key) end)
        v["editFrame"].eb2:SetScript("OnEscapePressed",function() dialog2:Hide() end)

        if AutoScrap_CharSettings.ratioData[k+1] and AutoScrap_CharSettings.ratioData[k+1]["gold"] then
            v["editFrame"].text3:SetText("|cFFff4e3cMax: "..(AutoScrap_CharSettings.ratioData[k+1]["gold"]-1).."|r")
        else
            v["editFrame"].text3:SetText("|cFFff4e3cMax: ".."9999999".."|r")
        end
        v["editFrame"].text3:SetPoint("TOP",v["editFrame"].eb,"BOTTOM",0,-10)
        v["editFrame"].text3:Show()
        if k == 1 then
            v["frame"]:SetPoint("TOPLEFT",checkAdv,"BOTTOMLEFT", 0 , -10)
            v["frame"].text:SetText(L["From |cFFe1740d"].."0"..L["g|r To |cFFe1740d"]..AutoScrap_CharSettings.ratioData[k]["gold"]..L["g|r  protected Sell Rate: |cFFcc74ff"]..AutoScrap_CharSettings.ratioData[k]["rate"].."|r")
            v["editFrame"].text:SetText(L["From |cFFe1740d"].."0"..L["g|r To:"])
            v["editFrame"].eb:SetText(AutoScrap_CharSettings.ratioData[k]["gold"])
            v["editFrame"].text2:SetText(L["Sell Rate:"])
            v["editFrame"].eb2:SetText(AutoScrap_CharSettings.ratioData[k]["rate"])
        else
            v["frame"]:SetPoint("TOPLEFT",dataFrames[k-1]["frame"],"BOTTOMLEFT", 0 , -2)
            if AutoScrap_CharSettings.ratioData[k]["gold"] then
                v["frame"].text:SetText(L["From |cFFe1740d"]..(AutoScrap_CharSettings.ratioData[k-1]["gold"])..L["g|r To |cFFe1740d"]..AutoScrap_CharSettings.ratioData[k]["gold"]..L["g|r  protected Sell Rate: |cFFcc74ff"]..AutoScrap_CharSettings.ratioData[k]["rate"].."|r")
                v["editFrame"].text:SetText(L["From |cFFe1740d"]..(AutoScrap_CharSettings.ratioData[k-1]["gold"])..L["g|r To:"])
                v["editFrame"].eb:SetText(AutoScrap_CharSettings.ratioData[k]["gold"])
                v["editFrame"].text2:SetText(L["Sell Rate:"])
                v["editFrame"].eb2:SetText(AutoScrap_CharSettings.ratioData[k]["rate"])
            else
                if not v["frame"].add then
                    v["frame"].add = CreateFrame("Button", nil, v["frame"], "UIPanelButtonTemplate")
                end
                v["frame"].add:ClearAllPoints()
                v["frame"].add:SetSize(40, 22)
                v["frame"].add:SetText("Add")
                v["frame"].add:SetPoint("RIGHT", v["frame"], "RIGHT", 0, 0)
                v["frame"].add:SetScript("OnClick", function() addData() ns.toggleAutoScrapOff() end)
                v["frame"].edit:SetPoint("RIGHT", v["frame"].add, "LEFT", 0, 0)
                v["frame"].c:Hide()
                v["frame"].text:SetText(L["Over |cFFe1740d"]..(AutoScrap_CharSettings.ratioData[k-1]["gold"])..L["g|r  protected Sell Rate: |cFFcc74ff"]..AutoScrap_CharSettings.ratioData[k]["rate"].."|r")
                v["editFrame"].text:SetText(L["Over |cFFe1740d"]..(AutoScrap_CharSettings.ratioData[k-1]["gold"]).."g|r")
                v["editFrame"].eb:Hide()
                v["editFrame"].eb2backFrame:Hide()
                v["editFrame"].text2:SetPoint("LEFT",v["editFrame"].text,"RIGHT",5,0)
                v["editFrame"].text2:SetText(L["Sell Rate:"])
                v["editFrame"].eb2:SetText(AutoScrap_CharSettings.ratioData[k]["rate"])
                v["editFrame"].text3:Hide()
            end
        end
        v["frame"].text:SetPoint("LEFT",v["frame"] , "LEFT", 2,0)
        closeEditOpened()
    end
    dialog2:SetHeight(85+22+(count*(rowHeight+2)))
    return count
end

local function hideFrameRow()
    for k,v in ipairs(dataFrames) do
        if v["frame"] then
            v["frame"]:Hide()
        end
    end
end

local function setDataFrames()
    for k,v in ipairs(AutoScrap_CharSettings.ratioData) do
        dataFrames[k]={}
    end
end
ns.setRatioData = setDataFrames

setAdvOptions = function (checked)
    if checked then
        dialog2:SetHeight(300)
        dialog2:SetWidth(468)
        --title:Show()
        checkAdv:SetChecked(true)
        descText:Hide()
        eb2backFrame:Hide()
        eb2:Hide()
        dialog2Button:Hide()
        checkAdv:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -2+18);
        dialog2:SetHeight(85+22+(insertFrameRow()*(rowHeight+2)))
        dialog2ButtonClose:Show()
    else
        --title:Hide()
        dialog2:SetHeight(68+28-23)
        dialog2:SetWidth(210-30)
        checkAdv:SetChecked(false)
        descText:Show()
        eb2backFrame:Show()
        eb2:Show()
        dialog2Button:Show()
        checkAdv:SetPoint("TOPLEFT", descText, "BOTTOMLEFT", 0, -2);
        hideFrameRow()
        dialog2ButtonClose:Hide()
    end
end
ns.setAdvOptions = setAdvOptions
dialog2:Hide()
--fin diálogo ratio

z:Hide()

setTooltip(titleFrame,L["Your Protection tab"],L["All the setup here will work as opposite of Main Window, items won't be sold if they are equal or higher than the values of iLevel and ratio. Also you can protect some item class, or unprotect them if you don't care about them."])
setTooltip(ilevel,L["Item Level"],L["If you enable it you will keep on your inventory all items with this iLevel or higher."],L["If you check Accesories, whites or grays in the MainWindow to be always sold, they won't be affected by this stat, they have their own config."])
setTooltip(ratio,L["Sell Ratio on Auction House"],L["If you enable it you will keep on your inventory all the items with that ratio of selling in the acution house or higher"],L["If you check Accesories, whites or grays in the MainWindow to be always sold, they won't be affected by this stat, they have their own config."])
setTooltip(consumables,L["Consumables"],L["Protect all the consumable items for being sold, as potions, food that heals you, etc.."],L["If you have a white list with your inventory consumables, I recommend you to untick this group, because most consumables are junk."])
setTooltip(recipes,L["Recipes"],L["Protect all the recipes for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(cooking,L["Cooking"],L["Protect all the cooking stuff for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(leather,L["Leather"],L["Protect all the leather for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(cloths,L["Cloths"],L["Protect all the Cloths (tradeskill cloths) for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(herbs,L["Herbs"],L["Protect all the herbs for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(mining,L["Mining"],L["Protect all the metal and ore for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
--setTooltip(limpiarTODO,L["Empty Scrap List"],L["This will delete all the current Scrap item list. Usually you want to do this when the prices has changed after long time playing."],L["**Requiere reload the UI**"])
setTooltip(jewelry, L["Jewelry"], L["Protect all the jewel crafting struff for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(inscription, L["Inscription"], L["Protect all the inscription crafting struff for being sold."],L["Remember that untick a group doesn't mean you sell all of them, they will be sold only if they worth less value of MainWindow slider gold"])
setTooltip(boxes, L["Locked Boxes"], L["Protect all the locked boxes for being sold."],L["If you untick them, all the locked boxes will be keeped in your inventory."])