/*
 * Copyright 2024 The Android Open Source Project
 *
 * 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
 *
 *     https://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.
 */

plugins {
    id 'com.android.library'
}

android {
    namespace 'com.google.unity.plugin'
    compileSdk 34

    defaultConfig {
        minSdk 22
        targetSdk 34

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
        versionCode 9
        versionName '1.2'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_17
        targetCompatibility JavaVersion.VERSION_17
    }
}

dependencies {

    implementation project(path: ':UnityActivity')
    compileOnly files('libs/classes.jar')
    implementation 'androidx.webkit:webkit:1.12.1'
    implementation 'androidx.appcompat:appcompat:1.7.0'
}

tasks.register('buildAndDeploy') {
    dependsOn ':Plugin:assembleDebug', ':Plugin:renameAAR'
    finalizedBy(':Plugin:copyAARDebug')
}

tasks.register('renameAAR') {
    dependsOn(':Plugin:bundleDebugAar')
    doLast {
        def outputFolder = [buildDir.absolutePath, 'outputs', 'aar'].join(File.separator)
        println("outputFolder " + outputFolder)

        def source = [outputFolder, "${project.name}-debug.aar"].join(File.separator)
        if (!file(source).exists())
            return

        def renamed = [outputFolder, "UnityANR-Plugin.aar"].join(File.separator)
        println("Renaming library " + source + " to " + renamed)
        ant.move(file: source, tofile: renamed)
    }
}

tasks.register('copyAARDebug') {
    dependsOn('renameAAR')

    doLast {

        def outputPath = [buildDir.absolutePath, 'outputs', 'aar','UnityANR-Plugin.aar'].join(File.separator)
        println(outputPath)

        copy {
            from outputPath
            into "..\\..\\unity-anr-project\\runtime\\Plugins\\Android\\"
        }
    }

}

afterEvaluate {
    assembleDebug.finalizedBy('copyAARDebug')
}
