/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package me.diskstation.ammon.botfan;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import net.sourceforge.jwbf.core.actions.HttpActionClient;
import net.sourceforge.jwbf.core.contentRep.Article;

/**
 *
 * @author Marco Ammon <ammon.marco@t-online.de>
 */
public class StaticFan extends Botfan {

    private HttpActionClient client;

    public StaticFan() {
        super();
    }

    public StaticFan(HttpActionClient client) {
        super(client);
        this.client = client;
    }
    
    public static StaticFan forWiki(String wiki, int requestsPerMinute){
        StaticFan sf = new StaticFan(HttpActionClient.builder()
                .withUrl("http://wiki.teamliquid.net/" + wiki + "/") 
                .withUserAgent("Botfan", "0.1", "Clubfan") 
                .withRequestsPerUnit(requestsPerMinute, TimeUnit.MINUTES) 
                .build());
        sf.loginFromPrompt();
		//sf.login("username", "password");
        return sf;
    } 
    
    public void convertPageToStatic(String inputTitle, String outputTitle) {
        purgePage(inputTitle);
        String wikicode = getRenderedWikicodeFromPage(inputTitle);
        saveWikicodeToPage(outputTitle, wikicode, false);
    }

    protected String getRenderedWikicodeFromPage(String article) {
        //Prepend ":" as namespace identifier for main space
        if (!article.contains(":")) {
            article = ":" + article;
        }
        String wikicode = "{{" + article + "}}";
        ExpandWikicode ew = new ExpandWikicode(this, wikicode);
        return ew.getHtml();
    }

    protected void saveWikicodeToPage(String title, String content, boolean forceUpdate) {
        Article a = getArticle(title);
        //if (forceUpdate || !a.getText().equals(content)) {
		if (content.equals("\n")){
			content = "&nbsp;\n";
		}
        a.setText(content);
        a.setMinorEdit(true);
        a.save("Automatic conversion from dynamic page to static wikicode");
        
    }

    public static void main(String[] args) {
        String[] wikis = {"dota2", "counterstrike", "heroes", "starcraft2", "warcraft", "rocketleague", "overwatch"};
        for (String wiki : wikis) {
			System.out.println(wiki);
			System.out.println(new Date(System.currentTimeMillis()));
            StaticFan sf = StaticFan.forWiki(wiki, 10);
            String[] inputPages = {"Liquipedia:Upcoming and ongoing matches/dynamic", "Liquipedia:Upcoming and ongoing matches on mainpage/dynamic"};
            String[] outputPages = {"Liquipedia:Upcoming and ongoing matches", "Liquipedia:Upcoming and ongoing matches on mainpage"};
            for (int j = 0; j < inputPages.length; j++) {
                sf.convertPageToStatic(inputPages[j], outputPages[j]);
            }
        }
    }
}

@@ -1089,18 +1089,24 @@
 ompt();%0A
-%09%09
+        
 //sf.log
@@ -1331,32 +1331,138 @@
 ge(inputTitle);%0A
+        wikicode = wikicode.replaceAll(%22%5C%5C%5B%5C%5C%5BSMW::off%5C%5C%5D%5C%5C%5D%22, %22%22).replaceAll(%22%5C%5C%5B%5C%5C%5BSMW::on%5C%5C%5D%5C%5C%5D%22, %22%22);%0A
         saveWiki
@@ -2063,18 +2063,24 @@
 ent)) %7B%0A
-%09%09
+        
 if (cont
@@ -2098,19 +2098,28 @@
 %22%5Cn%22))%7B%0A
-%09%09%09
+            
 content 
@@ -2132,18 +2132,24 @@
 sp;%5Cn%22;%0A
-%09%09
+        
 %7D%0A      
@@ -2492,19 +2492,28 @@
 ikis) %7B%0A
-%09%09%09
+            
 System.o
@@ -2534,11 +2534,20 @@
 i);%0A
-%09%09%09
+            
 Syst

remove SMW internal output