using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Xml;
using System.Threading;
using DotNetWikiBot;

class MyBot : Bot
{
	public void MyFunction1()
	{
		// Write your own function here
	}

	public static void Main()
	{
		Site site = new Site("http://sv.wikipedia.org", "Innocent bot", "********");
		Site site2 = new Site("http://commons.wikimedia.org", "Innocent bot", "********");
		PageList pl = new PageList(site);
		int k = 1;
		for(k = 1; k<20; k++)
		{
			string sida = k.ToString();
			Page pc = new Page(site2, "Category:"+sida+" BC");
			Page p = new Page(site, sida+" f.Kr.");
			pc.Load();
			p.Load();
			if(pc.Exists() && p.Exists())
				pl.Add(p);
		}
		string a = "'ace', 'af', 'ak', 'als', 'am', 'ang', 'ab', 'ar', 'an', 'arc', 'roa-rup', 'frp', 'as', 'ast', 'gn', 'av', 'ay', 'az', 'bm', 'bn', 'bjn', 'zh-min-nan', 'nan', 'map-bms', 'ba', 'be', 'be-x-old', 'bh', 'bcl', 'bi', 'bar', 'bo', 'bs', 'br', 'bg', 'bxr', 'ca', 'cv', 'ceb', 'cs', 'ch', 'cbk-zam', 'ny', 'sn', 'tum', 'cho', 'co', 'cy', 'da', 'dk', 'pdc', 'de', 'dv', 'nv', 'dsb', 'dz', 'mh', 'et', 'el', 'eml', 'en', 'myv', 'es', 'eo', 'ext', 'eu', 'ee', 'fa', 'hif', 'fo', 'fr', 'fy', 'ff', 'fur', 'ga', 'gv', 'gag', 'gd', 'gl', 'gan', 'ki', 'glk', 'gu', 'got', 'hak', 'xal', 'ko', 'ha', 'haw', 'hy', 'hi', 'ho', 'hsb', 'hr', 'io', 'ig', 'ilo', 'bpy', 'id', 'ia', 'ie', 'iu', 'ik', 'os', 'xh', 'zu', 'is', 'it', 'he', 'jv', 'kl', 'kn', 'kr', 'pam', 'kbd', 'krc', 'ka', 'ks', 'csb', 'kk', 'kw', 'rw', 'ky', 'rn', 'sw', 'kv', 'kg', 'ht', 'ku', 'kj', 'mrj', 'lad', 'lbe', 'lo', 'la', 'ltg', 'lv', 'lb', 'lt', 'lij', 'li', 'ln', 'jbo', 'lg', 'lmo', 'hu', 'mk', 'mg', 'ml', 'mt', 'mi', 'mr', 'arz', 'mzn', 'ms', 'cdo', 'mwl', 'mdf', 'mo', 'mn', 'mus', 'my', 'nah', 'na', 'fj', 'nl', 'nds-nl', 'cr', 'ne', 'new', 'ja', 'nap', 'ce', 'frr', 'pih', 'no', 'nb', 'nn', 'nrm', 'nov', 'ii', 'oc', 'mhr', 'or', 'om', 'ng', 'hz', 'uz', 'pa', 'pi', 'pfl', 'pag', 'pnb', 'pap', 'ps', 'koi', 'km', 'pcd', 'pms', 'tpi', 'nds', 'pl', 'tokipona', 'tp', 'pnt', 'pt', 'aa', 'kaa', 'crh', 'ty', 'ksh', 'ro', 'rmy', 'rm', 'qu', 'rue', 'ru', 'sah', 'se', 'sm', 'sa', 'sg', 'sc', 'sco', 'stq', 'st', 'tn', 'sq', 'scn', 'si', 'simple', 'sd', 'ss', 'sk', 'cu', 'sl', 'szl', 'so', 'ckb', 'srn', 'sr', 'sh', 'su', 'fi', 'sv', 'tl', 'ta', 'kab', 'roa-tara', 'tt', 'te', 'tet', 'th', 'ti', 'tg', 'to', 'chr', 'chy', 've', 'tr', 'tk', 'tw', 'udm', 'bug', 'uk', 'ur', 'ug', 'za', 'vec', 'vi', 'vo', 'fiu-vro', 'wa', 'zh-classical', 'vls', 'war', 'wo', 'wuu', 'ts', 'yi', 'yo', 'zh-yue', 'diq', 'zea', 'bat-smg', 'zh', 'zh-tw', 'zh-cn'";
		
		foreach(Page p2 in pl)
		{
			
			string ngt = p2.title;
			ngt = ngt.Substring(0, ngt.IndexOf(" "))+" BC";
			p2.Load();
			if(p2.text.IndexOf("#Omdirigering") == -1 && p2.text.IndexOf("#OMDIRIGERING") == -1 && p2.text.IndexOf("#REDIR") == -1)
			{
				string[] iw = p2.GetInterWikiLinks();
                if(iw.Length>1)
					{
					int i = 0;
					int j = 0;
					for(i=0; i<=iw.Length-2; i++)
					{
						for(j=i+1; j<=iw.Length-1; j++)
						{
							string prei = "'"+iw[i].Substring(0,iw[i].IndexOf(":"))+"'";
							string prej = "'"+iw[j].Substring(0,iw[j].IndexOf(":"))+"'";
							if(a.IndexOf(prei)>a.IndexOf(prej))
							{
								string slask = iw[i];
								iw[i] = iw[j];
								iw[j] = slask;
								p2.text = p2.text.Replace("[["+iw[i]+"]]", "XYZPLACEHOLDERXYZ");
								p2.text = p2.text.Replace("[["+iw[j]+"]]", "ZYXPLACEHOLDERZYX");
								p2.text = p2.text.Replace("XYZPLACEHOLDERXYZ", "[["+iw[j]+"]]");
								p2.text = p2.text.Replace("ZYXPLACEHOLDERZYX", "[["+iw[i]+"]]");
							}
						}
					}
				}
				string text = p2.text;
				text = Regex.Replace(text, "\n==(?![ =])", "\n== ");
				text = Regex.Replace(text, "(?<![ =])==\n", " ==\n");
				text = Regex.Replace(text, "\n===(?![ =])", "\n=== ");
				text = Regex.Replace(text, "(?<![ =])===\n", " ===\n");
				text = Regex.Replace(text, "\n====(?![ =])", "\n==== ");
				text = Regex.Replace(text, "(?<![ =])====\n", " ====\n");
				if(text.IndexOf("<pre>") == -1 && text.IndexOf("<nowiki>") == -1 && text.IndexOf("<!--") == -1)
				{
					text = text.Replace("{{mall:", "{{");
					text = text.Replace("{{Mall:", "{{");
					text = text.Replace("{{template:", "{{");
					text = text.Replace("{{Template:", "{{");
				}
				text = text.Replace("[[kategori:", "[[Kategori:");
				text = text.Replace("[[Kategori: ", "[[Kategori:");
			
				if(text.IndexOf("{{Commonscat") == -1  && text.IndexOf("{{commonscat") == -1)
				{
					iw = p2.GetInterWikiLinks();
					int iwstart = text.IndexOf(iw[0]);
					int extl = text.IndexOf("Externa länkar");
					int plats = 0;
					if(extl == -1)
					{
						plats = text.LastIndexOf("\n", iwstart);
						text = text.Substring(0, plats)+"\n== Externa länkar ==\n{{Commonscat|"+ngt+"}}\n"+text.Substring(plats);
					}
					else
					{
						plats=text.IndexOf("\n", extl);
						text = text.Substring(0, plats)+"\n{{Commonscat|"+ngt+"}}\n"+text.Substring(plats);
					}
					//Console.WriteLine(text);
					
					Console.WriteLine(p2.title+" 2");
					p2.Save(p2.text, "Lägger in [[Mall:Commonscat]] enl [[WP:RH]]", true);
					Console.WriteLine(p2.title+" 3");
					Thread.Sleep(7000);
				}
			}
		}
	}
}