pipeline {
    agent any
    parameters {
        string(
            name: 'GIT_COMMIT',
            defaultValue: '',
            description: 'Git commit')
        choice(
            name: 'BRAND',
            choices: ['orchard', 'awal', 'sme'],
            description: 'Brand')
    }
    environment {
        shared = null
        config = null
    }
    stages {
        stage('Initialize') {
            steps {
                script {
                    checkout([
                        $class: 'GitSCM',
                        branches: [[name: params.GIT_COMMIT]],
                        doGenerateSubmoduleConfigurations: false,
                        extensions: [],
                        submoduleCfg: [],
                        userRemoteConfigs: [[
                            credentialsId: '577cbc72-7d9e-4eba-924c-ecbbe6de9805',
                            url: 'git@github.com:theorchard/orchardgo.git'
                        ]]
                    ])
                    shared = load './jenkins/shared.groovy'
                    config = load './jenkins/config.groovy'
                    library identifier: shared.library(), changelog: false
                    shared.initGitSha(params.GIT_COMMIT)
                    shared.notifySlackStarted()
                }
            }
        }
        stage('Binaries') {
            parallel {
                stage('Android') {
                    steps {
                        script {
                            shared.runJobWithRetry(shared.jobBinary(), [
                                shared.gitCommit(),
                                hostAndroid(),
                                shared.platformAndroid(),
                                brand(),
                                shared.environmentProd(),
                                shared.storeS3(),
                                shared.androidBinaryTypeApk(),
                                awsBinariesNamePrefix(),
                                shared.prodCodePushDisabled()
                            ])
                        }
                    }
                }
                stage('iOS') {
                    steps {
                        script {
                            shared.runJobWithRetry(shared.jobBinary(), [
                                shared.gitCommit(),
                                hostIos(),
                                shared.platformIos(),
                                brand(),
                                shared.environmentProd(),
                                shared.storeS3(),
                                shared.androidBinaryTypeApk(),
                                awsBinariesNamePrefix(),
                                shared.prodCodePushDisabled()
                            ])
                        }
                    }
                }
            }
        }
    }
    post {
        always {
            script {
                if (currentBuild.result == null) {
                    currentBuild.result = 'ABORTED'
                }
            }
        }
        success {
            script {
                shared.notifySlackFinished()
            }
        }
        failure {
            script {
                currentBuild.result = 'FAILURE'
                shared.notifySlackFinished()
            }
        }
        aborted {
            script {
                shared.notifySlackFinished()
            }
        }
    }
}

def brand() {
    return shared.brand(params.BRAND)
}

def awsBinariesNamePrefix() {
    return shared.awsBinariesNamePrefix(params.GIT_COMMIT)
}

def hostAndroid() {
    return shared.host(config.host.android)
}

def hostIos() {
    return shared.host(config.host.ios)
}
