package com.braintree.customersupportbackoffice.builders;

public class GooglePayScriptBuilder {
    private String src;
    private String container;
    private String buttonSizeMode;
    private String buttonType;

    public GooglePayScriptBuilder setSrc(String src) {
        this.src = src;
        return this;
    }

    public GooglePayScriptBuilder setContainer(String container) {
        this.container = container;
        return this;
    }

    public GooglePayScriptBuilder setButtonSizeMode(String buttonSizeMode) {
        this.buttonSizeMode = buttonSizeMode;
        return this;
    }

    public GooglePayScriptBuilder setButtonType(String buttonType) {
        this.buttonType = buttonType;
        return this;
    }

    public String buildScript() {
        String loadScriptFunction =
                "function loadScript(src) {" +
                        "    return new Promise(function (resolve, reject) {" +
                        "      const script = document.createElement('script');" +
                        "      script.src = src;" +
                        "      script.async = true;" +
                        "      script.onerror = function (err) {" +
                        "        reject(err, script);" +
                        "      };" +
                        "      script.onload = script.onreadystatechange = function () {" +
                        "        if (!this.readyState || this.readyState == 'complete') {" +
                        "          resolve();" +
                        "        }" +
                        "      };" +
                        "      document.head.appendChild(script);" +
                        "    });" +
                        "  }";

        return container + "<script>" + loadScriptFunction +
                String.format(
                        "   function executeDelayedScript() { setTimeout(function () {" +
                                "    loadScript('%s')" +
                                "      .then(onGooglePayLoaded)" +
                                "      .catch(function (error) {" +
                                "        console.error('Error loading Google Pay script:', error);" +
                                "      });" +
                                "  }, 1000);" +
                                "  function onGooglePayLoaded() {" +
                                "    addGooglePayButton();" +
                                "  }" +
                                "  function addGooglePayButton() {" +
                                "    const paymentsClient = new google.payments.api.PaymentsClient({" +
                                "      environment: ''," +
                                "    });" +
                                "    const button = paymentsClient.createButton({" +
                                "      buttonSizeMode: '%s'," +
                                "      buttonType: '%s'," +
                                "      onClick: onGooglePaymentButtonClicked," +
                                "    });" +
                                "    button.disabled = true;" +
                                "    const buttonContainer = document.getElementById('googlePay-button-container');" +
                                "    buttonContainer.appendChild(button);" +
                                "  }" +
                                "  function onGooglePaymentButtonClicked() {}" +
                                "}" +
                                "executeDelayedScript();",
                        src, buttonSizeMode, buttonType) +
                "</script>";
    }
}
