--DISPLAY
function()
    if aura_env.statee then
        return aura_env.statee.text
    end
end

--QUEST_ACCEPTED QUEST_TURNED_IN CHAT_MSG_ADDON
function(e, ...)
    if e == "CHAT_MSG_ADDON" then
        local identifier, msg, channel, sender = ...
        sender = sender:match("(.+)-")
        if identifier == "DQUEST" then
            local n, ID, pclass = msg:match("A(%d+)[-](%d+)[-](%d+)")
            n = tonumber(n)
            ID = tonumber(ID)
            pclass = tonumber(pclass)
            aura_env.guild[sender] = {
                ["n"] = n,
                ["ID"] = ID,
                ["pclass"] = pclass,
            }
            WeakAuras.ScanEvents("DAVE_QUEST_DING")
        end
    elseif e == "QUEST_ACCEPTED" then
        local ID = select(2, ...)
        if aura_env.questDatabase[ID] then
            aura_env.currentQuest = aura_env.questDatabase[ID]
            SendAddonMessage("DQUEST", "A" .. aura_env.questDatabase[ID] .. "-" .. ID .. "-" .. select(3, UnitClass("player")), "GUILD")
        end
        --elseif e == "QUEST_TURNED_IN" then
        --    local ID = select(1, ...)
        --    SendAddonMessage("DQUEST", "C" .. ID, "PARTY")
    end
end

--TSU
--DAVE_QUEST_DING
function(allstates, e)
    --local output = ""
    for k, v in pairs(allstates) do
        v.show = false
        v.changed = true
    end
    aura_env.quests = {}
    for k, v in pairs(aura_env.guild) do
        if not aura_env.quests[v.n] then
            aura_env.quests[v.n] = {}
        end
        if not aura_env.classList[k] then
            aura_env.classList[k] = v.pclass
        end
        aura_env.quests[v.n][#aura_env.quests[v.n] + 1] = k
        --print("aura_env.quests", aura_env.currentQuest)
        --DevTools_Dump(aura_env.quests)
        --print("aura_env.guild")
        --DevTools_Dump(aura_env.guild)
        --print("aura_env.quests[aura_env.currentQuest]")
        --DevTools_Dump(aura_env.quests[aura_env.currentQuest])
    end
    --output = output .. "Your quest\n"
    if aura_env.currentQuest <= 1 then
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Your quest [" .. aura_env.currentQuest .. "/89]    (" .. aura_env.questNames[aura_env.currentQuest] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest] and type(aura_env.quests[aura_env.currentQuest]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
        --output = output .. "Next quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Next quest    (" .. aura_env.questNames[aura_env.currentQuest + 1] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest + 1] and type(aura_env.quests[aura_env.currentQuest + 1]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest + 1]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
    elseif aura_env.currentQuest > 1 and aura_env.currentQuest < 89 then
        --output = output .. "Previous Quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Previous Quest    (" .. aura_env.questNames[aura_env.currentQuest - 1] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest - 1] and type(aura_env.quests[aura_env.currentQuest - 1]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest - 1]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
        --output = output .. "Your quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Your quest [" .. aura_env.currentQuest .. "/89]    (" .. aura_env.questNames[aura_env.currentQuest] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest] and type(aura_env.quests[aura_env.currentQuest]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
        --output = output .. "Next quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Next quest    (" .. aura_env.questNames[aura_env.currentQuest + 1] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest + 1] and type(aura_env.quests[aura_env.currentQuest + 1]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest + 1]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
    elseif aura_env.currentQuest == 89 then
        --output = output .. "Previous Quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Previous Quest    (" .. aura_env.questNames[aura_env.currentQuest - 1] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest - 1] and type(aura_env.quests[aura_env.currentQuest - 1]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest - 1]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
        --output = output .. "Your quest\n"
        allstates[#allstates + 1] = {
            changed = true,
            show = true,
            text = "Your quest [" .. aura_env.currentQuest .. "/89]    (" .. aura_env.questNames[aura_env.currentQuest] .. ")",
        }
        if aura_env.quests[aura_env.currentQuest] and type(aura_env.quests[aura_env.currentQuest]) == "table" then
            for k, v in pairs(aura_env.quests[aura_env.currentQuest]) do
                --output = output .. "    " .. aura_env.classColor(aura_env.classList[v]) .. v .. "|r\n"
                allstates[#allstates + 1] = {
                    changed = true,
                    show = true,
                    text = "      " .. v,
                    pclass = aura_env.classList[v],
                }
            end
        end
    end
    --DevTools_Dump(allstates)
    return true
end

--ANIMATION / COLOR
function()
    if aura_env.statee.pclass then
        return aura_env.classColor(aura_env.statee.pclass)
    end
end

--INIT
local plswork = RegisterAddonMessagePrefix("DQUEST")
if plswork == false then
    print("no work :( 001")
end
aura_env.currentQuest = 1
aura_env.guild = {}
aura_env.quests = {}
aura_env.classList = {}
aura_env.questDatabase = {
    [47835] = 1,
    [47867] = 2,
    [47223] = 3,
    [47224] = 4,
    [48440] = 5,
    [46938] = 6,
    [47589] = 7,
    [46297] = 8,
    [48483] = 9,
    [47627] = 10,
    [47641] = 11,
    [46732] = 12,
    [46816] = 13,
    [46839] = 14,
    [46840] = 15,
    [46841] = 16,
    [46842] = 17,
    [46843] = 18,
    [48500] = 19,
    [47431] = 20,
    [46213] = 21,
    [40238] = 22,
    [47541] = 23,
    [47508] = 24,
    [47771] = 25,
    [47526] = 26,
    [47754] = 27,
    [47652] = 28,
    [47653] = 29,
    [47743] = 30,
    [49143] = 31,
    [47287] = 32,
    [48559] = 33,
    [48199] = 34,
    [48200] = 35,
    [48201] = 36,
    [48202] = 37,
    [47473] = 38,
    [47889] = 39,
    [47890] = 40,
    [47891] = 41,
    [47892] = 42,
    [47986] = 43,
    [47987] = 44,
    [47988] = 45,
    [47991] = 46,
    [47990] = 47,
    [47989] = 48,
    [47992] = 49,
    [47993] = 50,
    [47994] = 51,
    [48081] = 52,
    [46815] = 53,
    [46818] = 54,
    [46834] = 55,
    [47066] = 56,
    [46941] = 57,
    [47686] = 58,
    [47882] = 59,
    [47688] = 60,
    [47883] = 61,
    [47689] = 62,
    [47685] = 63,
    [47687] = 64,
    [47690] = 65,
    [48107] = 66,
    [48461] = 67,
    [48344] = 68,
    [47691] = 69,
    [47854] = 70,
    [47995] = 71,
    [47853] = 72,
    [48345] = 73,
    [47855] = 74,
    [47856] = 75,
    [47416] = 76,
    [47238] = 77,
    [40761] = 78,
    [47101] = 79,
    [47180] = 80,
    [47100] = 81,
    [47183] = 82,
    [47184] = 83,
    [47203] = 84,
    [47217] = 85,
    [47218] = 86,
    [47219] = 87,
    [47220] = 88,
    [48560] = 89,
}
aura_env.questNames = {
    [1] = "The Hand of Fate",
    [2] = "Two If By Sea",
    [3] = "Light's Exodus",
    [4] = "The Vindicaar",
    [5] = "Into the Night",
    [6] = "Alone in the Abyss",
    [7] = "Righteous Fury",
    [8] = "Overwhelming Power",
    [9] = "A Stranger's Plea",
    [10] = "Vengeance",
    [11] = "Sign of Resistance",
    [12] = "The Prophet's Gambit",
    [13] = "Rendezvous",
    [14] = "From Darkness",
    [15] = "Prisoners No More",
    [16] = "Threat Reduction",
    [17] = "A Strike at the Heart",
    [18] = "Return to the Vindicaar",
    [19] = "A Moment of Respite",
    [20] = "Gathering Light",
    [21] = "Crystals Not Included",
    [22] = "A Grim Equation",
    [23] = "The Best Prevention",
    [24] = "Fire at Will",
    [25] = "Locating the Longshot",
    [26] = "Bringing the Big Guns",
    [27] = "Lightly Roasted",
    [28] = "The Light Mother",
    [29] = "Light's Return",
    [30] = "The Child of Light and Shadow",
    [31] = "Essence of the Light Mother",
    [32] = "The Vindicaar Matrix Core",
    [33] = "An Offering of Light",
    [34] = "The Burning Heart",
    [35] = "Securing a Foothold",
    [36] = "Reinforce Light's Purchase",
    [37] = "Reinforce the Veiled Den",
    [38] = "Sizing Up The Opposition",
    [39] = "The Speaker Calls",
    [40] = "Visions of Torment",
    [41] = "Dire News",
    [42] = "Storming the Citadel",
    [43] = "Scars of the Past",
    [44] = "Preventive Measures",
    [45] = "Chaos Theory",
    [46] = "Dark Machinations",
    [47] = "A Touch of Fel",
    [48] = "Heralds of Apocalypse",
    [49] = "Dawn of Justice",
    [50] = "Lord of the Spire",
    [51] = "Forming a Bond",
    [52] = "A Floating Ruin",
    [53] = "Mac'Aree, Jewel of Argus",
    [54] = "Defenseless and Afraid",
    [55] = "Khazaduum, First of His Name",
    [56] = "Consecrating Ground",
    [57] = "The Path Forward",
    [58] = "Not-So-Humble Beginnings",
    [59] = "Conservation of Magic",
    [60] = "Invasive Species",
    [61] = "The Longest Vigil",
    [62] = "Gatekeeper's Challenge: Tenacity",
    [63] = "Gatekeeper's Challenge: Cunning",
    [64] = "Gatekeeper's Challenge: Mastery",
    [65] = "The Defiler's Legacy",
    [66] = "The Sigil of Awakening",
    [67] = "Where They Least Expect It",
    [68] = "We Have a Problem",
    [69] = "A Non-Prophet Organization ",
    [70] = "Wrath of the Hight Exarch",
    [71] = "Overt Ops",
    [72] = "Flanking Maneuvers",
    [73] = "Talgath's Forces",
    [74] = "What Might Have Been",
    [75] = "Across the Universe",
    [76] = "Shadow of the Triumvirate",
    [77] = "The Seat of the Triumvirate",
    [78] = "Whispers from Oronaar",
    [79] = "Arkhaan's Prayers",
    [80] = "The Pulsing Madness",
    [81] = "Arkhaan's Pain",
    [82] = "Arkhaan's Plan",
    [83] = "Arkhaan's Peril",
    [84] = "Throwing Shade",
    [85] = "Sources of Darkness",
    [86] = "The Shadowguard Incursion",
    [87] = "A Vessel Made Ready",
    [88] = "A Beacon in the Dark",
    [89] = "An Offering of Shadow",
}
aura_env.classColor = function(class)
    if class == 1 then return 0.78, 0.61, 0.43 elseif
    class == 2 then return 0.96, 0.55, 0.73 elseif
    class == 3 then return 0.67, 0.83, 0.45 elseif
    class == 4 then return 1, 0.96, 0.41 elseif
    class == 5 then return 1, 1, 1 elseif
    class == 6 then return 0.77, 0.12, 0.23 elseif
    class == 7 then return 0, 0.44, 0.87 elseif
    class == 8 then return 0.25, 0.78, 0.92 elseif
    class == 9 then return 0.53, 0.53, 0.93 elseif
    class == 10 then return 0, 1, 0.59 elseif
    class == 11 then return 1, 0.49, 0.04 elseif
    class == 12 then return 0.64, 0.19, 0.79 else
    return 1, 1, 1 end
end
