pipeline {
    agent any
    parameters {
        string(
            name: 'GIT_COMMIT',
            defaultValue: '',
            description: 'Git commit')
        string(
            name: 'AWS_BINARIES_NAME_PREFIX',
            defaultValue: 'branchE2E',
            description: 'AWS binaries name prefix')
    }
    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('OrchardGo') {
            parallel {
                stage('iOS') {
                    stages {
                        stage('iOS QA for e2e') {
                            steps {
                                script {
                                    shared.runJobWithRetry(shared.jobBinary(), [
                                        shared.gitCommit(),
                                        hostIos(),
                                        shared.platformIos(),
                                        shared.brandOrchard(),
                                        shared.environmentQa(),
                                        shared.storeS3(),
                                        shared.androidBinaryTypeApk(),
                                        awsBinariesNamePrefix()
                                    ])
                                }
                            }
                        }
                        stage('iOS e2e') {
                            steps {
                                script {
                                    shared.runJobWithRetry(shared.jobE2E(), [
                                        shared.e2eBrandOrchard(),
                                        shared.e2eOsIos(),
                                        shared.e2eOsIosVer(),
                                        shared.e2eDeviceIos(),
                                        awsBinariesNamePrefix()
                                    ])
                                }
                            }
                        }
                    }
                }
                stage('Android') {
                    stages {
                        stage('Android QA for e2e') {
                            steps {
                                script {
                                    shared.runJobWithRetry(shared.jobBinary(), [
                                        shared.gitCommit(),
                                        hostAndroid(),
                                        shared.platformAndroid(),
                                        shared.brandOrchard(),
                                        shared.environmentQa(),
                                        shared.storeS3(),
                                        shared.androidBinaryTypeApk(),
                                        awsBinariesNamePrefix()
                                    ])
                                }
                            }
                        }
                        stage('Android e2e') {
                            steps {
                                script {
                                    shared.runJobWithRetry(shared.jobE2E(), [
                                        shared.e2eBrandOrchard(),
                                        shared.e2eOsAndroid(),
                                        shared.e2eOsAndroidVer(),
                                        shared.e2eDeviceAndroid(),
                                        awsBinariesNamePrefix()
                                    ])
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    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 awsBinariesNamePrefix() {
    shared.awsBinariesNamePrefix(params.AWS_BINARIES_NAME_PREFIX)
}

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

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