using System;
using System.Text.RegularExpressions;
using System.Threading;

public class AutoReport
{
    private const string milHistTemplates = "MILHIST|WPMH|WPMIL|Military *history|WikiProject *Military|WikiProject *War";
    private const string coordinators = "Wikipedia talk:WikiProject Military history/Coordinators";

    private bool isBclass (Bot bot, string title)
    {   
        var talk = new Page (bot, title);
        talk.Load ();
   
        Template template = talk.Find (milHistTemplates);
        if (null != template)
        {                   
            return template.Exists ("^class$", "^B$");
        }
        return false;
    }

    private bool assessedBClass (Bot bot, Contribution contribution)
    {
//      Console.WriteLine (contribution.Title + " - " + contribution.Comment);
        bool assessed = false;
        if (Regex.IsMatch (contribution.Comment, "Automatic MILHIST checklist assessment"))
        {
            if (Regex.IsMatch (contribution.Comment, "B class"))
            {
                assessed = true;
            }
            else
            {
                assessed = isBclass (bot, contribution.Title);
            }   
        }
//      Console.WriteLine ("assessed " + (assessed ? "true" : "false"));
        return assessed;       
    }

    private AutoReport ()
    {
        Bot bot         = null;
        int exitStatus  = 0;
        const int batchSize = 50;
        DateTime t      = DateTime.Now;
        string lastDay  = t.PreviousMonthLastDay().Timestamp();
        string firstDay = t.PreviousMonthFirstDay().Timestamp();
        Query query     = new Query ("MilHistBot", batchSize, lastDay, firstDay);
        string summary  =" AutoCheck report for " + t.Month();
       
        try
        {
            bot = new Bot ();
            Page page = new Page (bot, coordinators);
            Section section = new Section (page, summary);
            while (true)
            {           
                var contributions = bot.Contributions (query);
                foreach (Contribution contribution in contributions)
                {
                    if (assessedBClass (bot, contribution))
                    {
                        Console.WriteLine(contribution.Title);
                        section.Add ("* [[" + contribution.Title + "]]\n");
                    }
                }
                if (contributions.Count < batchSize)
                {
                    break;
                }                      
            }
            section.Add ("~~~~\n");
            section.Save (summary);
        }
        catch (BotException bex)
        {
            Console.WriteLine (bex.Message);
            exitStatus = 1;
        }
        finally
        {
            if (null != bot)
            {
                bot.Close ();
            }
        }
        Environment.Exit (exitStatus);   
    }

    static public void Main (string [] args)
    {       
        var autoReport = new AutoReport ();                              
    }
}