//Code written by Lsj to create articles about chemical elements for Cebuano wp

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 DotNetWikiBot;

class MyBot : Bot
{
	

    public static void Main()
	{
        Console.Write("Password: ");
        string password = Console.ReadLine();
        string botkonto = "Lsjbot";
        Site site = new Site("http://ceb.wikipedia.org", botkonto, password);
        Site ensite = new Site("http://en.wikipedia.org", botkonto, password);
        
        Dictionary<string,string> elname = new Dictionary<string,string>();
        Dictionary<string, string> altname = new Dictionary<string, string>();

        using (StreamReader sr = new StreamReader("C:\\DotNetWikiBot\\elements.csv"))
        {

            while (!sr.EndOfStream)
            {
                String line = sr.ReadLine();
                //Console.WriteLine(line);

                string[] words = line.Split(';');
                for (int jj = 1; jj < words.Length; jj++)
                {
                    words[jj] = words[jj].Trim();
                    if (words[jj].Length < 3)
                        words[jj] = "";
                }

                if ( words.Length > 2 )
                    elname.Add(words[1],words[2]);
                if ((words.Length > 3)&& (words[3].Length > 2))
                    altname.Add(words[1], words[3]);

            }
        }

        DateTime oldtime = DateTime.Now;



        foreach(String elen in elname.Keys)
        {
            DateTime nexttime = oldtime.AddSeconds(10);

            Page pen = new Page(ensite, elen);
            Page penbox = new Page(ensite, "Template:Infobox " + elen);
            Page p = new Page(site, elname[elen]);
            Page pbox = new Page(site, "Plantilya:Infobox " + elen);

            p.Load();
            if (p.Exists())
                continue;

            pen.Load();
            if (!pen.Exists())
                continue;
            if (!pen.text.Contains("nfobox " + elen))
                continue;

            penbox.Load();
            if (!penbox.Exists())
                continue;

            pbox.Load();
            if (!pbox.Exists())
            {
                pbox.text = penbox.text;
            }

            int z = -1;
            foreach (string s in penbox.GetTemplateParameter("Elementbox", "number"))
                z = Convert.ToInt32(s);


            string symbol = "Xx";
            foreach (string s in penbox.GetTemplateParameter("Elementbox", "symbol"))
                symbol = s;

            string series = "";
            foreach (string s in penbox.GetTemplateParameter("Elementbox", "series"))
                series = s;

            string phase = "";
            foreach (string s in penbox.GetTemplateParameter("Elementbox", "phase"))
                phase = s;

            string cebseries = "";
            switch (series)
            {
                case "metal":
                    cebseries = "puthaw";
                    break;
                case "other metal":
                    cebseries = "puthaw";
                    break;
                case "nonmetal":
                    cebseries = "wala puthaw";
                    break;
                case "transition metal":
                    cebseries = "puthaw nga transisyon";
                    break;
                case "actinide":
                    cebseries = "aktinido";
                    break;
                case "noble gas":
                    cebseries = "gas nga halangdon";
                    break;
                case "alkaline earth metal":
                    cebseries = "puthaw";
                    break;
                case "alkali metal":
                    cebseries = "puthaw";
                    break;
                case "lanthanide":
                    cebseries = "puthaw nga lantanido";
                    break;
                default:
                    cebseries = series;
                    break;

            }

            pbox.SetTemplateParameter("Elementbox", "series", cebseries, false);

            string cebphase = "";
            switch (phase)
            {
                case "gas":
                    cebphase = "gas";
                    break;
                case "solid":
                    cebphase = "magahi";
                    break;
                case "liquid":
                    cebphase = "likido";
                    break;

            }

            pbox.SetTemplateParameter("Elementbox", "phase", cebphase, false);


            p.text = "{{paghimo ni bot|Lsjbot|elemento|date=2012-12}}\n";
            p.text += "{{Infobox " + elen + "}}\n\n";

            p.text += "Ang '''" + elname[elen] + "''' ";

            if (altname.ContainsKey(elen))
            {
                p.text += "o '''" + altname[elen] + "''' ";
                Page predalt = new Page(site, elen);
                predalt.Load();
                if (!predalt.Exists())
                {
                    predalt.text = "#REDIRECT [[" + p.title + "]]";
                    predalt.Save();
                }
            }

            if (elen != elname[elen])
            {
                p.text += "([[Iningles]] ''" + elen + "'') ";
                Page preden = new Page(site, elen);
                preden.Load();
                if (!preden.Exists())
                {
                    preden.text = "#REDIRECT [[" + p.title + "]]";
                    preden.Save();
                }
            }

            p.text += "mao ang [[elementong kimiko]] sa [[talaang peryodiko]] nga may simbolo nga '''"+symbol+"''' ug kaiphan nga atomik "+z.ToString()+". ";

            p.text += "Ang " + elname[elen] + " mao ang [["+cebseries+"]].\n\n";

            p.text += "== Ang mga gi basihan niini ==\n\n<references/>\n\n";

            p.text += "[[Kategoriya:Mga elementong kimiko]]\n";
            p.text += "[[Kategoriya:"+cebseries+"]]\n\n";

            string[] iwen = new String[1] {"en:"+pen.title};
            p.AddInterWikiLinks(pen.GetInterWikiLinks());
            p.AddInterWikiLinks(iwen);

            pbox.Save();

            p.Save();

            
            Page predsym = new Page(site, symbol.ToUpper());
            predsym.Load();
            if (!predsym.Exists())
            {
                predsym.text = "Ang '''"+symbol.ToUpper()+"''' puyde mopasabot sa:\n\n* Ang elementong kimiko '''[[" + p.title + "]]'''\n\n{{giklaro}}";
                predsym.Save();
            }
            Page predsym2 = new Page(site, symbol);
            predsym2.Load();
            if (!predsym2.Exists())
            {
                predsym2.text = "#REDIRECT [[" + predsym.title + "]]"; 
                predsym2.Save();
            }

            while (DateTime.Now.CompareTo(nexttime) < 0)
                continue;
            oldtime = DateTime.Now;


            //Console.WriteLine("<return>");
            //Console.ReadLine();
            
        }
        

    }
}