package com.org

class SayHello {

    String name = null
    def script = null
    // def awscli = new AwsCli(this.script)

    SayHello(String name, def script) {
        this.name = name
        this.script = script
    }
    String tryReadYaml() {
        def yamlMap = this.script.readYaml(file: '.acquia/pipeline.yaml')
        return yamlMap
    }

    String hello() {
        return "Hello, ${name}!"
    }

    String tryWithCredentials() { 
        this.script.withCredentials([this.script.usernamePassword(credentialsId: 'test-credentials', 
            passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
            return this.script.sh(script: "echo \"hello ${USERNAME} with password ${PASSWORD}\"", returnStdout: true)
        }
    }

    def tryShCommands() {
        def output = this.script.sh(script: 'echo "hello from sh command"', returnStdout: true)
        println output
    }

    String tryReadFile() {
        return this.script.readFile('README.md')
    }

    String tryReadingResponses() {
        this.script.withCredentials([this.script.string(credentialsId: 'git-token', variable: 'GIT_TOKEN')]) {
            def response = this.script.httpRequest contentType: 'APPLICATION_JSON',
                                                httpMode: 'GET',
                                                customHeaders: [[name: 'Authorization',
                                                value: "Token ${this.script.GIT_TOKEN}"]],
                                                url: 'https://api.github.com/search/repositories?q=language:groovy&sort=stars&order=desc'

            // Reading the response content as JSON
            def jsonResponse = this.script.readJSON text: response.getContent()
            return jsonResponse
        }
        
    }

    // def trySharedLibMethod() {
    //     def result = this.script.sayHello('2 Cheese Burgers and 1 Beer')
    //     println result
    // }

    def tryClosure(Closure closure) {
        return closure.call()
    }

    void tryWithEnv() {
        println "Environment variable: ${this.script.env.HELLO}"
    }

    // def tryMethodFromOtherClass() {
    //     def result = awscli.listObjectsFromS3("--bucket heavy-plastic")
    //     println result
    // }

    def tryGettingStage() {
        return {
            stage('We are testing') {
                def result = this.script.sh(script: 'echo "hello from stage"', returnStdout: true)
                println "Stage result: ${result}"
            }
        }
    }

    void tryExceptionCatch() {
        try {
            def result = 10 / 0
        } catch (ArithmeticException e) {
            println "Exception caught: ${e}"
        }
    }
}