/*
 * Copyright 2021 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.
 */

import org.gradle.internal.os.OperatingSystem;

apply plugin: 'com.android.application'

// See README for details on enabling PAD
def usePad = PADEnabled

// Define a path to the extracted Play Core SDK files.
// If using a relative path, wrap it with file() since CMake requires absolute paths.
def playcoreDir = file('libs/play-core-native-sdk')

android {
    compileSdkVersion 36
    ndkVersion '27.2.12479018'

    defaultConfig {
        applicationId 'com.google.sample.agdktunnel'
        minSdkVersion 24
        targetSdkVersion 36
        versionCode     125
        versionName    '1.2.5'

        externalNativeBuild {
            cmake {
                // Define the PLAYCORE_LOCATION directive.
                arguments "-DANDROID_STL=c++_shared",
                        "-DPLAYCORE_LOCATION=$playcoreDir",
                        "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
            }
        }
        ndk {
            abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
        }
    }
    buildTypes {
        release {
            minifyEnabled = false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro',
                    '$playcoreDir/proguard/common.pgcfg',
                    '$playcoreDir/proguard/per-feature-proguard-files'
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_STL=c++_shared",
                              "-DUSE_ASSET_PACKS=$usePad",
                              "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
                }
            }
        }
        debug {
            externalNativeBuild {
                minifyEnabled = false
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                        'proguard-rules.pro'
                cmake {
                    arguments "-DANDROID_STL=c++_shared",
                              "-DUSE_ASSET_PACKS=$usePad",
                              "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
                }
            }
            multiDexEnabled true
        }
    }
    externalNativeBuild {
        cmake {
            path 'src/main/cpp/CMakeLists.txt'
        }
    }
    buildFeatures {
        prefab true
        shaders true
    }
    assetPacks = [":install_time_assets", ":on_demand_assets"]
    // Enable the splitting of your asset packs per textures
    bundle {
        texture {
            enableSplit true
        }
    }
    namespace 'com.google.sample.agdktunnel'
    lint {
        abortOnError false
    }

    androidComponents {
        onVariants(selector().all()) { appVariant ->
            appVariant.sources.java.addStaticSourceDirectory("../../common/base_game_framework/java")
        }
    }
}

dependencies {
    implementation "androidx.lifecycle:lifecycle-viewmodel:2.2.0"
    implementation "androidx.lifecycle:lifecycle-livedata:2.2.0"
    implementation "androidx.lifecycle:lifecycle-runtime:2.2.0"
    implementation "androidx.core:core:1.5.0"
    implementation "androidx.constraintlayout:constraintlayout:2.0.4"
    implementation 'androidx.fragment:fragment:1.2.5'
    implementation 'com.google.oboe:oboe:1.5.0'
    implementation "androidx.games:games-frame-pacing:2.1.2"
    implementation "androidx.games:games-activity:3.0.5"
    implementation "androidx.games:games-controller:2.0.2"

    // Dependency for using APIs wrapped using library wrapper
    implementation 'com.google.android.gms:play-services-gni-native-c:1.0.0-beta2'

    // Google Play Games dependencies
    implementation "com.google.android.gms:play-services-games-v2:17.0.0"
    // Use the Play Core AAR included with the SDK.
    if(PADEnabled.toBoolean()) {
        implementation files("$playcoreDir/playcore.aar")
    }
    // Google Play Games for PC Input SDK
    implementation 'com.google.android.libraries.play.games:inputmapping:1.1.0-beta'

    // Example of using local .aar files in libs/ directory.
    // Comment out the androidx.games implementation lines if
    // you use a local lib.
    //implementation fileTree(dir: 'libs', include: ['GameActivity.aar',
    //                                               'GameController.aar',
    //                                               'games-frame-pacing-1.9.0.aar',
    //                                               'games-performance-tuner-1.5.0.aar'])
}


task copyTexturesToAssets(type: Copy) {
    def assetsDir = new File(projectDir, 'src/main/assets/no_asset_packs_textures')
    from (rootProject.file('install_time_assets/src/main/assets/textures')) {
        include('*.ktx')
    }
    into assetsDir
    from (rootProject.file('on_demand_assets/src/main/assets/textures')) {
        include('*.ktx')
    }
    into assetsDir
}

task deleteAssets(type: Delete) {
    def assetsDir = new File(projectDir, 'src/main/assets/no_asset_packs_textures')
    delete assetsDir
}

tasks.whenTaskAdded { task ->
    if (!PADEnabled.toBoolean()) {
        // Copy the ETC2 textures into directory
        // src/main/assets/no_asset_packs_textures/
        task.dependsOn copyTexturesToAssets
    } else {
        // Delete the src/main/assets/no_asset_packs_textures if using asset packs
        task.dependsOn deleteAssets
    }
}