import java.lang.reflect.Method

import sbt._
import sbt.Keys._
import sbt.testing.Logger

////********************************
//// Common settings across XXXXXXXX
////********************************
object Common {

    val consoleLog = new Logger {
        def ansiCodesSupported = false
        def error(message: String) = println("error: " + message)
        def info(message: String)  = println("info: " + message)
        def warn(message: String)  = println("warn: " + message)
        def debug(message: String) = println("debug: " + message)
        def trace(t: Throwable)    = println("trace: " + t)
    }

    val settings = Seq(
        organization := "XXXXXXXX",
        version := "1.0.4",
        scalaVersion := "2.11.7",
        scalacOptions ++= Seq(
            "-deprecation", // Emit warning and location for usages of deprecated APIs.
            "-feature", // Emit warning and location for usages of features that should be imported explicitly.
            "-unchecked", // Enable additional warnings where generated code depends on assumptions.
            "-language:_",
            "-target:jvm-1.8",
            "-encoding", "UTF-8",
            //"-Ymacro-debug-lite", // Print macro
            "-Xfatal-warnings", // Fail the compilation if there are any warnings.
            "-Xlint", // Enable recommended additional warnings.
            "-Ywarn-adapted-args", // Warn if an argument list is modified to match the receiver.
            "-Ywarn-dead-code", // Warn when dead code is identified.
            "-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
            "-Ywarn-nullary-override", // Warn when non-nullary overrides nullary, e.g. def foo() over def foo.
            "-Ywarn-numeric-widen" // Warn when numerics are widened.
        ),
        resolvers ++= Seq(Resolver.sonatypeRepo, Resolver.sonatypeSnapshotRepo, Resolver.scalazRepo),
        unmanagedResourceDirectories in Test <+=  baseDirectory ( _ /"target/web/public/test" ),
        testListeners in Test := Seq(new TestLogger(new TestLogging(consoleLog, {_ => new ContentLogger(consoleLog, () => {}) })) {
            override def doComplete(finalResult: TestResult.Value): Unit = {
                super.doComplete(finalResult)
                consoleLog.info("The test has been completed")
            }
        }),
        testOptions in Test += Tests.Cleanup( (loader: java.lang.ClassLoader) => {
            val clazz = loader.loadClass("common.test.CassandraDatabase$")
            val instance = clazz.getField("MODULE$").get(null)
            clazz.getMethod("shutdown").invoke(instance)
        } ),
        fork in test := true,
        fork in testOnly := true,
        javaOptions in Test ++= Option(System.getProperty("config.resource")).map("-Dconfig.resource=" + _).toSeq,
        javaOptions in Test ++= Option(System.getProperty("office.home")).map("-Doffice.home=" + _).toSeq
    )
}