Quantcast
SOLVENT_DEBUG = false

function TotalKeystonesFromBags()
  keystones = {
    { -- Dwarf
      ["count"] = 0,
      ["id"] = 52843,
    },
    { -- Draenei
      ["count"] = 0,
      ["id"] = 64394,
    },
    { -- Fossil
      ["count"] = 0,
      ["id"] = 0,
    },
    { -- Night Elf
      ["count"] = 0,
      ["id"] = 63127,
    },
    { -- Nerubian
      ["count"] = 0,
      ["id"] = 64396,
    },
    { -- Orc
      ["count"] = 0,
      ["id"] = 64392,
    },
    { -- Tol'vir
      ["count"] = 0,
      ["id"] = 64397,
    },
    { -- Troll
      ["count"] = 0,
      ["id"] = 63128,
    },
    { -- Vrykul
      ["count"] = 0,
      ["id"] = 64395,
    },
  }

  for bag=0,4 do
    numSlots = GetContainerNumSlots(bag)
    if SOLVENT_DEBUG then
      print(GetBagName(bag) .. ': ' .. numSlots .. ' slot bag')
    end

    for slot=1,numSlots do
      itemId = GetContainerItemID(bag,slot)
      if itemId then
        texture, itemCount, locked, quality, readable, lootable, itemLink = GetContainerItemInfo(bag, slot);
        for key,hash in pairs(keystones) do
          --print(id .. " :: " .. itemId)

          if hash["id"] == itemId then
            itemName, otherJunk = GetItemInfo(hash["id"])
            if SOLVENT_DEBUG then
              print(" >> Found " .. itemName .. ": " .. hash["id"])
            end

            keystones[key]["count"] = itemCount
          end
        end
      end
    end
  end

  return keystones
end

function GetCurrentArtifactInfoByRace(raceId)
  SetSelectedArtifact(raceId)
  name, otherJunk = GetSelectedArtifactInfo()
  for i=1,GetNumArtifactsByRace(raceId) do
    iterationName, theRest = GetArtifactInfoByRace(raceId, i)
    print(name .. ' - ' .. iterationName)
    if name == iterationName then
      return iterationName, theRest
    end
  end

  return nil
end

statusBars = {}
function SolventGetCurrentArtifacts()
  keystones = TotalKeystonesFromBags()

  prof1, prof2, archaeology, fishing, cooking, firstAid = GetProfessions();
  name, icon, skillLevel, maxSkillLevel, numAbilities, spelloffset, skillLine, skillModifier = GetProfessionInfo(archaeology)
  if SOLVENT_DEBUG then
    print(name .. ": " .. skillLevel .. "/" .. maxSkillLevel)
  end

  local raceCount = GetNumArchaeologyRaces();
  local artifacts = {}
  local lastRaceName
  for i=1,raceCount do
    local raceName, raceTexture, raceItemID, numFragmentsCollected, numFragmentsRequired = GetArchaeologyRaceInfo(i);
    SetSelectedArtifact(i)
    local artifactName, artifactDescription, artifactRarity, artifactIcon, hoverDescription, keystoneCount, bgTexture = GetSelectedArtifactInfo()
    --local artifactName, artifactDescription, artifactRarity, artifactIcon, hoverDescription, keystoneCount, bgTexture, firstCompletionTime, completionCount = GetArtifactInfoByRace(i, 0);

    --print(i .. ' - ' .. raceName)

    if GetNumArtifactsByRace(i) > 0 then
      artifacts[raceName] = {
        name = artifactName,
        collected = numFragmentsCollected,
        required = numFragmentsRequired,
        slots = keystoneCount,
        items = keystones[i]["count"],
      }

      if SOLVENT_DEBUG then
        DEFAULT_CHAT_FRAME:AddMessage(raceName .. ": " .. artifactName .. " - " .. numFragmentsCollected .. "/" .. numFragmentsRequired .. " - " .. keystones[i]["count"] .. " keystones in bags, " .. keystoneCount .. " keystone slots available")
      end

      if statusBars[i] == nil then
        if SOLVENT_DEBUG then
          DEFAULT_CHAT_FRAME:AddMessage("No Frame for " .. raceName .. ", creating...")
        end

        statusBars[i] = CreateFrame("Frame", "SolventSolveFrameRow" .. raceName, UIParent, "SolventRowTemplate")
        statusBars[i]:SetWidth(200)
        statusBars[i]:SetHeight(12)

        statusBar = _G["SolventSolveFrameRow" .. raceName .. "StatusBar"]

        statusBar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
        statusBar:GetStatusBarTexture():SetHorizTile(false)
        --statusBar:SetWidth(200)
        --statusBar:SetHeight(12)
        statusBar:SetStatusBarColor(0,1,0)
        if i == 1 then
          statusBars[i]:SetPoint("TOP", SolventSolveFrameAddonTitleText,"BOTTOM", 0, -15)
        else
          statusBars[i]:SetPoint("TOP", _G["SolventSolveFrameRow" .. lastRaceName], "BOTTOM", 0, -15)
        end
      else
        statusBar = _G["SolventSolveFrameRow" .. raceName .. "StatusBar"]
      end

      statusBar:SetMinMaxValues(0, numFragmentsRequired)
      statusBar:SetValue(numFragmentsCollected)
      statusBar.fragments:SetText(numFragmentsCollected .. ' / ' .. numFragmentsRequired)
      if strlen(artifactName) > 25 then
        statusBar.artifact:SetText(strsub(artifactName, 1, 25) .. '...')
      else
        statusBar.artifact:SetText(artifactName)
      end
      _G["SolventSolveFrameRow" .. raceName .. "Icon"].texture:SetTexture(raceTexture)
      _G["SolventSolveFrameRow" .. raceName .. "Keystones"].keystones:SetText(keystones[i]["count"] .. ' / ' .. keystoneCount)

      lastRaceName = raceName
    end
  end

   return artifacts
end

function SolventStartup()
  DEFAULT_CHAT_FRAME:AddMessage("Solvent Archaeology Addon Loaded!")
  SolventGetCurrentArtifacts()
end

dframe = CreateFrame("Frame", nil, UIParent)
dframe:RegisterEvent("CURRENCY_DISPLAY_UPDATE");
--dframe:RegisterEvent("ARTIFACT_UPDATE");       ONLY AFTER SOLVING!
--dframe:RegisterEvent("BAG_UPDATE");   ONLY AFTER SURVEYING!
dframe:SetScript("OnEvent", SolventGetCurrentArtifacts);