local function stadium_id(ctx)
    stadium = ctx.stadium
	if string.len(stadium) == 1 then
	   stid = "st00" .. stadium
	elseif string.len(stadium) == 2 then
	   stid = "st0" .. stadium
	elseif string.len(stadium) == 3 then
	   stid = "st" .. stadium
	end
	ad_cl = string.format("ad_%s_cl.fpk", stid)
	ad_uel = string.format("ad_%s_el.fpk", stid)
	ad_sc = string.format("ad_%s_sc.fpk", stid)
end

function rewrite(ctx, filename)
    if ad_cl and ad_uel and ad_sc then
       if ctx.tournament_id == 2 or ctx.tournament_id == 3 or ctx.tournament_id == 4 or ctx.tournament_id == 400 or ctx.tournament_id == 401 or ctx.tournament_id == 402 or ctx.tournament_id == 403 or ctx.tournament_id == 404 or ctx.tournament_id == 405 or ctx.tournament_id == 406 or ctx.tournament_id == 600  or ctx.tournament_id == 601 or ctx.tournament_id == 602 or ctx.tournament_id == 603 or ctx.tournament_id == 604 or ctx.tournament_id == 605 or ctx.tournament_id == 606 or ctx.tournament_id == 607 or ctx.tournament_id == 34 or ctx.tournament_id == 35 or ctx.tournament_id == 36 or ctx.tournament_id == 37 or ctx.tournament_id == 38 or ctx.tournament_id == 39 or ctx.tournament_id == 40 or ctx.tournament_id == 6800 or ctx.tournament_id == 6801 or ctx.tournament_id == 6802 or ctx.tournament_id == 6803 or ctx.tournament_id == 6804 or ctx.tournament_id == 6805 or ctx.tournament_id == 6806 or ctx.tournament_id == 6807 or ctx.tournament_id == 7200 or ctx.tournament_id == 7201 or ctx.tournament_id == 7202 or ctx.tournament_id == 7203 or ctx.tournament_id == 7204 or ctx.tournament_id == 7205 or ctx.tournament_id == 7206 or ctx.tournament_id == 7207 or ctx.tournament_id == 7400 or ctx.tournament_id == 7600 or ctx.tournament_id == 7800 or ctx.tournament_id == 7801 or ctx.tournament_id == 8000 or ctx.tournament_id == 8001 then
     	  return string.gsub(filename, "ad_st%d%d%d_normal.fpk", ad_cl)
	   elseif ctx.tournament_id == 5 or ctx.tournament_id == 1000 or ctx.tournament_id == 1001 or ctx.tournament_id == 1002 or ctx.tournament_id == 1003 or ctx.tournament_id == 1004 or ctx.tournament_id == 1005 or ctx.tournament_id == 1006 or ctx.tournament_id == 1007 or ctx.tournament_id == 1008 or ctx.tournament_id == 1009  or ctx.tournament_id == 1010 or ctx.tournament_id == 1011 or ctx.tournament_id == 6 then
          return string.gsub(filename, "ad_st%d%d%d_normal.fpk", ad_uel)
	   elseif ctx.tournament_id == 7 then
          return string.gsub(filename, "ad_st%d%d%d_normal.fpk", ad_sc)
	   else
		  return nil
       end
	end
end

local function get_filepath(ctx, filename, key)
    if key then
       if ctx.tournament_id == 2 or ctx.tournament_id == 3 or ctx.tournament_id == 4 or ctx.tournament_id == 400 or ctx.tournament_id == 401 or ctx.tournament_id == 402 or ctx.tournament_id == 403 or ctx.tournament_id == 404 or ctx.tournament_id == 405 or ctx.tournament_id == 406 or ctx.tournament_id == 600  or ctx.tournament_id == 601 or ctx.tournament_id == 602 or ctx.tournament_id == 603 or ctx.tournament_id == 604 or ctx.tournament_id == 605 or ctx.tournament_id == 606 or ctx.tournament_id == 607 then
	    	filepath = ctx.sider_dir .. ".\\modules\\EvoSwitcher\\EvoBanner\\" .. filename
	   elseif ctx.tournament_id == 5 or ctx.tournament_id == 1000 or ctx.tournament_id == 1001 or ctx.tournament_id == 1002 or ctx.tournament_id == 1003 or ctx.tournament_id == 1004 or ctx.tournament_id == 1005 or ctx.tournament_id == 1006 or ctx.tournament_id == 1007 or ctx.tournament_id == 1008 or ctx.tournament_id == 1009  or ctx.tournament_id == 1010 or ctx.tournament_id == 1011 or ctx.tournament_id == 6 then
	    	filepath = ctx.sider_dir .. ".\\modules\\EvoSwitcher\\EvoBanner\\" .. filename
	   elseif ctx.tournament_id == 7 then
	    	filepath = ctx.sider_dir .. ".\\modules\\EvoSwitcher\\EvoBanner\\" .. filename
	   elseif ctx.tournament_id == 34 or ctx.tournament_id == 35 or ctx.tournament_id == 36 or ctx.tournament_id == 37 or ctx.tournament_id == 38 or ctx.tournament_id == 39 or ctx.tournament_id == 40 or ctx.tournament_id == 6800 or ctx.tournament_id == 6801 or ctx.tournament_id == 6802 or ctx.tournament_id == 6803 or ctx.tournament_id == 6804 or ctx.tournament_id == 6805 or ctx.tournament_id == 6806 or ctx.tournament_id == 6807 or ctx.tournament_id == 7200 or ctx.tournament_id == 7201 or ctx.tournament_id == 7202 or ctx.tournament_id == 7203 or ctx.tournament_id == 7204 or ctx.tournament_id == 7205 or ctx.tournament_id == 7206 or ctx.tournament_id == 7207 or ctx.tournament_id == 7400 or ctx.tournament_id == 7600 or ctx.tournament_id == 7800 or ctx.tournament_id == 7801 or ctx.tournament_id == 8000 or ctx.tournament_id == 8001 then
	   		if ctx.match_info == 53 then
	    		filepath = ctx.sider_dir .. ".\\modules\\EvoSwitcher\\EvoBanner\\FIFA World Cup\\Final\\" .. filename
	    	else
	    		filepath = ctx.sider_dir .. ".\\modules\\EvoSwitcher\\EvoBanner\\FIFA World Cup\\Group_Knockout\\" .. filename
	    	end
	   else
       end
       return filepath
    end
end


function init(ctx)
    ctx.register("livecpk_rewrite", rewrite)
    ctx.register("after_set_conditions", stadium_id)
    ctx.register("livecpk_get_filepath", get_filepath)
end

return { init = init }