/*
 * Copyright 2007 Thomas Stock.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 * Contributors:
 *
 */
package net.sourceforge.jwbf.mediawiki;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.TimeZone;

import lombok.val;
import lombok.extern.slf4j.Slf4j;

import org.junit.internal.AssumptionViolatedException;
import org.mockito.Mockito;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Files;

/**
 * @author Thomas Stock
 */
@Slf4j
public class LiveTestFather {

  private final SimpleMap data;

  private static final Collection<String> specialChars = Lists.newArrayList();

  static {
    specialChars.add("\"");
    specialChars.add("\'");
    specialChars.add("?");
    specialChars.add("%");
    specialChars.add("&");
    specialChars.add("[");
    specialChars.add("]");
  }

  private LiveTestFather() {
    val noLiveTests = System.getProperty("noLiveTests", "false");
    val workWithDisk = !Boolean.valueOf(noLiveTests).booleanValue();
    if (workWithDisk) {
      data = new TestConfig();
    } else {
      data = mock(SimpleMap.class);
      when(data.get(Mockito.isA(String.class))) //
          .thenThrow(new AssumptionViolatedException("ignore this"));
    }
  }

  private static LiveTestFather instance;

  private static LiveTestFather get() {
    if (instance == null) {
      instance = new LiveTestFather();
    }
    return instance;
  }

  /**
   * 
   * @return the current UTC
   */
  public static Date getCurrentUTC() {
    val currentDate = System.currentTimeMillis();
    val tz = TimeZone.getDefault();
    val localCal = Calendar.getInstance(tz);
    localCal.setTimeInMillis(currentDate - tz.getOffset(currentDate));

    return new Date(localCal.getTimeInMillis());

  }

  public String getVal(String key) {
    val value = data.get(key);
    if (Strings.isNullOrEmpty(value)) {
      data.put(key, " ");
      log.warn("EMPTY value for " + key);
    }
    return data.get(key);
  }

  public static String getValue(final String key) {
    return get().getVal(key);
  }

  private static interface SimpleMap {
    String get(String key);

    String put(String key, String value);
  }

  private static class TestConfig implements SimpleMap {

    private final Properties properties;
    private final String filename;

    public TestConfig() {
      properties = new Properties();
      filename = findTestConfig();
      readOrCreate(filename);
    }

    private void write(String filename) {
      synchronized (this) {
        try {
          properties.storeToXML(new FileOutputStream(filename), "");
        } catch (FileNotFoundException e) {
          throw new IllegalStateException(e);
        } catch (IOException e) {
          throw new IllegalStateException(e);
        }
      }
    }

    private void readOrCreate(String filename) {
      synchronized (this) {
        try {
          properties.loadFromXML(new FileInputStream(filename));
        } catch (InvalidPropertiesFormatException e) {
          throw new IllegalStateException(e);
        } catch (FileNotFoundException e) {
          File f = new File(filename);
          try {
            Files.createParentDirs(f);
            f.createNewFile();
          } catch (IOException e1) {
            throw new IllegalStateException(e);
          }
        } catch (IOException e) {
          throw new IllegalStateException(e);
        }
      }
    }

    private String findTestConfig() {
      // find jwftestfile
      String filename = "";
      Collection<String> filepos = Lists.newArrayList();
      String jwbfDefaultTestConfig = System.getProperty("user.home") + "/.jwbf/test.xml";
      filepos.add(jwbfDefaultTestConfig);
      filepos.add(System.getProperty("user.home") + "/jwbftest.xml");
      filepos.add("test.xml");

      for (String fname : filepos) {
        if (new File(fname).canRead()) {
          filename = fname;
          log.info("use testfile: " + filename);

          break;
        }
      }
      if (filename.length() < 1) {
        log.warn("no testfile found. Use: " + jwbfDefaultTestConfig);
        filename = jwbfDefaultTestConfig;
      }
      return filename;
    }

    public String get(String key) {
      String value = properties.getProperty(key);
      if (!Strings.isNullOrEmpty(value)) {
        value = value.trim();
      }
      return value;
    }

    public String put(String key, String value) {
      val result = (String) properties.put(key, value);
      write(filename);
      return result;
    }

  }

  public static Collection<String> getSpecialChars() {
    return specialChars;
  }

}

@@ -1750,159 +1750,30 @@
     
-val noLiveTests = System.getProperty(%22noLiveTests%22, %22false%22);%0A    val workWithDisk = !Boolean.valueOf(noLiveTests).booleanValue();%0A    if (workWithDisk
+if (executeLiveTests()
 ) %7B%0A
@@ -1969,24 +1969,24 @@
 re this%22));%0A
-
     %7D%0A  %7D%0A%0A 
@@ -1980,24 +1980,537 @@
 %0A    %7D%0A  %7D%0A%0A
+  protected boolean executeLiveTests() %7B%0A    val workWithDisk = optSysProperty(%22noLiveTests%22, false);%0A    if (workWithDisk) %7B%0A      throw new IllegalArgumentException(%22do not uses this toggle - use withLiveTests%22);%0A    %7D%0A%0A    return optSysProperty(%22withLiveTests%22, false);%0A  %7D%0A%0A  private boolean optSysProperty(String name, boolean defaultValue) %7B%0A    val stringValue = System.getProperty(name, defaultValue + %22%22);%0A    val booleanValue = Boolean.valueOf(stringValue).booleanValue();%0A    return booleanValue;%0A  %7D%0A%0A
   private st

Change "noLiveTests" to "withLiveTests"