package com.braintree.customersupportbackoffice.builders;

import de.hybris.bootstrap.annotations.UnitTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class GooglePayScriptBuilderTest {

    private static final String SRC = "src";
    private static final String CONTAINER = "container";
    private static final String BUTTON_SIZE_MODE = "buttonSizeMode";
    private static final String BUTTON_TYPE = "buttonType";

    private static final String EXPECTED_SCRIPT = CONTAINER +
            "<script>" +
            "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);" +
            "    });" +
            "  }" +
            "   function executeDelayedScript() { setTimeout(function () {" +
            "    loadScript('" + SRC + "')" +
            "      .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: '" + BUTTON_SIZE_MODE + "'," +
            "      buttonType: '" + BUTTON_TYPE + "'," +
            "      onClick: onGooglePaymentButtonClicked," +
            "    });" +
            "    button.disabled = true;" +
            "    const buttonContainer = document.getElementById('googlePay-button-container');" +
            "    buttonContainer.appendChild(button);" +
            "  }" +
            "  function onGooglePaymentButtonClicked() {}" +
            "}" +
            "executeDelayedScript();" +
            "</script>";

    @InjectMocks
    private GooglePayScriptBuilder unit;

    @Test
    public void shouldBuildGooglePayScript() {
        String result = unit.setSrc(SRC)
                .setContainer(CONTAINER)
                .setButtonSizeMode(BUTTON_SIZE_MODE)
                .setButtonType(BUTTON_TYPE)
                .buildScript();

        assertEquals(EXPECTED_SCRIPT, result);
    }

}