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 static string[] RemoveDuplicates(string[] myList) {
        System.Collections.ArrayList newList = new System.Collections.ArrayList();

        foreach (string str in myList)
            if (!newList.Contains(str))
                newList.Add(str);
        return (string[])newList.ToArray(typeof(string));
    }

	public static void Main()
	{
		string user = "";
		string pass = "";
		string orgLI;
		string orgSV;
		bool changed;
		Site siteSV;
		Site siteLI;
//		Site siteEN;
		siteSV = new Site("http://sv.wikiquote.org", user, pass);
//		siteLI = new Site("http://li.wikiquote.org", user, pass);
		siteLI = new Site("http://en.wikiquote.org", user, pass);
		Page myPageLI = new Page(siteLI);
//		Page myPageEN = new Page(siteEN);
/*		
		string tmpStr;
		string tmpStr2;
*/		
		PageList plSV = new PageList(siteSV);
//		plSV.FillFromRecentChanges(false, false, false, false, true, 1000, 7);
//		plSV.FilterNamespaces(new int[] {14});
//plSV.FillFromCustomSpecialPage("Kategorier", 500);
//plSV.FillAllFromCategory("Topp");
plSV.FillAllFromCategoryTree("Teman");
plSV.RemoveRecurring();
		foreach(Page myPageSV in plSV)
		{
			if(myPageSV.title == "Kategori:Ordspråk")
				continue;
			changed = false;
			myPageSV.Load();
			string[] iwLinksSV = myPageSV.GetInterWikiLinks();
			foreach(string iwStr in iwLinksSV)
			{
				if(iwStr.StartsWith("en:"))
				{
					myPageLI.title = iwStr.Substring(3);
					myPageLI.Load();
					orgLI = myPageLI.text;
					orgSV = myPageSV.text;
					string[] iwLinksLI = myPageLI.GetInterWikiLinks();
					if(iwLinksLI.Length == 0)
					{
						int i2 = 0;
						Array.Resize<string>(ref iwLinksLI, iwLinksSV.Length);
						for(int i = 0; i < iwLinksSV.Length; i++)
						{
							if(!iwLinksSV[i].StartsWith("en:"))
								iwLinksLI[i2++] = iwLinksSV[i];
						}
						iwLinksLI[i2] = "sv:" + myPageSV.title;
						myPageLI.AddInterWikiLinks(iwLinksLI);
						changed = true;
					}
					else
					{
						//merge the lists
						string[] iwLinks = new string[iwLinksLI.Length + iwLinksSV.Length];
						iwLinksLI.CopyTo(iwLinks, 0);
						iwLinksSV.CopyTo(iwLinks, iwLinksLI.Length);
						//Add link to sv
						iwLinks[iwLinks.Length - 1] = "sv:" + myPageSV.title;
						//Remove duplicates
						iwLinks = RemoveDuplicates(iwLinks);

						//Remove own language version
						string[] newIWSV = new string[iwLinks.Length - 1];
						string[] newIWLI = new string[iwLinks.Length - 1];
						int i2 = 0;
						int i3 = 0;
						for(int i = 0; i < iwLinks.Length; i++)
						{
							if(!iwLinks[i].StartsWith("sv:") && !iwLinks[i].StartsWith("simple:"))
								newIWSV[i2++] = iwLinks[i];
							if(!iwLinks[i].StartsWith("en:") && !iwLinks[i].StartsWith("simple:"))
								newIWLI[i3++] = iwLinks[i];
						}
						myPageSV.RemoveInterWikiLinks();
						myPageLI.RemoveInterWikiLinks();
						myPageSV.AddInterWikiLinks(newIWSV);
						myPageLI.AddInterWikiLinks(newIWLI);
						myPageLI.SortInterWikiLinks();
						myPageSV.SortInterWikiLinks();

						if(myPageLI.text != orgLI || myPageSV.text != orgSV)
							changed = true;
						if(changed)
						{
							myPageLI.Save(myPageLI.text, "Changing interwikinks", true);
							myPageSV.Save(myPageSV.text, "Ändrar interwikilänkar", true);
						}

					}
				}
			}
			if(changed)
				Thread.Sleep(60000);
		}
	}
}