BINDING_HEADER_BLINKTRASH = "블링크의 잡템 정리";
BINDING_NAME_BLINKTRASH_SELLTRASH = "잡템 팔기";

BLINKTRASH_DEBUG = false;
BLINKTRASH_ITEMS = {};
BLINKTRASH_SELLCOUNT = 12; -- 한번에 판매할 갯수 : 0일경우 한꺼번에 다 팜

function BlinkTrash_Print(msg, r, g, b , frame)
	if ( not r ) then
		r = 1.0;
	end
	if ( not g ) then
		g = 1.0;
	end
	if ( not b ) then
		b = 0.0;
	end
	if ( not msg or msg == nil ) then
		msg = "-nil-";
	end
	if( frame ) then
		frame:AddMessage(msg, r, g, b);
	else
		ChatFrame1:AddMessage(msg, r, g, b);
	end
end

--function BlinkTrash_OnLoad()
--	BLINKTRASH_ITEMS = {};
--end

function BlinkTrash_IsTrash(bag,slot)
	local result = false;
	local link = GetContainerItemLink(bag, slot);
	if ( link ) then
		local itemName, itemLink, itemRarity = GetItemInfo(link);
		if( itemName and itemRarity ) then
			if ( itemRarity == 0 ) then -- 0 is poor item. 0~6
				return true;
			end
		end
	end
end

function BlinkTrash_ItemIsLocked(bag,slot)
	if bag == -1 and slot == -1 then return false; end

	if bag == -1 then
		return IsInventoryItemLocked(slot);
	else
		local _,_,retval = GetContainerItemInfo(bag,slot);
		return retval;
	end
end

function BlinkTrash_ScanBag()
	BLINKTRASH_ITEMS = {};
	local maxslot = 0;
	local emptyslot = 0;
	for i=0, NUM_BAG_FRAMES do
		for j=1, GetContainerNumSlots(i) do
			local _,stackCount = GetContainerItemInfo(i,j);
			local link = GetContainerItemLink(i, j);
			local name, id;
			if ( link ) then
				name = GetItemInfo(link);
				id = BlinkTrash_LinkToID(link);
			end
			if ( not name ) then
				emptyslot = emptyslot + 1;
			else
				if ( BlinkTrash_IsTrash(i,j) == true ) then 
					table.insert( BLINKTRASH_ITEMS, {bag=i,slot=j,name=name,id=id,count=stackCount} );
				end
			end
			maxslot = maxslot + 1;
		end
	end
	return emptyslot, maxslot;
end

function BlinkTrash_LinkToID(link)
	if link then
		local _, _, id = string.find(link, "(%d+):");
		return id;
	end
end

function BlinkTrash_SellTrashItems()
	local emptyslot,maxslot = BlinkTrash_ScanBag();
	local item_count = #BLINKTRASH_ITEMS;
	local sold = 0;
	if ( item_count > 0 and MerchantFrame:IsVisible() ) then
		if ( InCombatLockdown() ) then
			BlinkTrash_Print("전투중에는 아이템을 팔수 없습니다.");
			return;
		end
		local items = BlinkTrash_PopItems();
		if ( items and type(items)=='table' ) then
			for idx, item in ipairs(items) do
				if ( BlinkTrash_ItemIsLocked(item.bag,item.slot) ) then
					-- do nothing
				else
					BlinkTrash_Print(string.format("%s|1을;를;",GetContainerItemLink(item.bag,item.slot)).." 상점에 팔았습니다.");
					UseContainerItem( item.bag, item.slot );
					sold = sold + 1;
				end
			end
		end
		local remain = #BLINKTRASH_ITEMS;
		BlinkTrash_Print(""..sold.. "개의 잡템을 팔고, ".. remain.. "개의 잡템이 남았습니다.")
	elseif ( item_count > 0 and not MerchantFrame:IsVisible() ) then
		BlinkTrash_Print("상점창이 열려있지 않으면 팔 수 없습니다. ("..item_count.."개)")
		local strPrice = BlinkTrash_GetPrice();
		if ( strPrice and strPrice~="" ) then
			BlinkTrash_Print("판매예상금액 ("..strPrice..")");
		end
	else
		BlinkTrash_Print("정리할 잡템이 없습니다.");
	end
	BlinkTrash_Print("가방 "..(maxslot-emptyslot).." / "..maxslot);
end

function BlinkTrash_PopItems()
	local result = {};
	if ( BLINKTRASH_SELLCOUNT > 0 ) then
		if ( #BLINKTRASH_ITEMS >= BLINKTRASH_SELLCOUNT ) then
			for i=1, BLINKTRASH_SELLCOUNT, 1 do
				local item = table.remove(BLINKTRASH_ITEMS,1);
				table.insert(result,item);
			end
			return result;
		elseif ( #BLINKTRASH_ITEMS > 0 ) then
			local count = #BLINKTRASH_ITEMS;
			for i=1, count, 1 do
				local item = table.remove(BLINKTRASH_ITEMS,1);
				table.insert(result,item);
			end
			return result;
		else
			return nil;
		end
	else
		return BLINKTRASH_ITEMS;
	end
end

function BlinkTrash_GetPrice()
	local item_count = #BLINKTRASH_ITEMS;
	local totalprice = 0;
	local uncounted = 0;
	local strPrice = "";
	if item_count > 0 and SellValues and InvList_ShortenItemName then
		for k,item in pairs(BLINKTRASH_ITEMS) do
			local itemShortName = InvList_ShortenItemName(item.name);
			local price = SellValues[itemShortName];
			if (price and price > 0) then
				totalprice = totalprice + ( price * item.count );
			else
				uncounted = uncounted + 1;
			end
		end
	elseif item_count > 0 and ColaLight and ColaLight.db.account.SellValues then
		for k,item in pairs(BLINKTRASH_ITEMS) do
			local price = ColaLight.db.account.SellValues[item.id];
			if (price and price > 0) then
				totalprice = totalprice + ( price * item.count );
			else
				uncounted = uncounted + 1;
			end
		end
	end
	local gold,silver,copper = BlinkTrash_GoldFormat(totalprice);
	if ( gold > 0 ) then
		strPrice = "|cffffdd00"..gold.." 골드|r |cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
	elseif ( silver > 0 ) then
		strPrice = "|cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
	elseif ( copper > 0 ) then
		strPrice = "|cffff8800"..copper.." 쿠퍼|r";
	end
	if (totalprice>0 and uncounted>0) then
		strPrice = strPrice .. " |cff00dd00이상|r";
	elseif ( totalprice == 0 and uncounted>0 ) then
		strPrice = strPrice .. "|cff9d9d9d알수없음|r";
	end
	return strPrice;
--
--	if item_count > 0 and ( (SellValues and InvList_ShortenItemName) or (ColaLight and ColaLight.db.account.SellValues) ) then
--		if SellValues and InvList_ShortenItemName then
--			for k,item in pairs(BLINKTRASH_ITEMS) do
--				local itemShortName = InvList_ShortenItemName(item.name);
--				local price = SellValues[itemShortName];
--				if (price and price > 0) then
--					totalprice = totalprice + ( price * item.count );
--				else
--					uncounted = uncounted + 1;
--				end
--			end
--		elseif ColaLight and ColaLight.db.account.SellValues then
--			for k,item in pairs(BLINKTRASH_ITEMS) do
--				local price = ColaLight.db.account.SellValues[item.id];
--				if (price and price > 0) then
--					totalprice = totalprice + ( price * item.count );
--				else
--					uncounted = uncounted + 1;
--				end
--			end
--		end
--
--		local gold,silver,copper = BlinkTrash_GoldFormat(totalprice);
--		if ( gold > 0 ) then
--			strPrice = "|cffffdd00"..gold.." 골드|r |cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
--		elseif ( silver > 0 ) then
--			strPrice = "|cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
--		elseif ( copper > 0 ) then
--			strPrice = "|cffff8800"..copper.." 쿠퍼|r";
--		end
--		if (totalprice>0 and uncounted>0) then
--			strPrice = strPrice .. " |cff00dd00이상|r";
--		elseif ( totalprice == 0 and uncounted>0 ) then
--			strPrice = strPrice .. "|cff9d9d9d알수없음|r";
--		end
--		return strPrice;
----	elseif ( item_count > 0 and ColaLight and ColaLight.db.account.SellValues ) then
----		for k,item in pairs(BLINKTRASH_ITEMS) do
----			local price = ColaLight.db.account.SellValues[item.id];
----			if (price and price > 0) then
----				totalprice = totalprice + ( price * item.count );
----			else
----				uncounted = uncounted + 1;
----			end
----		end
----		local gold,silver,copper = BlinkTrash_GoldFormat(totalprice);
----		if ( gold > 0 ) then
----			strPrice = "|cffffdd00"..gold.." 골드|r |cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
----		elseif ( silver > 0 ) then
----			strPrice = "|cffdddddd"..silver.." 실버|r |cffff8800"..copper.." 쿠퍼|r";
----		elseif ( copper > 0 ) then
----			strPrice = "|cffff8800"..copper.." 쿠퍼|r";
----		end
----		if (totalprice>0 and uncounted>0) then
----			strPrice = strPrice .. " |cff00dd00이상|r";
----		elseif ( totalprice == 0 and uncounted>0 ) then
----			strPrice = strPrice .. "|cff9d9d9d알수없음|r";
----		end
----		return strPrice;
--	else
--		return nil;
--	end
end

function BlinkTrash_GoldFormat(money)
	local gold = 0;
	local silver = 0;
	local copper = 0;
	if ( money >= 10000 ) then
		gold = math.floor(money/10000);
		money = money - (gold*10000);
	end
	if ( money >= 100 ) then
		silver = math.floor(money/100);
		money = money - (silver*100);
	end
	copper = money;
	return gold,silver,copper;
end
