وحدة:ملعب/Mr. Ibrahem/du

local p = {}
local Frame = mw.getCurrentFrame()
local Delink = require('Module:Delink2')
local Frame;
local ar_entity;
local en_entity;

function p.trim(s)
	ss = s
	if s and s ~= "" then
    	ss = mw.ustring.gsub(ss, "^%s*(.-)%s*$", "%1") or ""
    end
    return ss
end
-- {{#invoke:ملعب/Mr. Ibrahem/du|w}}
function p.sitelink( entity , site )
	if entity
	and entity.sitelinks 
	and entity.sitelinks[''..site..'']
	and entity.sitelinks[''..site..''].site 
	and entity.sitelinks[''..site..''].title 
	then
		if entity.sitelinks[''..site..''].site == site then
		return entity.sitelinks[''..site..''].title
		else return ''
		end
	end
	return ''
end

function p.countSiteLinks(entity)
	numb = 0
	if entity and entity.sitelinks then
		for i, v in pairs(entity.sitelinks) do 
			numb = numb +1
		end
	end
	return numb
end


function p.text( String )
	if String and String ~= "" then return true end
end

function p.remove_wiki( title )
	title = string.gsub( title, '%[%[%:تصنيف%:', '' )
	title = string.gsub( title, '%[%[%:تصنيف', '' )
	title = string.gsub( title, '%]%]', '' )
	return title
end

function p.getrtarget( cate )
	cate = p.remove_wiki(cate)
	local catcontent = mw.title.new( cate or '', 'Category' ):getContent()
	if string.match( catcontent or '', '{{ *تحويل' ) then
		local rtarget = mw.ustring.match( catcontent, '{{تحويل تصنيف%s*|%s*([^|}]+)' )
		if rtarget then
			rtarget = mw.ustring.gsub(rtarget, '^1%s*=%s*', '')
			rtarget = string.gsub(rtarget, '^[Cc]ategory:', '')
			rtarget = string.gsub(rtarget, '^تصنيف:', '')
			return rtarget
		end
	end
	return false
end

function p.bgcolor( t )
	result = 'bgcolor="#f5ab62"'
	at = { t.arcat , "" , t.encat , t.encat_in_ar_qid , "" , t.arcat_in_en_qid}
	--op = mw.getCurrentFrame():expandTemplate{ title = "user:Mr._Ibrahem/duplicate2/bgcolor"	,  args = at }
	op = mw.getCurrentFrame():expandTemplate{ title = "user:Mr._Ibrahem/duplicate2/bgcolor"	,  args = at }
	
	if p.text( t.ar_qid ) and p.text( t.en_qid ) and  t.ar_qid == t.en_qid
		then 
			mw.log("same Qid " .. t.ar_qid  )
			return result
	end
	
	if p.text( t.arcat ) and p.text( t.arcat_in_en_qid ) then
		ar_catr = p.getrtarget( t.arcat )
		
		ar_in_en_qid = p.remove_wiki( t.arcat_in_en_qid )
		mw.log( 'ar_in_en_qid : ' .. ar_in_en_qid )
		
		if ar_catr and ar_catr == ar_in_en_qid then 
			mw.log( 'ar_catr : ' .. ar_catr )
			mw.log("arcat has تحويل تصنيف to " .. ar_in_en_qid  )
			return result .. 'xx'
		end
	end

	if p.text( t.arcat ) and p.text( t.arcat_in_en_qid ) and  t.arcat == t.arcat_in_en_qid
		then 
			mw.log("same arcat " .. t.arcat  )
			return result
	end
	if p.text( t.Delink_arcat ) and p.text( t.arcat_in_en_qid ) and  t.Delink_arcat == t.arcat_in_en_qid
		then 
			mw.log("same Delink_arcat " .. t.Delink_arcat  )
			return result
	end
	if p.text( t.encat ) and p.text( t.encat_in_ar_qid ) then
		if t.encat == t.encat_in_ar_qid
			then 
				mw.log("same encat " .. t.encat  )
				return result
		else
				mw.log("'" .. t.encat .. " ' ~= '" .. t.encat_in_ar_qid .."'")
		end
	end
	
	return ""
end

function p.render_result( t )
	local op = p.bgcolor(t)
	local has_redirect = false
	if op == 'bgcolor="#f5ab62"xx' then
		op = 'bgcolor="#f5ab62"'
		has_redirect = true
	end
	mw.log(op)
    Args =  { 
			bgcolor = op
			, arcat = t.arcat
			, arcat_has_redirect = tostring(has_redirect)
			, ar_qid = t.ar_qid
			, count_ar = t.count_ar
			, encat_in_ar_qid = t.encat_in_ar_qid
			, arcat_in_en_qid = t.arcat_in_en_qid
			, en_qid = t.en_qid
			, encat = t.encat
			, count_en = t.count_en
		}
    --rp = mw.getCurrentFrame():expandTemplate{ 	title = "user:Mr._Ibrahem/duplicate2/core_raw3" , args = Args } 
	--mw.log(rp)
	local tit = "user:Mr._Ibrahem/duplicate2/core_raw1"
	--local tit = "user:Mr._Ibrahem/duplicate2/core_raw2"
	result = mw.getCurrentFrame():expandTemplate{ title = tit	, args = Args } 
	return result
end

function p.render_args( args )
	local t = {}
	t.arcat = p.trim(args[1] or args["arcat"] or "")
	t.Delink_arcat = Delink._delink( t.arcat )
	t.ar_qid = p.trim(args[2] or args["ar_qid"] or "")
	if not t.ar_qid or t.ar_qid == "" then
		t.ar_qid = mw.wikibase.getEntityIdForTitle( t.Delink_arcat ) or ""
		mw.log("make qid ".. t.ar_qid .. " from title" .. t.Delink_arcat)
	end
	if t.ar_qid == false then t.ar_qid = '' end
	if t.ar_qid and t.ar_qid ~= "" then
		ar_entity = mw.wikibase.getEntityObject(t.ar_qid)
	end
	t.count_ar = p.countSiteLinks( ar_entity )
	t.encat_in_ar_qid = args[3] or args["encat_in_ar_qid"] or ""
	
	if not t.encat_in_ar_qid or t.encat_in_ar_qid == "" then
		t.encat_in_ar_qid = p.sitelink( ar_entity , "enwiki" )
		if t.encat_in_ar_qid and t.encat_in_ar_qid ~= "" then
			mw.log("make encat	".. t.encat_in_ar_qid .. " from ar_qid" .. t.ar_qid)
		end
	end
	
	t.encat = args[4] or args["encat"] or "" 
	if t.encat ~= "" then t.encat = mw.ustring.gsub(t.encat, "en:", "")  end
	t.encat = Delink._delink( t.encat )
	t.encat = p.trim( t.encat )
	t.en_qid = p.trim(args["en_qid"] or args[5] or "") 
	
	if t.en_qid and t.en_qid ~= "" then
		en_entity = mw.wikibase.getEntityObject(t.en_qid)
	end
	t.count_en = p.countSiteLinks( en_entity )
	t.arcat_in_en_qid = p.trim(args[6] or "")
	if not t.arcat_in_en_qid or t.arcat_in_en_qid == "" then
		t.arcat_in_en_qid = p.sitelink( en_entity , "arwiki" )
		if t.arcat_in_en_qid and t.arcat_in_en_qid ~= "" then
			mw.log("make arcat	".. t.arcat_in_en_qid .. " from en_qid" .. t.en_qid)
		end
	end
	return p.render_result( t )
end

function p.w(frame)
	frame = frame:getParent()
	Frame = frame
	local t = p.render_args(frame.args)
	return t
end

function p.ww(frame)
	Frame = frame
	local t = p.render_args(frame.args)
	return t
end

function p.render_arg_e( frame )
	local t = {}
	local argss = frame.args or frame:getParent().args
	t.encat = argss[4] or ""
	if t.encat ~= "" then t.encat = mw.ustring.gsub(t.encat, "en:", "")  end
	mw.log("t.encat" .. t.encat)
	t.encat = Delink._delink( t.encat )
	t.encat = p.trim( t.encat )
	t.en_qid = p.trim(argss[5] or argss["en_qid"] or "")
	if t.en_qid  == "" or t.en_qid  == nil then 
		return "" .. t.encat 
	else
		return "" .. t.en_qid 
	end
end

function p.we(frame)
	frame = frame:getParent()
	Frame = frame
	local t = p.render_arg_e(frame)
	return t
end

return p