<?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">
    <!-- Quick search the phrase and check if the result page contains correct information -->
    <actionGroup name="StorefrontCheckQuickSearchActionGroup">
        <arguments>
            <argument name="phrase" />
        </arguments>
        <submitForm selector="{{StorefrontQuickSearchSection.searchMiniForm}}" parameterArray="['q' => {{phrase}}]" stepKey="fillQuickSearch" />
        <seeInCurrentUrl url="{{StorefrontCatalogSearchPage.url}}" stepKey="checkUrl"/>
        <dontSeeInCurrentUrl url="form_key=" stepKey="checkUrlFormKey"/>
        <seeInTitle userInput="Search results for: '{{phrase}}'" stepKey="assertQuickSearchTitle"/>
        <see userInput="Search results for: '{{phrase}}'" selector="{{StorefrontCatalogSearchMainSection.SearchTitle}}" stepKey="assertQuickSearchName"/>
    </actionGroup>

    <!-- Quick search the phrase and check if the result page contains correct information, usable with type="string" -->
    <actionGroup name="StorefrontCheckQuickSearchStringActionGroup">
        <arguments>
            <argument name="phrase" type="string"/>
        </arguments>
        <fillField stepKey="fillInput" selector="{{StorefrontQuickSearchResultsSection.searchTextBox}}" userInput="{{phrase}}"/>
        <submitForm selector="{{StorefrontQuickSearchResultsSection.searchTextBox}}" parameterArray="[]" stepKey="submitQuickSearch" />
        <seeInCurrentUrl url="{{StorefrontCatalogSearchPage.url}}" stepKey="checkUrl"/>
        <dontSeeInCurrentUrl url="form_key=" stepKey="checkUrlFormKey"/>
        <seeInTitle userInput="Search results for: '{{phrase}}'" stepKey="assertQuickSearchTitle"/>
        <see userInput="Search results for: '{{phrase}}'" selector="{{StorefrontCatalogSearchMainSection.SearchTitle}}" stepKey="assertQuickSearchName"/>
    </actionGroup>

    <!-- Opens product from QuickSearch and performs assertions-->
    <actionGroup name="StorefrontOpenProductFromQuickSearch">
        <arguments>
            <argument name="productName" type="string"/>
            <argument name="productUrlKey" type="string"/>
        </arguments>
        <click stepKey="openProduct" selector="{{StorefrontQuickSearchResultsSection.productByName(productName)}}"/>
        <waitForPageLoad stepKey="waitForProductLoad"/>
        <seeInCurrentUrl url="{{productUrlKey}}" stepKey="checkUrl"/>
        <see stepKey="checkName" selector="{{StorefrontProductInfoMainSection.productName}}" userInput="{{productName}}"/>
    </actionGroup>

    <!-- Adds product from Quicksearch page and perform assertions-->
    <actionGroup name="StorefrontAddToCartFromQuickSearch">
        <arguments>
            <argument name="productName" type="string"/>
        </arguments>
        <moveMouseOver stepKey="hoverOverProduct" selector="{{StorefrontQuickSearchResultsSection.productByIndex('1')}}"/>
        <click selector="{{StorefrontQuickSearchResultsSection.productByName(productName)}} {{StorefrontQuickSearchResultsSection.addToCartBtn}}" stepKey="addToCart"/>
        <waitForElementVisible selector="{{StorefrontQuickSearchResultsSection.messageSection}}" time="30" stepKey="waitForProductAdded"/>
        <see selector="{{StorefrontQuickSearchResultsSection.messageSection}}" userInput="You added {{productName}} to your shopping cart." stepKey="seeAddedToCartMessage"/>
    </actionGroup>

    <actionGroup name="StorefrontQuickSearchCheckProductNameInGrid">
        <arguments>
            <argument name="productName" type="string"/>
            <argument name="index" type="string"/>
        </arguments>
        <see selector="{{StorefrontQuickSearchResultsSection.productByIndex(index)}}" userInput="{{productName}}" stepKey="seeProductName"/>
    </actionGroup>

    <actionGroup name="StorefrontQuickSearchSeeProductByName">
        <arguments>
            <argument name="productName" type="string"/>
        </arguments>
        <see selector="{{StorefrontQuickSearchResultsSection.productByName(productName)}}" userInput="{{productName}}" stepKey="seeProductName"/>
    </actionGroup>

    <actionGroup name="StorefrontQuickSearchCheckProductNameNotInGrid">
        <arguments>
            <argument name="productName" type="string"/>
        </arguments>
        <dontSee selector="{{StorefrontQuickSearchResultsSection.allResults}}" userInput="{{productName}}" stepKey="dontSeeProductName"/>
    </actionGroup>
    
    <!-- Open advanced search page -->
    <actionGroup name="StorefrontOpenAdvancedSearchActionGroup">
        <click selector="{{StorefrontFooterSection.AdvancedSearch}}" stepKey="clickAdvancedSearchLink" />
        <seeInCurrentUrl url="{{StorefrontCatalogSearchAdvancedFormPage.url}}" stepKey="checkUrl"/>
        <seeInTitle userInput="Advanced Search" stepKey="assertAdvancedSearchTitle1"/>
        <see userInput="Advanced Search" selector="{{StorefrontCatalogSearchAdvancedFormSection.SearchTitle}}" stepKey="assertAdvancedSearchTitle2"/>
    </actionGroup>

    <!-- Check that Advanced Search result page contains correct information -->
    <actionGroup name="StorefrontCheckAdvancedSearchResultActionGroup">
        <seeInCurrentUrl url="{{StorefrontCatalogSearchAdvancedResultPage.url}}" stepKey="checkUrl"/>
        <seeInTitle userInput="Advanced Search Results" stepKey="assertAdvancedSearchTitle"/>
        <see userInput="Catalog Advanced Search" selector="{{StorefrontCatalogSearchMainSection.SearchTitle}}" stepKey="assertAdvancedSearchName"/>
    </actionGroup>

    <!-- Select the category in the filter -->
    <actionGroup name="StorefrontSelectSearchFilterCategoryActionGroup">
        <arguments>
            <argument name="category"/>
        </arguments>
        <click selector="{{StorefrontCategoryFilterSection.CategoryFilter}}" stepKey="clickCategoryFilterTitle" />
        <scrollTo selector="{{StorefrontCategoryFilterSection.CategoryByName(category.name)}}" stepKey="scrollToClickCategoryFilter"/>
        <click selector="{{StorefrontCategoryFilterSection.CategoryByName(category.name)}}" stepKey="clickCategoryFilter"/>
    </actionGroup>

    <!-- Go to store's advanced catalog search page -->
    <actionGroup name="GoToStoreViewAdvancedCatalogSearchActionGroup">
        <amOnPage url="{{StorefrontCatalogSearchAdvancedFormPage.url}}" stepKey="GoToStoreViewAdvancedCatalogSearchActionGroup"/>
        <waitForPageLoad time="90" stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product name -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByProductNameActionGroup">
        <arguments>
            <argument name="name" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.ProductName}}" userInput="{{name}}" stepKey="fill"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product sku -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByProductSkuActionGroup">
        <arguments>
            <argument name="sku" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.SKU}}" userInput="{{sku}}" stepKey="fill"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product description -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByDescriptionActionGroup">
        <arguments>
            <argument name="description" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.Description}}" userInput="{{description}}" stepKey="fill"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product short description -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByShortDescriptionActionGroup">
        <arguments>
            <argument name="shortDescription" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.ShortDescription}}" userInput="{{shortDescription}}" stepKey="fill"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product name and price -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByProductNameAndPriceActionGroup">
        <arguments>
            <argument name="name" type="string"/>
            <argument name="priceFrom" type="string"/>
            <argument name="priceTo" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.ProductName}}" userInput="{{name}}" stepKey="fillName"/>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.PriceFrom}}" userInput="{{priceFrom}}" stepKey="fillPriceFrom"/>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.PriceTo}}" userInput="{{priceTo}}" stepKey="fillPriceTo"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Storefront advanced catalog search by product name and description -->
    <actionGroup name="StorefrontAdvancedCatalogSearchByProductNameAndDescriptionActionGroup">
        <arguments>
            <argument name="name" type="string"/>
            <argument name="description" type="string"/>
        </arguments>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.ProductName}}" userInput="{{name}}" stepKey="fillName"/>
        <fillField selector="{{StorefrontCatalogSearchAdvancedFormSection.Description}}" userInput="{{description}}" stepKey="fillDescription"/>
        <click selector="{{StorefrontCatalogSearchAdvancedFormSection.SubmitButton}}" stepKey="clickSubmit"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <!-- Asserts that search results do not contain any results-->
    <actionGroup name="StorefrontCheckSearchIsEmpty">
        <see stepKey="checkEmpty" selector="{{StorefrontQuickSearchResultsSection.messageSection}}" userInput="Your search returned no results"/>
    </actionGroup>
</actionGroups>