using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Threading;
using System.Net;
using System.Web;
using DotNetWikiBot;

public class TableRow
{
        public string cell1;
        public string cell2;
        
        public TableRow(string c1, string c2)
        {
                cell1 = c1;
                cell2 = c2;
        }
}

class MyBot : Bot
{
		public static List<string> item(Site site, string prop, string objekt)
		{
				List<string> r = new List<string>();
                
				string url = "action=wbgetentities&ids=q"+objekt+"&format=xml";
				
                XmlDocument doc = new XmlDocument();
                        
				string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
				File.WriteAllText("huvaorg.xml", tmpStr);
				int sn = tmpStr.IndexOf("<property id=\"p"+prop+"\">");
				if(sn != -1)
				{
					string s1 = tmpStr.Substring(sn);
					string s0 = s1;
					int sn2 = s1.IndexOf("<property id=\"p",5);
					int sn3 = s1.IndexOf("</property>")+11;
					if(sn2 != -1 && sn2 < sn3)
					{
						string s3 = s1.Substring(sn2, sn3-sn2);
						string s4 = s3.Replace("value entity-type=", "xalue entity-type=");
						//Console.WriteLine(s4);
						s1 = s1.Replace(s3, s4);
					}
					s1 = s1.Substring(0,s1.IndexOf("</property>")+11);
					s0 = s0.Substring(0,s0.IndexOf("</property>")+11);
					
					string s2 = s1.Replace("value entity-type=", "walue entity-type=");
					
					tmpStr = tmpStr.Replace(s0, s2);
					//File.WriteAllText("huvajobb.xml", tmpStr);
					//File.WriteAllText("huvas0.txt", s0);
					//File.WriteAllText("huvas1.txt", s1);
					//File.WriteAllText("huvas2.txt", s2);
					doc.LoadXml(tmpStr);
					for(int i = 0 ;i< doc.GetElementsByTagName("walue").Count ;i++)
					{
						string title = doc.GetElementsByTagName("walue")[i].Attributes.GetNamedItem("numeric-id").Value;
						r.Add(title);
					}
				}
				return r;
		}
		public static List<string> stringvalue(Site site, string item, string prop)
		{
				List<string> r = new List<string>();
                
				string url = "action=wbgetentities&ids=q"+item+"&format=xml";
				
                XmlDocument doc = new XmlDocument();
                        
				string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
				int sn = tmpStr.IndexOf("<property id=\"p"+prop+"\">");
				if(sn != -1)
				{
					string s1 = tmpStr.Substring(sn);
					s1 = s1.Substring(0,s1.IndexOf("</property>"));
					string s2 = s1.Replace("datavalue value=", "eatavalue value=");
					tmpStr = tmpStr.Replace(s1, s2);
					doc.LoadXml(tmpStr);
					for(int i = 0 ;i< doc.GetElementsByTagName("eatavalue").Count ;i++)
					{
						string title = doc.GetElementsByTagName("eatavalue")[i].Attributes.GetNamedItem("value").Value;
						r.Add(title);
					}
				}
				return r;
		}
		public static string label(Site site, string q)
		{
				string r = "";
                
				string url = "action=wbgetentities&ids=q"+q+"&format=xml";
				
                XmlDocument doc = new XmlDocument();
                        
				string tmpStr = site.PostDataAndGetResultHTM(site.site+"/w/api.php", url);
				doc.LoadXml(tmpStr);
				for(int i = 0 ;i< doc.GetElementsByTagName("label").Count ;i++)
				{
						string value = doc.GetElementsByTagName("label")[i].Attributes.GetNamedItem("value").Value;
						string language = doc.GetElementsByTagName("label")[i].Attributes.GetNamedItem("language").Value;
						if (language == "sv")
							r = value;
				}
				return r;
		}
		public static string sitelink(Site sitewd, string q)
		{
				string r = "";
                
				string url = "action=wbgetentities&ids=q"+q+"&format=xml";
				
                XmlDocument doc = new XmlDocument();
                        
				string tmpStr = sitewd.PostDataAndGetResultHTM(sitewd.site+"/w/api.php", url);
				doc.LoadXml(tmpStr);
				for(int i = 0 ;i< doc.GetElementsByTagName("sitelink").Count ;i++)
				{
						string title = doc.GetElementsByTagName("sitelink")[i].Attributes.GetNamedItem("title").Value;
						string site = doc.GetElementsByTagName("sitelink")[i].Attributes.GetNamedItem("site").Value;
						if (site == "svwiki")
							r = title;
				}
				return r;
		}


	
	public static void FN()
	{
		Site site = new Site("http://sv.wikipedia.org", "Innocent bot", "********");
		Site sitewd = new Site("http://wikidata.org", "Innocent bot", "********");
		
		List<string> t = new List<string>();
		List<TableRow> n = new List<TableRow>();
		t = item(sitewd, "100", "1065");
		int i = 0;
		foreach(string t1 in t)
		{
			string t2 = label(sitewd, t1);
			n.Add(new TableRow(t2, t1));
		}
		
		n.Sort(delegate(TableRow r1, TableRow r2)
			{
				return r1.cell1.CompareTo(r2.cell1);
			}
		);
		string sida = "Detta är en lista över alla medlemmar i FN. Listan genereras utifrån objekt på Wikidata. För medlemmarna se [[d:q1065|FN på Wikidata]]. Om en flagga saknas, finns den alltså inte inlagd på Wikidata. Om en 'svensk etikett saknas' så finns inget inskrivet i rubriken på det objektet på Wikidata. Om Wikidata länkas istället för en artikel på svwp, så har det objektet ingen länk hit. Om ingen stats-/regeringschef finns angiven för landet, så kan inte heller något skrivas ut här.\n"+
			"{| class=\"wikitable\"\n"+
			"|-bgcolor=\"#cccccc\"\n"+
			"![[Världens länder|Stat]]\n"+
			"![[Statschef]]\n"+
			"![[Regeringschef]]\n"+
			"|-\n";

		foreach(TableRow n1 in n)
		{
			Console.WriteLine(n1.cell1);
			string namn = n1.cell1;
			string link = sitelink(sitewd, n1.cell2);
			string text = "";
			if (link == namn)
				text = "[["+link+"]]";
			else
				text = "[["+link+"|"+namn+"]]";
			
			List<string> flag = new List<string>();
			flag = stringvalue(sitewd, n1.cell2, "41");
			string f = "";
			foreach(string f2 in flag)
			{
				f += "[[Fil:"+f2+"|22x20px]]";
			}
			if(flag.Count == 0)
				f += "[[d:q"+n1.cell2+"|INGEN FIL INLAGD]]";
			sida += "| "+f+" "+text+"\n";
			
			Console.WriteLine(f+" "+text);
			List<string> statschef = new List<string>();
			List<string> regchef = new List<string>();
			statschef = item(sitewd, "35", n1.cell2);
			regchef = item(sitewd, "46", n1.cell2);
			string schef = "";
			foreach(string stats in statschef)
			{
				string link2 = sitelink(sitewd, stats);
				string lab2 = label(sitewd, stats);
				if(lab2 == "")
					lab2 = "SAKNAR SVENSK ETIKETT!";
				if (link2 == "")
					link2 = "d:q"+stats;
				if (link2 == lab2)
					schef += "[["+link2+"]]";
				else
					schef += "[["+link2+"|"+lab2+"]]";
			}
			string rchef = "";
			foreach(string stats in regchef)
			{
				string link2 = sitelink(sitewd, stats);
				string lab2 = label(sitewd, stats);
				if(lab2 == "")
					lab2 = "SAKNAR SVENSK ETIKETT!";
				if (link2 == "")
					link2 = "d:q"+stats;
				if (link2 == lab2)
					rchef += "[["+link2+"]]";
				else
					rchef += "[["+link2+"|"+lab2+"]]";
			}
			Console.WriteLine(schef);
			Console.WriteLine(rchef);
			string rschef = "| ";
			if(rchef == "" && schef == "")
				rschef += " colspan=\"2\" align=center | [[:d:q"+n1.cell2+"|Inget inlagt i stats-/regeringschef]]\n";
			else
			{
				if(schef == "")
					rschef += " [[:d:q"+n1.cell2+"|Inget inlagt i statschef]]\n|"+rchef+"\n";
				else
				{
					if(rchef == "")
						rschef += schef + "\n| [[:d:q"+n1.cell2+"|Inget inlagt i regeringschef]]\n";
					else
					{
						if(schef == rchef)
							rschef += " colspan=\"2\" align=center | "+rchef+"\n";
						else
							rschef += schef+"\n| "+rchef+"\n";
					}
				}
			}
		
			
			rschef = rschef.Replace("]][[", "]]<br/>[[");
			sida += rschef;
			sida += "|-\n";
		}
		sida += "|}\n";
		Page p = new Page(site, "Användare:Innocent bot/FN");
		p.Load();
		p.Save(sida, "test", false);
	}
	public static void Main()
	{
		FN();
		/*

		List<string> statschef = new List<string>();
		statschef = item(sitewd, "35", "40");//40 Österrike 222 Albanien
		foreach(string chef in statschef)
			Console.WriteLine(chef);
		*/
	}
	
}