/*
 * Copyright (C) 2018 Florian Dreier
 *
 * This file is part of MyTargets.
 *
 * MyTargets is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * MyTargets 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.
 */

buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'org.apache.commons:commons-io:1.3.2'
    }
}

import org.apache.commons.io.FileUtils

if (hasProperty('CROWDIN_API_KEY')) {

    crowdinUpload {
        apiKey = CROWDIN_API_KEY
        projectId = 'mytargets'
        branch = gitBranchName
        files = [
                [
                        title      : 'App',
                        name       : 'strings.xml',
                        source     : 'app/src/main/res/values/strings.xml',
                        translation: 'app/src/main/res/values-%android_code%/strings.xml'
                ],
                [
                        title      : 'Shared',
                        name       : 'strings-shared.xml',
                        source     : 'shared/src/main/res/values/strings.xml',
                        translation: 'shared/src/main/res/values-%android_code%/strings.xml'
                ],
                [
                        title      : 'Standard rounds',
                        name       : 'rounds.xml',
                        source     : 'shared/src/main/res/values/rounds.xml',
                        translation: 'shared/src/main/res/values-%android_code%/rounds.xml'
                ],
                [
                        title      : 'Target faces',
                        name       : 'target_faces.xml',
                        source     : 'shared/src/main/res/values/target_faces.xml',
                        translation: 'shared/src/main/res/values-%android_code%/target_faces.xml'
                ],
                [
                        title      : 'Wearable',
                        name       : 'strings-wearable.xml',
                        source     : 'wearable/src/main/res/values/strings.xml',
                        translation: 'wearable/src/main/res/values-%android_code%/strings.xml'
                ],
                [
                        title      : 'Help',
                        name       : 'help.html',
                        source     : 'app/src/main/res/raw/help.html',
                        translation: 'app/src/main/res/raw-%android_code%/help.html'
                ],
                [
                        title      : 'Play Store Title',
                        name       : 'title.txt',
                        source     : 'app/src/regular/play/en-US/listing/title',
                        translation: 'app/src/regular/play/%locale%/listing/title'
                ],
                [
                        title      : 'Play Store Short Description',
                        name       : 'short_description.txt',
                        source     : 'app/src/regular/play/en-US/listing/shortdescription',
                        translation: 'app/src/regular/play/%locale%/listing/shortdescription'
                ],
                [
                        title      : 'Play Store Full Description',
                        name       : 'full_description.txt',
                        source     : 'app/src/regular/play/en-US/listing/fulldescription',
                        translation: 'app/src/regular/play/%locale%/listing/fulldescription'
                ]
        ]
    }

    crowdinDownload {
        apiKey = CROWDIN_API_KEY
        projectId = 'mytargets'
        branch = gitBranchName
        destination = "$projectDir"
        renameMapping = [
                from: '^((?:(?!values).)*)(?:(values-)((?!(pt|zh))(.*)-r(.*)|((pt|zh).*)))?(/(?:(?!values).)*)$',
                to  : /\1\2\5\7\9/
        ]
        doLast {
            FileUtils.copyDirectory(file('app/src/regular/play/ca-ES'), file('app/src/regular/play/ca'))
            FileUtils.copyDirectory(file('app/src/regular/play/et-EE'), file('app/src/regular/play/et'))
            FileUtils.copyDirectory(file('app/src/regular/play/sk-SK'), file('app/src/regular/play/sk'))
            FileUtils.copyDirectory(file('app/src/regular/play/sl-SI'), file('app/src/regular/play/sl'))
            file('app/src/regular/play/af-ZA').deleteDir()
            file('app/src/regular/play/ar-SA').deleteDir()
            file('app/src/regular/play/ca-ES').deleteDir()
            file('app/src/regular/play/el-GR').deleteDir()
            file('app/src/regular/play/es-AR').deleteDir()
            file('app/src/regular/play/et-EE').deleteDir()
            file('app/src/regular/play/fi-FI').deleteDir()
            file('app/src/regular/play/he-IL').deleteDir()
            file('app/src/regular/play/id-ID').deleteDir()
            file('app/src/regular/play/ko-KR').deleteDir()
            file('app/src/regular/play/ro-RO').deleteDir()
            file('app/src/regular/play/sk-SK').deleteDir()
            file('app/src/regular/play/sl-SI').deleteDir()
            file('app/src/regular/play/sr-SP').deleteDir()
            file('app/src/regular/play/uk-UA').deleteDir()
            file('app/src/regular/play/vi-VN').deleteDir()
        }
    }.dependsOn crowdinUpload

    allprojects {
        tasks.whenTaskAdded { task ->
            if (task.name.matches('.*assemble.*Release')) {
                task.dependsOn crowdinDownload
            }
        }
    }
}
