local AC = AutoCategory
local LCK = LibCharacterKnowledge

-- A very simple plugin for several one-liner addons which only have
-- a single rule function to register.
--
-- No strings or predefined rules to load.

AutoCategory_MiscAddons = {
    RuleFunc = {},
}

--Initialize plugin for Auto Category - Misc Addons
function AutoCategory_MiscAddons.Initialize()

    -- Master Merchant
	if MasterMerchant then
        AutoCategory.AddRuleFunc("getpricemm", 
            AutoCategory_MiscAddons.RuleFunc.GetPriceMM)
        AutoCategory.AddRuleFunc("mm_getprice", 
            AutoCategory_MiscAddons.RuleFunc.GetPriceMM)
        AutoCat_Logger():Info("Initializing Master Merchant plugin integration")
    else
        -- assign dummy rule functions
        AutoCategory.AddRuleFunc("getpricemm", AutoCategory.dummyRuleFunc)   -- always return false
        AutoCategory.AddRuleFunc("mm_getprice", AutoCategory.dummyRuleFunc)   -- always return false
    end
	
    -- Tamriel Trade Center
	if TamrielTradeCentre and TamrielTradeCentrePrice then
        AutoCategory.AddRuleFunc("getamountttc",
            AutoCategory_MiscAddons.RuleFunc.GetAmountTTC)    
        AutoCategory.AddRuleFunc("ttc_getamount",
            AutoCategory_MiscAddons.RuleFunc.GetAmountTTC)    
        AutoCategory.AddRuleFunc("getpricettc", AutoCategory_MiscAddons.RuleFunc.GetPriceTTC)
        AutoCategory.AddRuleFunc("ttc_getpricettc", AutoCategory_MiscAddons.RuleFunc.GetPriceTTC)
        AutoCat_Logger():Info("Initializing Tamriel Trade Center plugin integration")

    else
        -- assign dummy rule functions
        AutoCategory.AddRuleFunc("getamountttc", AutoCategory.dummyRuleFunc)   -- always return false
        AutoCategory.AddRuleFunc("ttc_getamount", AutoCategory.dummyRuleFunc)   -- always return false
        AutoCategory.AddRuleFunc("getpricettc", AutoCategory.dummyRuleFunc)   -- always return false
        AutoCategory.AddRuleFunc("ttc_getpricettc", AutoCategory.dummyRuleFunc)   -- always return false
    end
    
    -- AlphaGear
    if not AG then
        -- assign dummy rule functions
        AutoCategory.AddRuleFunc("alphagear", AutoCategory.dummyRuleFunc)   -- always return false
		
    else
        AutoCategory.AddRuleFunc("alphagear", AutoCategory_MiscAddons.RuleFunc.AlphaGear)
        AutoCat_Logger():Info("Initializing AlphaGear plugin integration")
    end

    -- SetTracker
    if not SetTrack then
        -- assign dummy rule functions
        AutoCategory.AddRuleFunc("istracked", AutoCategory.dummyRuleFunc)    -- always return false
		
    else
        AutoCategory.AddRuleFunc("istracked", AutoCategory_MiscAddons.RuleFunc.IsTracked)
	    AutoCat_Logger():Info("Initializing Set Tracker plugin integration")
    end

	-- Character Knowledge
	if not LibCharacterKnowledge then
        -- assign dummy rule functions
        AutoCategory.AddRuleFunc("ck_isknowncat", AutoCategory.dummyRuleFunc)    -- always return false
        AutoCategory.AddRuleFunc("ck_isknown", AutoCategory.dummyRuleFunc)    -- always return false
		
    else
        AutoCategory.AddRuleFunc("ck_isknowncat", AutoCategory_MiscAddons.RuleFunc.CK_IsKnownCat)
        AutoCategory.AddRuleFunc("ck_isknown", AutoCategory_MiscAddons.RuleFunc.CK_IsKnown)
	    AutoCat_Logger():Info("Initializing LibCharacterKnowledge plugin integration")
    end
    
    
end

local function getCurrentItemLink()
	return GetItemLink(AC.checkingItemBagId, AC.checkingItemSlotIndex)
end

-- Implement getpricemm() check function for Master Merchant
function AutoCategory_MiscAddons.RuleFunc.GetPriceMM( ... )
	--local fn = "mm_getprice"
	if MasterMerchant then
		local itemLink = getCurrentItemLink()
		local mmData = MasterMerchant:itemStats(itemLink, false)
        if (mmData.avgPrice ~= nil) then
            return mmData.avgPrice
        end
	end
	return 0 
end

-- Implement getpricettc() check function for Tamriel Trade Center
function AutoCategory_MiscAddons.RuleFunc.GetPriceTTC( ... )
	--local fn = "ttc_getprice"
	if TamrielTradeCentre and TamrielTradeCentrePrice then
		local itemLink = getCurrentItemLink()
		local priceInfo = TamrielTradeCentrePrice:GetPriceInfo(itemLink)
		if priceInfo then 
			local ac = select( '#', ... ) 
			if ac == 0 then
				--get suggested price
				if priceInfo.SuggestedPrice then
					return priceInfo.SuggestedPrice
				end
				
			else
				local arg = select( 1, ... )
				if type( arg ) == "string" then
					if arg == "average" then
						if priceInfo.Avg then
							return priceInfo.Avg
						end
						
					elseif arg == "suggested" then
						if priceInfo.SuggestedPrice then
							return priceInfo.SuggestedPrice
						end
						
					elseif arg == "both" then
						if priceInfo.SuggestedPrice then
							return priceInfo.SuggestedPrice
							
						elseif priceInfo.Avg then
							return priceInfo.Avg
						end
					end
				end
			end 
		end
	end
	return 0 
end

-- Implement getamountttc() check function for Tamriel Trade Center
function AutoCategory_MiscAddons.RuleFunc.GetAmountTTC( ... )
	--local fn = "ttc_getamount"
	if TamrielTradeCentre and TamrielTradeCentrePrice then
		local itemLink = getCurrentItemLink()
		local priceInfo = TamrielTradeCentrePrice:GetPriceInfo(itemLink)
		if priceInfo then
			return priceInfo.AmountCount
		end
	end
	return 0
end

-- Implement alphagear() check function for Alpha Gear
function AutoCategory_MiscAddons.RuleFunc.AlphaGear( ... ) 
	if not (AG ) then
		return false
	end
	local fn = "alphagear"
	local ac = select( '#', ... )
	if ac == 0 then
		error( string.format("error: %s(): require arguments." , fn))
	end
	
	local uid = Id64ToString(GetItemUniqueId(AC.checkingItemBagId, AC.checkingItemSlotIndex))
	if not uid then return false end

	for ax = 1, ac do 
		local arg = select( ax, ... )
		local comIndex = -1
		if not arg then
			error( string.format("error: %s():  argument is nil." , fn))
		end
		if type( arg ) == "number" then
			comIndex = arg
			
		elseif type( arg ) == "string" then
			comIndex = tonumber(arg)
			
		else
			error( string.format("error: %s(): argument is error." , fn ) )
		end
		
		local nr = comIndex
		if AG.setdata[nr].Set.gear > 0 then
			for slot = 1,14 do
				if AG.setdata[AG.setdata[nr].Set.gear].Gear[slot].id == uid then
					local setName = AG.setdata[nr].Set.text[1]
					AutoCategory.AdditionCategoryName = setName
					return true
				end
			end
		end 
	end
	
	return false 
end

-- Implement istracked() check function for Set Tracker
function AutoCategory_MiscAddons.RuleFunc.IsTracked( ... )
  local fn = "istracked"
  if SetTrack == nil then
    return false
  end
  local ac = select( '#', ... ) 
  local checkSets = {}
  for ax = 1, ac do
    
    local arg = select( ax, ... )
    if not arg then
      error( string.format("error: %s():  argument is nil." , fn))
    end
    checkSets[arg]=true
  end
  
  local iTrackIndex, sTrackName = SetTrack.GetTrackingInfo(AC.checkingItemBagId, AC.checkingItemSlotIndex)
  if iTrackIndex >= 0 then
    if ac > 0 then
      if checkSets[sTrackName] ~= nil then
        -- true only if a specified set name is tracked on this item
        return true
      end
      -- item was tracked but not one of the specified sets, so return false
      return false
    else
      -- specific set names weren't given so true if tracked at all
      return true
    end
  end
  -- not a set tracked by SetTrack
  return false  
end

--[[
local function getCharId( charname )
	local sv = {}
	for i = 1, GetNumCharacters() do
		local name, _, _, _, _, _, characterId = GetCharacterInfo(i)
		sv[name] = characterId
	end
	if sv[charname] ~= nil then
	    return sv[charname]
	end
	return nil
end
--]]

-- Get a list of all characters on all local accounts
local function initChars()
	if LibCharacterKnowledge == nil then
		return
	end
	AutoCategory_MiscAddons.server = zo_strsplit(" ", GetWorldName())[1]
	AutoCategory_MiscAddons.charlist = 
		LCK.GetCharacterList(AutoCategory_MiscAddons.server)
end

-- Implement ck_isknown() check function for Set Tracker
function AutoCategory_MiscAddons.RuleFunc.CK_IsKnown( ... )
	--local fn = "ck_isknown"
	if LibCharacterKnowledge == nil then
		return false
	end
	
	local itemLink = getCurrentItemLink()
	--local cat = LCK.GetItemCategory(itemLink)
	
	--local server = zo_strsplit(" ", GetWorldName())[1]
	if AutoCategory_MiscAddons.charlist == nil then
		initChars()
	end
	local ac = select( '#', ... )
	local crafter, knowledge
	if ac == 0 then
		knowledge = LCK.GetItemKnowledgeForCharacter(itemLink, server)
		
	else
		local arg = select(1, ...)
		for _, v in pairs(AutoCategory_MiscAddons.charlist) do
			local name = zo_strformat("<<1>>",v.name)
			if name == arg then
				crafter = v.id --characterId
			end
		end
		knowledge = LCK.GetItemKnowledgeForCharacter(itemLink, server, crafter)
	end
	if knowledge  == LCK.KNOWLEDGE_KNOWN then
		return true
	end
  -- not known by character
  return false  
end

-- Implement ck_isknowncat() check function for LibCharacterKnowledge
function AutoCategory_MiscAddons.RuleFunc.CK_IsKnownCat( ... )
	local fn = "ck_isknowncat"
	if LibCharacterKnowledge == nil then
		return false
	end

	-- check if item is in one of CK's defined categories
	local itemLink = getCurrentItemLink()
	local cat = LCK.GetItemCategory(itemLink)
	if cat == LCK.ITEM_CATEGORY_NONE then return false end
	
	-- decide which CK categories to look for
	local names = { 
		["recipe"] =  LCK.ITEM_CATEGORY_RECIPE, 
		["plan"] = LCK.ITEM_CATEGORY_PLAN, 
		["motif"] = LCK.ITEM_CATEGORY_MOTIF,
		["scribing"] = LCK.ITEM_CATEGORY_SCRIBING,
		}
	local chkCats = {}
	local ac = select( '#', ... )
	if ac == 0 then
		return true
	else
		for ax = 1, ac do
			local arg = select(ax, ...)
			if not arg or not names[arg] then 
				error( string.format("error: %s(): argument %d is error. %s not recognized." , fn, ax, arg ) )
			end
			
			chkCats[names[arg]] = true
		end
	end	
	if chkCats[cat] then
	    return true
	end
	return false  
end


-- Register this plugin with AutoCategory to be initialized and used when AutoCategory loads.
AutoCategory.RegisterPlugin("MiscAddons", AutoCategory_MiscAddons.Initialize)