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

local function savePosition(frame)
    AutoScrap_CharSettings.TimerFrame.startPoint, _ , AutoScrap_CharSettings.TimerFrame.relativePoint, AutoScrap_CharSettings.TimerFrame.xOfs, AutoScrap_CharSettings.TimerFrame.yOfs = frame:GetPoint()
end

local wsize, hsize = 128,154
local r,g,b,a = 200,150,13,0.5
local outterFrame = CreateFrame("Frame",nil,UIParent)
outterFrame:SetSize(wsize,hsize)
outterFrame:SetClampedToScreen(true)
outterFrame:SetMovable(true)
outterFrame:SetResizable(false)
outterFrame:EnableMouse(true)
outterFrame:SetToplevel(true)
outterFrame:SetFrameStrata("HIGH") --MEDIUM -- la barra secundaria de skills del juego sale por encima al redimensionar y evita pulsar los botoness
outterFrame:SetScript("OnMouseDown",function (self,button) if button=="LeftButton" then outterFrame:StartMoving() end end)
outterFrame:SetScript("OnMouseUp",function (self,button) if button=="LeftButton" then outterFrame:StopMovingOrSizing() savePosition(self) end end)
outterFrame:SetPoint("CENTER")
--[[ outterFrame.tex = outterFrame:CreateTexture()
outterFrame.tex:SetAllPoints(outterFrame)
outterFrame.tex:SetColorTexture(r/255, b/255, b/255 , a) ]] --255/255, 20/255, 147/255 , 0.2

outterFrame.texTop = outterFrame:CreateTexture(nil, "BACKGROUND")
outterFrame.texTop:SetSize(wsize,22)
outterFrame.texTop:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
outterFrame.texTop:SetTexCoord(375/512,504/512,231/512,253/512)
outterFrame.texTop:SetPoint("TOP")

outterFrame.texBottom = outterFrame:CreateTexture(nil, "BACKGROUND")
outterFrame.texBottom:SetSize(wsize,22)
outterFrame.texBottom:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
outterFrame.texBottom:SetTexCoord(375/512,504/512,(341-22)/512,341/512)
outterFrame.texBottom:SetPoint("BOTTOM")

outterFrame.texMid = outterFrame:CreateTexture(nil, "BACKGROUND")
outterFrame.texMid:SetSize(wsize,1)
outterFrame.texMid:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
outterFrame.texMid:SetTexCoord(375/512,504/512,259/512,320/512)
outterFrame.texMid:SetPoint("TOP",outterFrame.texTop ,"BOTTOM", 0 , 0)
outterFrame.texMid:SetPoint("BOTTOM",outterFrame.texBottom ,"TOP", 0 , 0)

function ns.showMyTimerCountDown()
    outterFrame:Show()
end
local myTimerRunning = false
local timerRunning = false
local stopMyTimerNow
function ns.hideMyTimerCountDown()
    if timerRunning then
        return
    end
    if myTimerRunning then
        stopMyTimerNow()
    end
    outterFrame:Hide()
end
function ns.toggleMyTimerCountDown()
    if outterFrame:IsVisible() then
        ns.hideMyTimerCountDown()
    else
        ns.showMyTimerCountDown()
    end
end
ns.hideMyTimerCountDown()

local z = CreateFrame("Frame",nil,outterFrame)
z:SetSize(wsize,hsize)
z.tex = z:CreateTexture()
z:SetPoint("CENTER")

local buttonSize = 13
local closeButton = ns.crearBoton3Fases(z,"Interface\\AddOns\\AutoScrap\\images\\mailpanel", 112,126,140,153, function ()
    ns.hideMyTimerCountDown()
end)
closeButton:SetPoint("TOPRIGHT", z, "TOPRIGHT", -3, -3)
local optionsButton = ns.crearBoton3Fases(z,"Interface\\AddOns\\AutoScrap\\images\\mailpanel", 112,126,122,135, function ()
    ns.toggleTimerADVOptions()
end)
optionsButton:SetPoint("RIGHT", closeButton, "LEFT", -2, 0)

z.frameLeaderTop = CreateFrame("Frame", nil, z)
z.frameLeaderTop:SetSize(wsize-6,30)
z.frameLeaderTop:SetPoint("TOPLEFT", z, "TOPLEFT", 3, -(buttonSize+1)-3)
--[[ z.frameLeaderTop.tex = z.frameLeaderTop:CreateTexture()
z.frameLeaderTop.tex:SetAllPoints(z.frameLeaderTop)
z.frameLeaderTop.tex:SetColorTexture(34/255, 78/255, 200/255 , 0.2) ]]
local buttonStopGray, buttonPlayGray

local playFunc, StopFunc, skipFunc
local buttonPlay = ns.Utils.crearBoton(z.frameLeaderTop,40,30,L["Play"],function ()
    --print ("play")
    playFunc()
    buttonPlayGray()
end)
local buttonStop = ns.Utils.crearBoton(z.frameLeaderTop,40,30,L["Stop"],function ()
    --print ("Stop")
    StopFunc()
    buttonStopGray()
end)
local pauseFunc, resumeFunc
local buttonSkip = ns.Utils.crearBoton(z.frameLeaderTop,40,30,L["Skip"],function ()
    --print ("skip")
    resumeFunc()
    skipFunc()
end)

buttonStopGray = function ()
    buttonStop:GrayedOut()
    buttonPlay:EnableAgain()
end

buttonPlayGray= function ()
    buttonPlay:GrayedOut()
    buttonStop:EnableAgain()
end
buttonStopGray()

z.frameCounter = CreateFrame("Frame", nil, z)
z.frameCounter:SetSize(wsize-6, 50)
z.frameCounter:SetPoint("TOPLEFT", z.frameLeaderTop, "BOTTOMLEFT", 0, 0 )
--[[ z.frameCounter.tex = z.frameCounter:CreateTexture()
z.frameCounter.tex:SetAllPoints(z.frameCounter)
z.frameCounter.tex:SetColorTexture(200/255, 1/255, 200/255 , 0.5) ]]
z.frameCounter.texMid = z.frameCounter:CreateTexture(nil, "BACKGROUND")
z.frameCounter.texMid:SetAllPoints(z.frameCounter)
z.frameCounter.texMid:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
z.frameCounter.texMid:SetTexCoord(382/512,504/512,366/512,416/512)

z.frameCounter.texMidTop = z.frameCounter:CreateTexture(nil, "OVERLAY")
z.frameCounter.texMidTop:SetAllPoints(z.frameCounter)
z.frameCounter.texMidTop:SetTexture("Interface\\AddOns\\AutoScrap\\images\\mailpanel")
z.frameCounter.texMidTop:SetTexCoord(382/512,504/512,426/512,476/512)
z.frameCounter.texMidTop:SetAlpha(0.5)

local textCounter = z.frameCounter:CreateFontString (nil, "ARTWORK", "GameFontNormal")
textCounter:SetFont("Fonts\\FRIZQT__.TTF", 18, "OUTLINE")
local colorBlancoStart, colorBlancoEnd = "|cFFFFFFFF", "|r"
textCounter:SetText (colorBlancoStart.."Kill Time"..colorBlancoEnd)
textCounter:SetJustifyH("CENTER")
textCounter:SetPoint("TOP", z.frameCounter, "TOP", 0,-2)

local textCounterNumbers = z.frameCounter:CreateFontString (nil, "ARTWORK", "GameFontNormal")
textCounterNumbers:SetFont("Fonts\\FRIZQT__.TTF", 26, "OUTLINE")
textCounterNumbers:SetText (colorBlancoStart.."00:00"..colorBlancoEnd)
textCounterNumbers:SetJustifyH("CENTER")
textCounterNumbers:SetPoint("TOP", textCounter, "BOTTOM", 0,-2)

z.frameLeaderBottom = CreateFrame("Frame", nil, z)
z.frameLeaderBottom:SetSize(wsize-6, 54)
z.frameLeaderBottom:SetPoint("TOPLEFT", z.frameCounter, "BOTTOMLEFT", 0, 0 )
--[[ z.frameLeaderBottom.tex = z.frameLeaderBottom:CreateTexture()
z.frameLeaderBottom.tex:SetAllPoints(z.frameLeaderBottom)
z.frameLeaderBottom.tex:SetColorTexture(50/255, 200/255, 100/255 , 0.5) ]]

local menosUno = ns.Utils.crearBoton(z.frameLeaderBottom,25,25,"-1",function ()
    if AutoScrap_CharSettings.lastTimerconfig.breakTimer > 1 then
        AutoScrap_CharSettings.lastTimerconfig.breakTimer = AutoScrap_CharSettings.lastTimerconfig.breakTimer -1
        ns.populateTimerADVOptionsInfo()
    end
end)
menosUno:SetPoint("TOPLEFT",z.frameLeaderBottom,"TOPLEFT", 0, -2)
local masUno = ns.Utils.crearBoton(z.frameLeaderBottom,25,25,"+1",function ()
    if AutoScrap_CharSettings.lastTimerconfig.breakTimer < 3600 then
        AutoScrap_CharSettings.lastTimerconfig.breakTimer = AutoScrap_CharSettings.lastTimerconfig.breakTimer +1
        ns.populateTimerADVOptionsInfo()
    end
end)
masUno:SetPoint("TOPRIGHT",z.frameLeaderBottom,"TOPRIGHT",-2,-2)

local textCounterBreak = z.frameLeaderBottom:CreateFontString (nil, "ARTWORK", "GameFontNormal")
textCounterBreak:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
textCounterBreak:SetText (colorBlancoStart.."Break Time:"..colorBlancoEnd)
textCounterBreak:SetJustifyH("CENTER")
textCounterBreak:SetPoint("TOP", z.frameLeaderBottom, "TOP", 0, -2)

local textCounterBreakPreview = z.frameLeaderBottom:CreateFontString (nil, "ARTWORK", "GameFontNormal")
textCounterBreakPreview:SetFont("Fonts\\FRIZQT__.TTF", 10, "")
textCounterBreakPreview:SetText (colorBlancoStart..ns.Utils.horaBonita(92)..colorBlancoEnd)
textCounterBreakPreview:SetJustifyH("CENTER")
textCounterBreakPreview:SetPoint("TOP", textCounterBreak, "BOTTOM", 0, -2)

function ns.populateTimerBreakTimeOnMain(time)
    textCounterBreakPreview:SetText (colorBlancoStart..ns.Utils.horaBonita(time)..colorBlancoEnd)
end

local pausedPressed = false
local pauseButton = ns.Utils.crearBoton(z.frameLeaderBottom,wsize-6-1,25,L["Pause After"],function ()
    pauseFunc()
end)
pauseButton:SetPoint("TOP", z.frameLeaderBottom, "TOP", -2, -2-25-2)

local resumebutton = ns.Utils.crearBoton(z.frameLeaderBottom,wsize-6,25,L["Resume"],function ()
    resumeFunc()
end)
resumebutton:SetPoint("CENTER", pauseButton, "CENTER")
resumebutton:Hide()

local function toDBMSeconds(seconds)
    return seconds/60
end

buttonPlay:SetPoint("TOPLEFT")
buttonStop:SetPoint("LEFT", buttonPlay, "RIGHT")
buttonSkip:SetPoint("LEFT", buttonStop, "RIGHT")

local screenNumberSize, screenNumberY = 200, 100
local colorNaranjaStart, colorNaranjaEnd = "|cFFC32A10", "|r"

local text9 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text9:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text9:SetText (colorNaranjaStart.."9"..colorNaranjaEnd)
text9:SetJustifyH("CENTER")
text9:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text9:Hide()

local text8 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text8:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text8:SetText (colorNaranjaStart.."8"..colorNaranjaEnd)
text8:SetJustifyH("CENTER")
text8:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text8:Hide()
local text7 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text7:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text7:SetText (colorNaranjaStart.."7"..colorNaranjaEnd)
text7:SetJustifyH("CENTER")
text7:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text7:Hide()
local text6 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text6:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text6:SetText (colorNaranjaStart.."6"..colorNaranjaEnd)
text6:SetJustifyH("CENTER")
text6:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text6:Hide()
local text5 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text5:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text5:SetText (colorNaranjaStart.."5"..colorNaranjaEnd)
text5:SetJustifyH("CENTER")
text5:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text5:Hide()
local text4 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text4:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text4:SetText (colorNaranjaStart.."4"..colorNaranjaEnd)
text4:SetJustifyH("CENTER")
text4:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text4:Hide()
local text3 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text3:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text3:SetText (colorNaranjaStart.."3"..colorNaranjaEnd)
text3:SetJustifyH("CENTER")
text3:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text3:Hide()

local text2 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text2:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text2:SetText (colorNaranjaStart.."2"..colorNaranjaEnd)
text2:SetJustifyH("CENTER")
text2:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text2:Hide()

local text1 = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
text1:SetFont("Fonts\\FRIZQT__.TTF", screenNumberSize, "OUTLINE")
text1:SetText (colorNaranjaStart.."1"..colorNaranjaEnd)
text1:SetJustifyH("CENTER")
text1:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY)
text1:Hide()

local textScreen = z.frameCounter:CreateFontString (nil, "OVERLAY", "GameFontNormal")
textScreen:SetFont("Fonts\\FRIZQT__.TTF", (screenNumberSize/4), "OUTLINE")
textScreen:SetText (colorNaranjaStart.."Kill Time"..colorNaranjaEnd)
textScreen:SetJustifyH("CENTER")
textScreen:SetPoint("CENTER", UIParent, "CENTER", 0,screenNumberY*2)
textScreen:Hide()

--buttonPlay:GrayedOut()
--buttonStop:GrayedOut()
--menosUno:EnableAgain()
local myTimerStartedTime, myTimerEndTime =  nil, nil
z.frameCounter.TimeSinceLastUpdate = 0
local updateMyTimerInterval= 0.1
local lastOnScreen = 50
local voice, l = "OverCraft", "en"
stopMyTimerNow = function ()
    textCounterNumbers:SetText (colorBlancoStart.."00:00"..colorBlancoEnd)
    myTimerRunning = false
    z.frameCounter:SetScript("OnUpdate", nil)
    text9:Hide()
    text8:Hide()
    text7:Hide()
    text6:Hide()
    text5:Hide()
    text4:Hide()
    text3:Hide()
    text2:Hide()
    text1:Hide()
    textScreen:Hide()
end
local function onUpdateMyTimer(self,elapsed)
    self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed
    if (self.TimeSinceLastUpdate < updateMyTimerInterval) then
        return
    end
    local t = myTimerEndTime - GetTime() +1 --+1 para sacar el número
    if t < 10 and t>=9 and lastOnScreen ~= 9 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text9:Show()
        C_Timer.After(800/1000, function ()
            text9:Hide()
        end)
        lastOnScreen = 9
    elseif t < 9 and t>=8 and lastOnScreen ~= 8 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text8:Show()
        C_Timer.After(800/1000, function ()
            text8:Hide()
        end)
        lastOnScreen = 8
    elseif t < 8 and t>=7 and lastOnScreen ~= 7 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text7:Show()
        C_Timer.After(800/1000, function ()
            text7:Hide()
        end)
        lastOnScreen = 7
    elseif t < 7 and t>=6 and lastOnScreen ~= 6 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text6:Show()
        C_Timer.After(800/1000, function ()
            text6:Hide()
        end)
        lastOnScreen = 6
    elseif t < 6 and t>=5 and lastOnScreen ~= 5 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text5:Show()
        C_Timer.After(800/1000, function ()
            text5:Hide()
        end)
        lastOnScreen = 5
    elseif t < 5 and t>=4 and lastOnScreen ~= 4 then
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        text4:Show()
        C_Timer.After(800/1000, function ()
            text4:Hide()
        end)
        lastOnScreen = 4
    elseif t < 4 and t>=3 and lastOnScreen ~= 3 then
        --PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        C_Timer.After(10/1000, function() 
            PlaySoundFile("interface\\addons\\autoscrap\\voice\\"..voice.."\\"..l.."\\3.mp3","Master")
        end)
        text3:Show()
        C_Timer.After(800/1000, function ()
            text3:Hide()
        end)
        lastOnScreen = 3
    elseif t < 3 and t>=2 and lastOnScreen ~= 2 then
        --PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        C_Timer.After(10/1000, function() 
            PlaySoundFile("interface\\addons\\autoscrap\\voice\\"..voice.."\\"..l.."\\2.mp3","Master")
        end)
        text2:Show()
        C_Timer.After(800/1000, function ()
            text2:Hide()
        end)
        lastOnScreen = 2
    elseif t< 2 and t>=1 and lastOnScreen ~= 1 then
        --PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell.mp3","Master")
        C_Timer.After(10/1000, function() 
            PlaySoundFile("interface\\addons\\autoscrap\\voice\\"..voice.."\\"..l.."\\1.mp3","Master")
        end)
        text1:Show()
        C_Timer.After(800/1000, function ()
            text1:Hide()
        end)
        lastOnScreen = 1
    end
    --t=t-1
    if t>0 then
        textCounterNumbers:SetText (colorBlancoStart.. string.format("%02d:%02d",math.floor(t/60),math.floor(t%60))..colorBlancoEnd)
    else
        stopMyTimerNow()
    end
end
local prevTimer
local function myTimer(time, type)
    if time == 0 then -- poner lo mismo al final del timer
        stopMyTimerNow()
    else
        ns.showMyTimerCountDown()
        local voiceWinner = math.random(1,5)
        if voiceWinner == 1 then
            voice = "OverCraft"
        elseif voiceWinner == 2 then
            voice = "Synnove"
        elseif voiceWinner == 3 then
            voice = "Dayrane"
        elseif voiceWinner == 4 then
            voice = "Yushan"
        else
            voice = "Yorozuya_Kros"
        end
        --[[ if math.random(1,2) == 1 then
            l = "en"
        else
            l= "es"
        end ]]
        l= "en"
        if LOCALE_esES then
            l= "es"
        end
        myTimerRunning = true
        lastOnScreen = 50
        text9:Hide()
        text8:Hide()
        text7:Hide()
        text6:Hide()
        text5:Hide()
        text4:Hide()
        text3:Hide()
        text2:Hide()
        text1:Hide()
        myTimerStartedTime = GetTime()
        myTimerEndTime = myTimerStartedTime+time-0.020 --20 milisegundos menos para que no coincida al empezar un 5 y en el frame siguiente 4, que no sea exacto.
        z.frameCounter:SetScript("OnUpdate", onUpdateMyTimer)
        if prevTimer then
            prevTimer:Cancel()
        end
        if type == "kill" then
            textCounter:SetText (colorBlancoStart.."Kill Time"..colorBlancoEnd)
            textScreen:SetText(colorNaranjaStart.."Kill Time"..colorNaranjaEnd)
            textScreen:Show()
            prevTimer=C_Timer.NewTimer(3000/1000, function () textScreen:Hide() prevTimer=nil end)
        elseif type == "break" then
            textCounter:SetText (colorBlancoStart.."Loot Time"..colorBlancoEnd)
            textScreen:SetText(colorNaranjaStart.."Loot Time"..colorNaranjaEnd)
            textScreen:Show()
            prevTimer=C_Timer.NewTimer(3000/1000, function () textScreen:Hide() prevTimer=nil end)
        elseif type == "sell" then
            textCounter:SetText (colorBlancoStart.."Loot+Sell"..colorBlancoEnd)
            textScreen:SetText(colorNaranjaStart.."Loot+Sell"..colorNaranjaEnd)
            textScreen:Show()
            prevTimer=C_Timer.NewTimer(3000/1000, function () textScreen:Hide() prevTimer=nil end)
        end
        --PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell_enter.mp3","Master")
        PlaySoundFile("interface\\addons\\autoscrap\\voice\\bell4.mp3","Master")
    end
end
ns.runMyTimer = myTimer


local startTime, changeState
local isBreak,isKilling,isSelling = false,false,false
--local timeKilling, timeBreak, timeSelling, killRounds = 90, 67 , 120, 5
local timeKilling, timeBreak, timeSelling, killRounds = 5, 3 , 3 , 2
local roundsBeforeSell = 0
local sellIfInventoryFull, sellInventoryPercentage  = true, 80
local runMoreThanOnce = true

function ns.updateTimerLocalSetup ()
    timeKilling, timeBreak, timeSelling, killRounds = AutoScrap_CharSettings.lastTimerconfig.killTimer, AutoScrap_CharSettings.lastTimerconfig.breakTimer, AutoScrap_CharSettings.lastTimerconfig.sellTimer, AutoScrap_CharSettings.lastTimerconfig.maxRounds
    sellIfInventoryFull, sellInventoryPercentage  = true, AutoScrap_CharSettings.lastTimerconfig.autoSell
    runMoreThanOnce = AutoScrap_CharSettings.lastTimerconfig.enableBreak
end
local function startKilling()
    isKilling, isBreak, isSelling = true, false, false
    --print ("killing") --SlashCmdList["DEADLYBOSSMODS"]("break 10")
    roundsBeforeSell = roundsBeforeSell + 1 
    local timeKillingBonito  = ns.Utils.horaBonita(timeKilling)
    if AutoScrap_CharSettings.lastTimerconfig.DBMTimer and SlashCmdList["DEADLYBOSSMODS"] and type (SlashCmdList["DEADLYBOSSMODS"]) == "function" then
        SlashCmdList["DEADLYBOSSMODS"]("break "..toDBMSeconds(timeKilling))
    end
    if ns.isPartyRunning() then
        SendChatMessage(L["Kill Time: "]..timeKillingBonito ,"PARTY")
        SendChatMessage(L["Kill Time: "]..timeKillingBonito ,"PARTY")
        SendChatMessage(L["Kill Time: "]..timeKillingBonito ,"PARTY")
    else
        --RaidNotice_AddMessage(RaidWarningFrame, L["Kill Time: "]..timeKillingBonito, ChatTypeInfo["SYSTEM"])
    end
    if ns.isPartyRunning() then
        ns.sendPartyTimerMSG(AutoScrap_CharSettings.lastTimerconfig.killTimer, "kill")
    end
    myTimer(AutoScrap_CharSettings.lastTimerconfig.killTimer, "kill")
    if AutoScrap_CharSettings.lastTimerconfig.countDownTimer then
        C_PartyInfo.DoCountdown(AutoScrap_CharSettings.lastTimerconfig.killTimer)
    end
end
local function startBreak()
    isKilling, isBreak, isSelling = false, true, false
    --print ("break") --SlashCmdList["DEADLYBOSSMODS"]("break 10")
    local timeBreakBonito  = ns.Utils.horaBonita(timeBreak)
    if AutoScrap_CharSettings.lastTimerconfig.DBMTimer and SlashCmdList["DEADLYBOSSMODS"] and type (SlashCmdList["DEADLYBOSSMODS"]) == "function" then
        SlashCmdList["DEADLYBOSSMODS"]("break "..toDBMSeconds(timeBreak))
    end
    if ns.isPartyRunning() then
        SendChatMessage(L["Loot Time: "]..timeBreakBonito ,"PARTY")
        SendChatMessage(L["Loot Time: "]..timeBreakBonito ,"PARTY")
        SendChatMessage(L["Loot Time: "]..timeBreakBonito ,"PARTY")
    else
        --RaidNotice_AddMessage(RaidWarningFrame, L["Loot Time: "]..timeBreakBonito, ChatTypeInfo["SYSTEM"])
    end
    if ns.isPartyRunning() then
        ns.sendPartyTimerMSG(AutoScrap_CharSettings.lastTimerconfig.breakTimer, "break")
    end
    myTimer(AutoScrap_CharSettings.lastTimerconfig.breakTimer, "break")
    if AutoScrap_CharSettings.lastTimerconfig.countDownTimer then
        C_PartyInfo.DoCountdown(AutoScrap_CharSettings.lastTimerconfig.breakTimer)
    end
end
local function startSell(name)
    isKilling, isBreak, isSelling = false, false, true
    local timeSellingBonito  = ns.Utils.horaBonita(timeSelling)
    if AutoScrap_CharSettings.lastTimerconfig.DBMTimer and SlashCmdList["DEADLYBOSSMODS"] and type (SlashCmdList["DEADLYBOSSMODS"]) == "function" then
        SlashCmdList["DEADLYBOSSMODS"]("break "..toDBMSeconds(timeSelling))
    end
    if ns.isPartyRunning() then
        if name and ns.isPartyRunning() then
            SendChatMessage(name ,"PARTY")
        end
        SendChatMessage(L["Extended Loot+Sell Time: "]..timeSellingBonito,"PARTY")
        SendChatMessage(L["Extended Loot+Sell Time: "]..timeSellingBonito,"PARTY")
        SendChatMessage(L["Extended Loot+Sell Time: "]..timeSellingBonito,"PARTY")
    else
        --RaidNotice_AddMessage(RaidWarningFrame, L["Loot and Everybody Sells After! Time to Loot+Sell: "]..timeSellingBonito, ChatTypeInfo["SYSTEM"])
    end
    --print ("sell") --SlashCmdList["DEADLYBOSSMODS"]("break 10")
    if ns.isPartyRunning() then
        ns.sendPartyTimerMSG(AutoScrap_CharSettings.lastTimerconfig.sellTimer, "sell")
    end
    myTimer(AutoScrap_CharSettings.lastTimerconfig.sellTimer, "sell")
    if AutoScrap_CharSettings.lastTimerconfig.countDownTimer then
        C_PartyInfo.DoCountdown(AutoScrap_CharSettings.lastTimerconfig.sellTimer)
    end
end
local function stopAny(oneTimeEnded)
    if isKilling or isBreak or isSelling then
        if AutoScrap_CharSettings.lastTimerconfig.DBMTimer and SlashCmdList["DEADLYBOSSMODS"] and type (SlashCmdList["DEADLYBOSSMODS"]) == "function" then
            SlashCmdList["DEADLYBOSSMODS"]("break 0")
        end
        --print ("stopped signal dbm")
        if not oneTimeEnded then
            if ns.isPartyRunning() then
                SendChatMessage(L["AutoScrap Timer OFF"] ,"PARTY")
                SendChatMessage(L["AutoScrap Timer OFF"] ,"PARTY")
                SendChatMessage(L["AutoScrap Timer OFF"] ,"PARTY")
                if AutoScrap_CharSettings.lastTimerconfig.countDownTimer then
                    C_PartyInfo.DoCountdown(0)
                end
            else
                RaidNotice_AddMessage(RaidWarningFrame, L["Timer end"], ChatTypeInfo["SYSTEM"])
            end
        else
            if ns.isPartyRunning() then
                SendChatMessage(L["AutoScrap Timer OFF, LOOT!"] ,"PARTY")
                SendChatMessage(L["AutoScrap Timer OFF, LOOT!"] ,"PARTY")
                SendChatMessage(L["AutoScrap Timer OFF, LOOT!"] ,"PARTY")
                if AutoScrap_CharSettings.lastTimerconfig.countDownTimer then
                    C_PartyInfo.DoCountdown(0)
                end
            else
                RaidNotice_AddMessage(RaidWarningFrame, L["AutoScrap Timer OF, LOOT!"] , ChatTypeInfo["SYSTEM"])
            end
        end
    end
    isKilling, isBreak, isSelling = false, false, false
    if ns.isPartyRunning() then
        ns.sendPartyTimerMSG(0, "kill")
    end
    myTimer(0, "kill")
end


playFunc = function ()
    startTime = math.floor(GetTime())
    changeState = startTime + timeKilling
    roundsBeforeSell = 0
    resumeFunc()
    startKilling()
    timerRunning = true
    if not ns.getTimerRunning() then
        ns.startCombatFrame()
    end
end
StopFunc = function (oneTimeEnded)
    timerRunning = false
    stopAny(oneTimeEnded)
    buttonStopGray()
    resumeFunc()
end
skipFunc = function ()
        changeState = math.floor(GetTime()) - 1
end
function ns.isPartyTimerKillBreakRestRunning()
    return timerRunning
end

function ns.partyTimerKillBreakRestTick()
    if timerRunning then
        if pausedPressed then
            return
        end
        local currentTime  = math.floor(GetTime())
        --print ("->",changeState,currentTime, changeState - currentTime)
        if isKilling then
            if  currentTime - changeState >= 0 then
                if not runMoreThanOnce then
                    StopFunc(true)
                    return
                end
                local inventoryFull = false
                local maxBags, namePlayer, stringMessage
                if sellIfInventoryFull then
                    --print ("my bag",ns.getMyBagsPercentageFromCombatFrame())
                    if ns.isPartyRunning() then
                        --print ("party max bag",ns.getPartyInventoryMax())
                        maxBags, namePlayer = ns.getPartyInventoryMax()
                        if (maxBags >= sellInventoryPercentage) then
                            inventoryFull = true
                            if namePlayer ~= "" then
                                stringMessage = ""..namePlayer..L[" is getting full bags."]
                                --[[ if ns.isPartyRunning() then
                                    SendChatMessage(""..namePlayer..L[" is getting full bags."] ,"PARTY")
                                end ]]
                            end
                        end
                    end
                    if not inventoryFull and  ns.getMyBagsPercentageFromCombatFrame() >= sellInventoryPercentage then
                        inventoryFull = true
                        local player, serverShortName = UnitFullName("player")
                        stringMessage = ""..player.."-"..serverShortName..L[" is getting full bags."]
                    end
                end
                if sellIfInventoryFull and inventoryFull then
                    changeState = currentTime + timeSelling
                    roundsBeforeSell = 0
                    stringMessage = stringMessage or " SomeOne is full Bags"
                    startSell(stringMessage)
                else
                    if roundsBeforeSell >= killRounds then
                        changeState = currentTime + timeSelling
                        roundsBeforeSell = 0
                        startSell()
                    else
                        changeState = currentTime + timeBreak
                        startBreak()
                    end
                end
            end
        elseif isBreak then
            if  currentTime - changeState  >= 0 then
                if not runMoreThanOnce then
                    StopFunc(true)
                    return
                end
                changeState = currentTime + timeKilling
                startKilling()
            end
        elseif isSelling then
            if not runMoreThanOnce then
                StopFunc(true)
                return
            end
            if  currentTime - changeState  >= 0 then
                changeState = currentTime + timeKilling
                startKilling()
            end
        end
    end
end

function ns.forceStopTimerKillBreakSell()
    if timerRunning then
        StopFunc() 
    end
end

local function ocultarPartyLeader()
    z.frameLeaderTop:Hide()
    z.frameLeaderBottom:Hide()
    z.frameCounter:SetPoint("TOPLEFT", z, "TOPLEFT", 3, -(buttonSize+1)-1)
    z:SetSize(wsize,hsize-54-30-3)
    outterFrame:SetSize(wsize,hsize-54-30-3)
    --optionsButton:Hide()
end

local function mostrarPartyLeader()
    z.frameLeaderTop:Show()
    z.frameLeaderBottom:Show()
    z.frameCounter:SetPoint("TOPLEFT", z.frameLeaderTop, "BOTTOMLEFT", 0, 0 )
    z:SetSize(wsize,hsize)
    outterFrame:SetSize(wsize,hsize)
    --optionsButton:Show()
end

local partyLeader = true --en true porque party down no se tira de inicio, entonces true que corresponde siempre a partydown para ponerte timers a ti mismo sin parti.
function ns.setPartyLeaderInTimerKillBreakSell(flag)
    --print ("party leader", flag)
    if timerRunning and not flag then
        StopFunc()
    end
    if myTimerRunning and flag and not partyLeader then
        if ns.isPartyRunning() then
            ns.sendPartyTimerMSG(0, "kill")
        end
        myTimer(0, "kill")
    end
    if partyLeader and not flag then
        ocultarPartyLeader()
    elseif not partyLeader and flag then
        mostrarPartyLeader()
    end
    partyLeader = flag
end

function ns.partyIsUpOrDown( flag ) --false down.
    --print ("party up or down", flag)
    if timerRunning then
        StopFunc()
    end
end

pauseFunc = function ()
    if timerRunning then
        pausedPressed = true
        pauseButton:Hide()
        resumebutton:Show()
    end
end

resumeFunc = function ()
    pausedPressed = false
    pauseButton:Show()
    resumebutton:Hide()
end

function ns.setTimerFramePosition()
    outterFrame:SetPoint(AutoScrap_CharSettings.TimerFrame.startPoint, UIParent, AutoScrap_CharSettings.TimerFrame.relativePoint, AutoScrap_CharSettings.TimerFrame.xOfs, AutoScrap_CharSettings.TimerFrame.yOfs)
end