<?xml version="1.0" encoding="UTF-8"?>
<!--
 /**
  * Copyright © Magento, Inc. All rights reserved.
  * See COPYING.txt for license details.
  */
-->

<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd">
    <!-- Apply Sales Rule Coupon to the cart -->
    <actionGroup name="StorefrontApplyCouponActionGroup">
        <arguments>
            <argument name="coupon"/>
        </arguments>
        <waitForElement selector="{{StorefrontSalesRuleCartCouponSection.couponHeader}}" time="30" stepKey="waitForCouponHeader" />
        <conditionalClick selector="{{StorefrontSalesRuleCartCouponSection.couponHeader}}" dependentSelector="{{StorefrontSalesRuleCartCouponSection.discountBlockActive}}" visible="false" stepKey="clickCouponHeader" />
        <waitForElementVisible selector="{{StorefrontSalesRuleCartCouponSection.couponField}}" stepKey="waitForCouponField" />
        <fillField userInput="{{coupon.code}}" selector="{{StorefrontSalesRuleCartCouponSection.couponField}}" stepKey="fillCouponField"/>
        <click selector="{{StorefrontSalesRuleCartCouponSection.applyButton}}" stepKey="clickApplyButton"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Cancel Sales Rule Coupon applied to the cart -->
    <actionGroup name="StorefrontCancelCouponActionGroup">
        <waitForElement selector="{{StorefrontSalesRuleCartCouponSection.couponHeader}}" time="30" stepKey="waitForCouponHeader" />
        <conditionalClick selector="{{StorefrontSalesRuleCartCouponSection.couponHeader}}" dependentSelector="{{StorefrontSalesRuleCartCouponSection.discountBlockActive}}" visible="false" stepKey="clickCouponHeader" />
        <waitForElementVisible selector="{{StorefrontSalesRuleCartCouponSection.couponField}}" stepKey="waitForCouponField" />
        <click selector="{{StorefrontSalesRuleCartCouponSection.cancelButton}}" stepKey="clickCancelButton"/>
    </actionGroup>

    <!-- Check applied discount in cart summary -->
    <actionGroup name="StorefrontCheckCouponAppliedActionGroup">
        <arguments>
            <argument name="rule" />
            <argument name="discount" type="string" />
        </arguments>
        <waitForElementVisible selector="{{CheckoutCartSummarySection.discountTotal}}" stepKey="waitForDiscountTotal"/>
        <see userInput="{{rule.store_labels[1][store_label]}}" selector="{{CheckoutCartSummarySection.discountLabel}}" stepKey="assertDiscountLabel"/>
        <see userInput="-${{discount}}" selector="{{CheckoutCartSummarySection.discountTotal}}" stepKey="assertDiscountTotal"/>
    </actionGroup>

    <actionGroup name="VerifyDiscountAmount">
        <arguments>
            <argument name="productUrl" type="string"/>
            <argument name="quantity" type="string"/>
            <argument name="expectedDiscount" type="string"/>
        </arguments>
        <amOnPage url="{{productUrl}}" stepKey="goToProductPage"/>
        <waitForPageLoad stepKey="waitForProductPageLoad"/>
        <fillField selector="{{StorefrontProductActionSection.quantity}}" userInput="{{quantity}}" stepKey="fillQuantity"/>
        <click selector="{{StorefrontProductActionSection.addToCart}}" stepKey="addProductToCart"/>
        <waitForPageLoad stepKey="waitForAddToCart"/>
        <waitForElementVisible selector="{{StorefrontMessagesSection.success}}" stepKey="waitForSuccessMessage"/>
        <amOnPage url="{{CheckoutCartPage.url}}" stepKey="goToCartPage"/>
        <waitForPageLoad stepKey="waitForCartPage"/>
        <waitForElementVisible selector="{{CheckoutCartSummarySection.discountAmount}}" stepKey="waitForDiscountElement"/>
        <see selector="{{CheckoutCartSummarySection.discountAmount}}" userInput="{{expectedDiscount}}" stepKey="seeDiscountTotal"/>
    </actionGroup>
</actionGroups>
