/*
 * Copyright (C) 2016-2019 Álinson Santos Xavier <isoron@gmail.com>
 *
 * This file is part of Loop Habit Tracker.
 *
 * Loop Habit Tracker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * Loop Habit Tracker is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

plugins {
    id 'kotlin-multiplatform' version "1.3.72"
}

repositories {
    mavenCentral()
}

kotlin {
    targets {
        def sdkName = System.getenv('SDK_NAME')
        def isIphone = sdkName?.startsWith("iphoneos")
        def iosPreset = isIphone ? presets.iosArm64 : presets.iosX64

        fromPreset(iosPreset, 'ios') {
            binaries {
                framework {
                    baseName = "LoopHabitTracker"
                }
            }
            compilations.main {
                cinterops {
                    sqlite3 {
                        defFile project.file("src/main/c_interop/sqlite3.def")
                    }
                }
            }
        }

        fromPreset(presets.jvm, 'jvm') {

        }

        fromPreset(presets.js, 'js') {
            compilations.main.kotlinOptions {
                moduleKind = "commonjs"
            }
            compilations.test.kotlinOptions {
                moduleKind = "commonjs"
            }
        }
    }

    sourceSets {
        commonMain {
            kotlin { srcDir "src/main/common" }
            dependencies {
                implementation kotlin('stdlib-common')
                implementation "$KX_COROUTINES-core-common:$KX_COROUTINES_VERSION"
            }
        }

        commonTest {
            kotlin { srcDir "src/test/common" }
            dependencies {
                implementation kotlin('test-common')
                implementation kotlin('test-annotations-common')
            }
        }

        jvmMain {
            kotlin { srcDir "src/main/jvm" }
            dependencies {
                implementation kotlin('stdlib-jdk8')
                implementation "$KX_COROUTINES-core:$KX_COROUTINES_VERSION"
            }
        }

        jvmTest {
            kotlin { srcDir "src/test/jvm" }
            dependencies {
                implementation kotlin('test')
                implementation kotlin('test-junit')
                implementation 'org.xerial:sqlite-jdbc:3.25.2'
            }
        }

        jsMain {
            kotlin { srcDir "src/main/js" }
            dependencies {
                implementation kotlin('stdlib-js')
                implementation "$KX_COROUTINES-core-js:$KX_COROUTINES_VERSION"
            }
        }

        jsTest {
            kotlin { srcDir "src/test/js" }
            dependencies {
                implementation kotlin('test-js')
            }
        }

        iosMain {
            kotlin { srcDir "src/main/ios" }
            dependencies {
                implementation "$KX_COROUTINES-core-native:$KX_COROUTINES_VERSION"
            }
        }

        iosTest {
            kotlin { srcDir "src/test/ios" }
            dependencies {
                implementation "$KX_COROUTINES-core-native:$KX_COROUTINES_VERSION"
            }
        }
    }

    task iosTestCopyResources(type: Copy) {
        from 'assets/test/'
        from 'assets/main/'
        into 'build/bin/ios/debugTest'
    }

    if (project.tasks.findByName('iosTest')) {
        iosTest.dependsOn(iosTestCopyResources)
    }
}
