local tablet = AceLibrary("Tablet-2.0")
local dewdrop = AceLibrary("Dewdrop-2.0")
local L = AceLibrary("AceLocale-2.2"):new("MoteFu")

MoteFu = AceLibrary("AceAddon-2.0"):new("FuBarPlugin-2.0", "AceDB-2.0", "AceEvent-2.0", "AceHook-2.1", "AceConsole-2.0")
MoteFu.version = "3.3"
MoteFu.date = "07-30-2007"

--### RGB color values for the text used in the main display
local Earth_R  = .6; local Earth_G  = .6; local Earth_B  = .6;
local Air_R    = .7; local Air_G    =  1; local Air_B    = .8;
local Fire_R   =  1; local Fire_G   = .3; local Fire_B   =  0;
local Water_R  = .5; local Water_G  = .8; local Water_B  =  1;
local Mana_R   = .3; local Mana_G   = .6; local Mana_B   =  1;
local Life_R   =  0; local Life_G   = .8; local Life_B   =  0;
local Shadow_R = .6; local Shadow_G =  0; local Shadow_B =  1;
local Might_R  =  0; local Might_G  = .7; local Might_B  =  1;
local Nether_R =  1; local Nether_G = .7; local Nether_B =  0;

MoteFu:RegisterDB("MoteFuDB")
MoteFu:RegisterDefaults('profile', {
   showEarth   = true,
   showAir     = true,
   showFire    = true,
   showWater   = true,
   showMana    = true,
   showLife    = false,
   showShadow  = false,
   showMight   = false,
   showNether  = false,
   includeBank = true
})

MoteFu.hasIcon = false
MoteFu.clickableTooltip = true
MoteFu.defaultPosition = "CENTER"

function MoteFu:OnEnable()
   self:CharacterInit()
   self:RegisterEvent("BAG_UPDATE")
   self:RegisterEvent("BANKFRAME_OPENED")
   self:RegisterEvent("BANKFRAME_CLOSED")
   self:OnTextUpdate()
end

function MoteFu:OnDisable()
   self:UnregisterEvent("BAG_UPDATE")
   self:UnregisterEvent("BANKFRAME_OPENED")
   self:UnregisterEvent("BANKFRAME_CLOSED")
end

function MoteFu:CharacterInit()
   --### If the current realm is not found in the DB, add it
   if (not self.db.account[self.REALM or GetRealmName()]) then
      self.db.account[self.REALM or GetRealmName()] = {}
   end

   --### If the current character is not found in the DB, add it
   if (not self.db.account[self.REALM or GetRealmName()][self.NAME or UnitName("player")]) then
      self.db.account[self.REALM or GetRealmName()][self.NAME or UnitName("player")] = {}
      self.myData = self.db.account[self.REALM or GetRealmName()][self.NAME or UnitName("player")]
      if UnitLevel("player") ~= 0 then
         local bank = self.myData
		 bank.mEarth  = 0
		 bank.mAir    = 0
		 bank.mFire   = 0
		 bank.mWater  = 0
		 bank.mMana   = 0
         bank.mLife   = 0
		 bank.mShadow = 0

		 bank.pEarth  = 0
		 bank.pAir    = 0
		 bank.pFire   = 0
		 bank.pWater  = 0
		 bank.pMana   = 0
         bank.pLife   = 0
		 bank.pShadow = 0
         bank.pMight  = 0
         bank.pNether = 0
      end
   end
end

function MoteFu:OnTextUpdate()
   self:SetText("MoteFu")
end

local bankOpen = false
function MoteFu:BAG_UPDATE()
   if bankOpen then
	  self:CountBankContents()
   else
	  self:UpdateTooltip()
   end
end

function MoteFu:BANKFRAME_OPENED()
   bankOpen = true
   self:CountBankContents()
end

function MoteFu:BANKFRAME_CLOSED()
   bankOpen = false
end

function MoteFu:CountBankContents()
   self.myData = self.db.account[self.REALM or GetRealmName()][self.NAME or UnitName("player")]
   local bank = self.myData

   if UnitLevel("player") ~= 0 then
      bank.mEarth  = 0
      bank.mAir    = 0
      bank.mFire   = 0
      bank.mWater  = 0
      bank.mMana   = 0
      bank.mLife   = 0
      bank.mShadow = 0

      bank.pEarth  = 0
      bank.pAir    = 0
      bank.pFire   = 0
      bank.pWater  = 0
      bank.pMana   = 0
      bank.pLife   = 0
      bank.pShadow = 0
      bank.pMight  = 0
      bank.pNether = 0
   end

   --### Scan the main bank window
   numBankSlots = GetContainerNumSlots(BANK_CONTAINER);
   for BankSlotNum = 1, numBankSlots do
      link = GetContainerItemLink(BANK_CONTAINER, BankSlotNum);
      if( link ) then -- must be an item that bank slot
         _, qty = GetContainerItemInfo(BANK_CONTAINER, BankSlotNum);
		 if     string.find(link, ":22573:") then bank.mEarth  = bank.mEarth  + qty
		 elseif string.find(link, ":22572:") then bank.mAir    = bank.mAir    + qty
		 elseif string.find(link, ":22574:") then bank.mFire   = bank.mFire   + qty
		 elseif string.find(link, ":22578:") then bank.mWater  = bank.mWater  + qty
		 elseif string.find(link, ":22576:") then bank.mMana   = bank.mMana   + qty
         elseif string.find(link, ":22575:") then bank.mLife   = bank.mLife   + qty
		 elseif string.find(link, ":22577:") then bank.mShadow = bank.mShadow + qty

		 elseif string.find(link, ":22452:") then bank.pEarth  = bank.pEarth  + qty
		 elseif string.find(link, ":22451:") then bank.pAir    = bank.pAir    + qty
		 elseif string.find(link, ":21884:") then bank.pFire   = bank.pFire   + qty
		 elseif string.find(link, ":21885:") then bank.pWater  = bank.pWater  + qty
		 elseif string.find(link, ":22457:") then bank.pMana   = bank.pMana   + qty
         elseif string.find(link, ":21886:") then bank.pLife   = bank.pLife   + qty
		 elseif string.find(link, ":22456:") then bank.pShadow = bank.pShadow + qty
         elseif string.find(link, ":23571:") then bank.pMight  = bank.pMight  + qty
         elseif string.find(link, ":23572:") then bank.pNether = bank.pNether + qty
		 end
      end
   end

   --### Now scan the bank bags (5-11), if any
   --### Note: bags 0-4 are the ones a player carries with them
   for BagNum = 5, 11 do
	  numBagSlots = GetContainerNumSlots(BagNum)
   for BagSlotNum = 1, numBagSlots do
      link = GetContainerItemLink(BagNum, BagSlotNum);
      if( link ) then -- must be an item that bank slot
         _, qty = GetContainerItemInfo(BagNum, BagSlotNum);

         if     string.find(link, ":22573:") then bank.mEarth  = bank.mEarth  + qty
		 elseif string.find(link, ":22572:") then bank.mAir    = bank.mAir    + qty
		 elseif string.find(link, ":22574:") then bank.mFire   = bank.mFire   + qty
		 elseif string.find(link, ":22578:") then bank.mWater  = bank.mWater  + qty
		 elseif string.find(link, ":22576:") then bank.mMana   = bank.mMana   + qty
         elseif string.find(link, ":22575:") then bank.mLife   = bank.mLife   + qty
		 elseif string.find(link, ":22577:") then bank.mShadow = bank.mShadow + qty

		 elseif string.find(link, ":22452:") then bank.pEarth  = bank.pEarth  + qty
		 elseif string.find(link, ":22451:") then bank.pAir    = bank.pAir    + qty
		 elseif string.find(link, ":21884:") then bank.pFire   = bank.pFire   + qty
		 elseif string.find(link, ":21885:") then bank.pWater  = bank.pWater  + qty
		 elseif string.find(link, ":22457:") then bank.pMana   = bank.pMana   + qty
         elseif string.find(link, ":21886:") then bank.pLife   = bank.pLife   + qty
		 elseif string.find(link, ":22456:") then bank.pShadow = bank.pShadow + qty
         elseif string.find(link, ":23571:") then bank.pMight  = bank.pMight  + qty
         elseif string.find(link, ":23572:") then bank.pNether = bank.pNether + qty
		 end
      end
   end

   end

   self:UpdateTooltip()
end

function MoteFu:OnTooltipUpdate()

   local carried = {
      mEarth  = GetItemCount( 22573 ),
      mAir    = GetItemCount( 22572 ),
      mFire   = GetItemCount( 22574 ),
      mWater  = GetItemCount( 22578 ),
      mMana   = GetItemCount( 22576 ),
      mLife   = GetItemCount( 22575 ),
      mShadow = GetItemCount( 22577 ),

      pEarth  = GetItemCount( 22452 ),
      pAir    = GetItemCount( 22451 ),
      pFire   = GetItemCount( 21884 ),
      pWater  = GetItemCount( 21885 ),
      pMana   = GetItemCount( 22457 ),
      pLife   = GetItemCount( 21886 ),
      pShadow = GetItemCount( 22456 ),
      pMight  = GetItemCount( 23571 ),
      pNether = GetItemCount( 23572 ),      
   }

   --### Define the general table attributes, set default colors to yellow
   --### However, the actual colors used are defined at the top of this file
   local display = tablet:AddCategory(
      'columns', 3,
      'child_textR', 1,
      'child_textG', 1,
      'child_textB', 0,
      'child_text2R', 1,
      'child_text2G', 1,
      'child_text2B', 0,
      'child_text3R', 1,
      'child_text3G', 1,
      'child_text3B', 0,
      'child_justify', "LEFT",
      'child_justify2', "RIGHT",
      'child_justify3', "RIGHT"
   )

   display:AddLine(
      'text', L["Element"],
	  'textR', 1,
	  'textG', 1,
	  'textB', 1,
      'text2', L["Motes"],
	  'text2R', 1,
	  'text2G', 1,
	  'text2B', 1,
      'text3', L["Primals"],
	  'text3R', 1,
	  'text3G', 1,
	  'text3B', 1
   )

   if not self.db.profile.includeBank then

   if self.db.profile.showEarth then
      display:AddLine(
      'text', L["Earth"],
	  'textR' , Earth_R, 'textG' , Earth_G, 'textB' , Earth_B,
      'text2', carried.mEarth,
	  'text2R', Earth_R, 'text2G', Earth_G, 'text2B', Earth_B,
      'text3', carried.pEarth,
	  'text3R', Earth_R, 'text3G', Earth_G, 'text3B', Earth_B
      )
   end
   if self.db.profile.showAir then
      display:AddLine(
      'text', L["Air"],
	  'textR' , Air_R, 'textG' , Air_G, 'textB' , Air_B,
      'text2', carried.mAir,
	  'text2R', Air_R, 'text2G', Air_G, 'text2B', Air_B,
      'text3', carried.pAir,
	  'text3R', Air_R, 'text3G', Air_G, 'text3B', Air_B
	  )
   end
   if self.db.profile.showFire then
      display:AddLine(
      'text', L["Fire"],
	  'textR' , Fire_R, 'textG' , Fire_G, 'textB' , Fire_B,
      'text2', carried.mFire,
	  'text2R', Fire_R, 'text2G', Fire_G, 'text2B', Fire_B,
      'text3', carried.pFire,
	  'text3R', Fire_R, 'text3G', Fire_G, 'text3B', Fire_B
      )
   end
   if self.db.profile.showWater then
      display:AddLine(
      'text', L["Water"],
	  'textR' , Water_R, 'textG' , Water_G, 'textB' , Water_B,
      'text2', carried.mWater,
	  'text2R', Water_R, 'text2G', Water_G, 'text2B', Water_B,
      'text3', carried.pWater,
	  'text3R', Water_R, 'text3G', Water_G, 'text3B', Water_B
      )
   end
   if self.db.profile.showMana then
      display:AddLine(
      'text', L["Mana"],
	  'textR' , Mana_R, 'textG' , Mana_G, 'textB' , Mana_B,
      'text2', carried.mMana,
	  'text2R', Mana_R, 'text2G', Mana_G, 'text2B', Mana_B,
      'text3', carried.pMana,
	  'text3R', Mana_R, 'text3G', Mana_G, 'text3B', Mana_B
      )
   end
   if self.db.profile.showLife then
      display:AddLine(
      'text', L["Life"],
	  'textR' , Life_R, 'textG' , Life_G, 'textB' , Life_B,
      'text2', carried.mLife,
	  'text2R', Life_R, 'text2G', Life_G, 'text2B', Life_B,
      'text3', carried.pLife,
	  'text3R', Life_R, 'text3G', Life_G, 'text3B', Life_B
      )
   end
   if self.db.profile.showShadow then
      display:AddLine(
      'text', L["Shadow"],
	  'textR' , Shadow_R, 'textG' , Shadow_G, 'textB' , Shadow_B,
      'text2', carried.mShadow,
	  'text2R', Shadow_R, 'text2G', Shadow_G, 'text2B', Shadow_B,
      'text3', carried.pShadow,
	  'text3R', Shadow_R, 'text3G', Shadow_G, 'text3B', Shadow_B
      )
   end
   if self.db.profile.showMight then
      display:AddLine(
      'text', L["Might"],
	  'textR' , Might_R, 'textG' , Might_G, 'textB' , Might_B,
      'text3', carried.pMight,
	  'text3R', Might_R, 'text3G', Might_G, 'text3B', Might_B
      )
   end
   if self.db.profile.showNether then
      display:AddLine(
      'text', L["Nether"],
	  'textR' , Nether_R, 'textG' , Nether_G, 'textB' , Nether_B,
      'text3', carried.pNether,
	  'text3R', Nether_R, 'text3G', Nether_G, 'text3B', Nether_B
	  )
   end


   else -- include bank counts

   self.myData = self.db.account[self.REALM or GetRealmName()][self.NAME or UnitName("player")]
	local bank = self.myData

   if self.db.profile.showEarth then
      display:AddLine(
      'text', L["Earth"],
	  'textR' , Earth_R, 'textG' , Earth_G, 'textB' , Earth_B,
      'text2', carried.mEarth  .. " (" .. bank.mEarth  .. ")",
	  'text2R', Earth_R, 'text2G', Earth_G, 'text2B', Earth_B,
	  'text3', carried.pEarth  .. " (" .. bank.pEarth  .. ")",
	  'text3R', Earth_R, 'text3G', Earth_G, 'text3B', Earth_B
	  )
   end
   if self.db.profile.showAir then
      display:AddLine(
      'text', L["Air"],
	  'textR' , Air_R, 'textG' , Air_G, 'textB' , Air_B,
      'text2', carried.mAir    .. " (" .. bank.mAir    .. ")",
	  'text2R', Air_R, 'text2G', Air_G, 'text2B', Air_B,
	  'text3', carried.pAir    .. " (" .. bank.pAir    .. ")",
	  'text3R', Air_R, 'text3G', Air_G, 'text3B', Air_B
	  )
   end
   if self.db.profile.showFire then
      display:AddLine(
      'text', L["Fire"],
	  'textR' , Fire_R, 'textG' , Fire_G, 'textB' , Fire_B,
      'text2', carried.mFire   .. " (" .. bank.mFire   .. ")",
	  'text2R', Fire_R, 'text2G', Fire_G, 'text2B', Fire_B,
      'text3', carried.pFire   .. " (" .. bank.pFire   .. ")",
	  'text3R', Fire_R, 'text3G', Fire_G, 'text3B', Fire_B
	  )
   end
   if self.db.profile.showWater then
      display:AddLine(
      'text', L["Water"],
	  'textR' , Water_R, 'textG' , Water_G, 'textB' , Water_B,
      'text2', carried.mWater  .. " (" .. bank.mWater  .. ")",
	  'text2R', Water_R, 'text2G', Water_G, 'text2B', Water_B,
	  'text3', carried.pWater  .. " (" .. bank.pWater  .. ")",
	  'text3R', Water_R, 'text3G', Water_G, 'text3B', Water_B
	  )
   end
   if self.db.profile.showMana then
      display:AddLine(
      'text', L["Mana"],
	  'textR' , Mana_R, 'textG' , Mana_G, 'textB' , Mana_B,
      'text2', carried.mMana   .. " (" .. bank.mMana   .. ")",
	  'text2R', Mana_R, 'text2G', Mana_G, 'text2B', Mana_B,
      'text3', carried.pMana   .. " (" .. bank.pMana   .. ")",
	  'text3R', Mana_R, 'text3G', Mana_G, 'text3B', Mana_B
	  )
   end
   if self.db.profile.showLife then
      display:AddLine(
      'text', L["Life"],
	  'textR' , Life_R, 'textG' , Life_G, 'textB' , Life_B,
      'text2', carried.mLife   .. " (" .. bank.mLife   .. ")",
	  'text2R', Life_R, 'text2G', Life_G, 'text2B', Life_B,
      'text3', carried.pLife   .. " (" .. bank.pLife   .. ")",
	  'text3R', Life_R, 'text3G', Life_G, 'text3B', Life_B
	  )
   end
   if self.db.profile.showShadow then
      display:AddLine(
      'text', L["Shadow"],
	  'textR' , Shadow_R, 'textG' , Shadow_G, 'textB' , Shadow_B,
      'text2', carried.mShadow .. " (" .. bank.mShadow .. ")",
	  'text2R', Shadow_R, 'text2G', Shadow_G, 'text2B', Shadow_B,
      'text3', carried.pShadow .. " (" .. bank.pShadow .. ")",
	  'text3R', Shadow_R, 'text3G', Shadow_G, 'text3B', Shadow_B
	  )
   end
   if self.db.profile.showMight then
      display:AddLine(
      'text', L["Might"],
	  'textR' , Might_R, 'textG' , Might_G, 'textB' , Might_B,
      'text3', carried.pMight  .. " (" .. bank.pMight  .. ")",
	  'text3R', Might_R, 'text3G', Might_G, 'text3B', Might_B
	  )
   end
   if self.db.profile.showNether then
      display:AddLine(
      'text', L["Nether"],
	  'textR' , Nether_R, 'textG' , Nether_G, 'textB' , Nether_B,
      'text3', carried.pNether .. " (" .. bank.pNether .. ")",
	  'text3R', Nether_R, 'text3G', Nether_G, 'text3B', Nether_B
	  )
   end
   
   end

end

function MoteFu:OnMenuRequest(level, value)
	if level == 1 then
		dewdrop:AddLine(
			'text', L["Show Earth"],
			'arg1', self,
			'func', function()
				self.db.profile.showEarth = not self.db.profile.showEarth
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showEarth
		)
		dewdrop:AddLine(
			'text', L["Show Air"],
			'arg1', self,
			'func', function()
				self.db.profile.showAir = not self.db.profile.showAir
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showAir
		)
		dewdrop:AddLine(
			'text', L["Show Fire"],
			'arg1', self,
			'func', function()
				self.db.profile.showFire = not self.db.profile.showFire
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showFire
		)
		dewdrop:AddLine(
			'text', L["Show Water"],
			'arg1', self,
			'func', function()
				self.db.profile.showWater = not self.db.profile.showWater
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showWater
		)
		dewdrop:AddLine(
			'text', L["Show Mana"],
			'arg1', self,
			'func', function()
				self.db.profile.showMana = not self.db.profile.showMana
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showMana
		)
		dewdrop:AddLine(
			'text', L["Show Life"],
			'arg1', self,
			'func', function()
				self.db.profile.showLife = not self.db.profile.showLife
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showLife
		)
		dewdrop:AddLine(
			'text', L["Show Shadow"],
			'arg1', self,
			'func', function()
				self.db.profile.showShadow = not self.db.profile.showShadow
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showShadow
		)
		dewdrop:AddLine(
			'text', L["Show Primal Might"],
			'arg1', self,
			'func', function()
				self.db.profile.showMight = not self.db.profile.showMight
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showMight
		)		
		dewdrop:AddLine(
			'text', L["Show Primal Nether"],
			'arg1', self,
			'func', function()
				self.db.profile.showNether = not self.db.profile.showNether
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.showNether
		)				
        dewdrop:AddLine()
		dewdrop:AddLine(
			'text', L["Include Bank Counts"],
			'arg1', self,
			'func', function()
				self.db.profile.includeBank = not self.db.profile.includeBank
				self:UpdateTooltip()
			end,
			'checked', self.db.profile.includeBank
		)
		dewdrop:AddLine()
   elseif level == 2 then
      -- future site of character select option
   end
end
