import groovy.json.JsonSlurper
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent

def packageReactNativeVersion = new JsonSlurper()
    .parse(file("$rootDir/../package.json"))
    .devDependencies["react-native"]

// When built standalone (without RN plugin), we need an explicit version
def reactNativeVersion = rootProject.hasProperty('reactNativeVersion') 
    ? rootProject.reactNativeVersion 
    : packageReactNativeVersion

buildscript {
  ext.LocalServer = [
    kotlinVersion: "2.0.21",
    minSdkVersion: 24,
    compileSdkVersion: 36,
    targetSdkVersion: 36
  ]

  ext.getExtOrDefault = { prop ->
    if (rootProject.ext.has(prop)) {
      return rootProject.ext.get(prop)
    }

    return LocalServer[prop]
  }

  repositories {
    google()
    mavenCentral()
  }

  dependencies {
    classpath "com.android.tools.build:gradle:8.7.2"
    // noinspection DifferentKotlinGradleVersion
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('kotlinVersion')}"
  }
}


apply plugin: "com.android.library"
apply plugin: "kotlin-android"

// Only apply React Native plugin when available (not available in standalone test builds)
try {
    apply plugin: "com.facebook.react"
} catch (Exception e) {
    logger.warn("com.facebook.react plugin not available - this is expected for standalone test builds")
}

// Repositories for project dependencies (when built standalone)
repositories {
    google()
    mavenCentral()
    maven { url "$rootDir/../node_modules/react-native/android" }
}

android {
    namespace "com.reactnativelocalserver"
    compileSdkVersion getExtOrDefault("compileSdkVersion")
    defaultConfig {
        minSdkVersion getExtOrDefault("minSdkVersion")
        targetSdkVersion getExtOrDefault("targetSdkVersion")
    }

    buildFeatures {
        buildConfig = true
    }

    buildTypes {
        release {
            minifyEnabled false
        }
    }

    lint {
        disable "GradleCompatible"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation "com.facebook.react:react-android:$reactNativeVersion"
    implementation "org.jmdns:jmdns:3.6.0"

    testImplementation "junit:junit:4.12"
    testImplementation "org.assertj:assertj-core:3.11.1"
    testImplementation 'org.mockito:mockito-core:2.19.0'
    testImplementation "androidx.test:core:1.3.0"
}

// Enhance test commands
tasks.withType(Test) {
    // force tests to run every time
    outputs.upToDateWhen {
        false
    }
    // logging output
    testLogging {
        // set options for log level LIFECYCLE
        events TestLogEvent.FAILED,
            TestLogEvent.PASSED,
            TestLogEvent.SKIPPED,
            TestLogEvent.STANDARD_OUT
        exceptionFormat TestExceptionFormat.FULL
        showExceptions true
        showCauses true
        showStackTraces true

        // set options for log level DEBUG and INFO
        debug {
            events TestLogEvent.STARTED,
                TestLogEvent.FAILED,
                TestLogEvent.PASSED,
                TestLogEvent.SKIPPED,
                TestLogEvent.STANDARD_ERROR,
                TestLogEvent.STANDARD_OUT
            exceptionFormat TestExceptionFormat.FULL
        }
        info.events = debug.events
        info.exceptionFormat = debug.exceptionFormat

        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
                def startItem = '|  ', endItem = '  |'
                def repeatLength = startItem.length() + output.length() + endItem.length()
                println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
            }
        }
    }
}
