<?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">
    <actionGroup name="navigateToCreatedCMSPage">
        <annotations>
            <description>Goes to the Admin CMS Pages page. </description>
        </annotations>
        <arguments>
            <argument name="CMSPage" defaultValue=""/>
        </arguments>

        <amOnPage url="{{CmsPagesPage.url}}" stepKey="navigateToCMSPagesGrid"/>
        <waitForPageLoad stepKey="waitForPageLoad1"/>
        <conditionalClick selector="{{CmsPagesPageActionsSection.clearAllButton}}" dependentSelector="{{CmsPagesPageActionsSection.activeFilters}}" stepKey="clickToResetFilter" visible="true"/>
        <waitForPageLoad stepKey="waitForPageLoad2"/>
        <conditionalClick selector="//div[contains(@data-role, 'grid-wrapper')]/table/thead/tr/th/span[contains(text(), 'ID')]" dependentSelector="//span[contains(text(), 'ID')]/parent::th[not(contains(@class, '_descend'))]/parent::tr/parent::thead/parent::table/parent::div[contains(@data-role, 'grid-wrapper')]" stepKey="clickToAttemptSortByIdDescending" visible="true"/>
        <waitForLoadingMaskToDisappear stepKey="waitForFirstIdSortDescendingToFinish"/>

        <!-- Conditional Click again in case it goes from default state to ascending on first click -->
        <conditionalClick selector="//div[contains(@data-role, 'grid-wrapper')]/table/thead/tr/th/span[contains(text(), 'ID')]" dependentSelector="//span[contains(text(), 'ID')]/parent::th[not(contains(@class, '_descend'))]/parent::tr/parent::thead/parent::table/parent::div[contains(@data-role, 'grid-wrapper')]" stepKey="secondClickToAttemptSortByIdDescending" visible="true"/>
        <waitForLoadingMaskToDisappear stepKey="waitForSecondIdSortDescendingToFinish"/>
        <click selector="{{CmsPagesPageActionsSection.select(CMSPage.identifier)}}" stepKey="clickSelectCreatedCMSPage"/>
        <click selector="{{CmsPagesPageActionsSection.edit(CMSPage.identifier)}}" stepKey="navigateToCreatedCMSPage"/>
        <waitForPageLoad stepKey="waitForPageLoad3"/>
        <click selector="{{CmsNewPagePageContentSection.header}}" stepKey="clickExpandContentTabForPage"/>
        <waitForLoadingMaskToDisappear stepKey="waitForLoadingMaskOfStagingSection"/>
    </actionGroup>

    <actionGroup name="navigateToCreatedCMSBlockPage">
        <annotations>
            <description>Goes to the Admin Blocks page. Clicks on 'Edit' for the provided Block.</description>
        </annotations>
        <arguments>
            <argument name="CMSBlockPage" defaultValue=""/>
        </arguments>

        <amOnPage url="{{CmsBlocksPage.url}}" stepKey="navigateToCMSBlocksGrid"/>
        <waitForPageLoad stepKey="waitForPageLoad1"/>
        <conditionalClick selector="{{CmsPagesPageActionsSection.clearAllButton}}" dependentSelector="{{CmsPagesPageActionsSection.activeFilters}}" stepKey="clickToResetFilter" visible="true"/>
        <waitForPageLoad stepKey="waitForPageLoad2"/>
        <conditionalClick selector="{{BlockPageActionsSection.idColumn}}" dependentSelector="//span[contains(text(), 'ID')]/parent::th[not(contains(@class, '_descend'))]/parent::tr/parent::thead/parent::table/parent::div[contains(@data-role, 'grid-wrapper')]" stepKey="clickToAttemptSortByIdDescending" visible="true"/>
        <waitForLoadingMaskToDisappear stepKey="waitForFirstIdSortDescendingToFinish"/>

        <!-- Conditional Click again in case it goes from default state to ascending on first click  -->
        <conditionalClick selector="{{BlockPageActionsSection.idColumn}}" dependentSelector="//span[contains(text(), 'ID')]/parent::th[not(contains(@class, '_descend'))]/parent::tr/parent::thead/parent::table/parent::div[contains(@data-role, 'grid-wrapper')]" stepKey="secondClickToAttemptSortByIdDescending" visible="true"/>
        <waitForLoadingMaskToDisappear stepKey="waitForSecondIdSortDescendingToFinish"/>
        <click selector="{{BlockPageActionsSection.select(CMSBlockPage.identifier)}}" stepKey="clickSelectCreatedCMSBlock"/>
        <click selector="{{BlockPageActionsSection.edit(CMSBlockPage.identifier)}}" stepKey="navigateToCreatedCMSBlock"/>
        <waitForPageLoad stepKey="waitForPageLoad3"/>
        <waitForLoadingMaskToDisappear stepKey="waitForLoadingMaskOfStagingSection"/>
    </actionGroup>

    <actionGroup name="DeleteCMSBlockActionGroup">
        <annotations>
            <description>Goes to the Admin CMS Blocks page. Deletes the '_defaultBlock' Block. Validates that the Success Message is present and correct. PLEASE NOTE: The Block that is deleted it Hardcoded.</description>
        </annotations>

        <amOnPage url="{{CmsBlocksPage.url}}" stepKey="navigateToCMSPagesGrid"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
        <click selector="{{CmsPagesPageActionsSection.select(_defaultBlock.title)}}" stepKey="ClickOnSelect"/>
        <click selector="{{CmsPagesPageActionsSection.delete(_defaultBlock.title)}}" stepKey="ClickOnEdit"/>
        <waitForPageLoad stepKey="waitForPageLoad3"/>
        <click selector="{{CmsPagesPageActionsSection.deleteConfirm}}" stepKey="ClickToConfirm"/>
        <waitForPageLoad stepKey="waitForPageLoad4"/>
        <see userInput="You deleted the block." stepKey="VerifyBlockIsDeleted"/>
    </actionGroup>

    <actionGroup name="AddStoreViewToCmsPage" extends="navigateToCreatedCMSPage">
        <annotations>
            <description>EXTENDS: navigateToCreatedCMSPage. Adds the provided Store View to a Page.</description>
        </annotations>
        <arguments>
            <argument name="storeViewName" type="string"/>
        </arguments>

        <remove keyForRemoval="clickExpandContentTabForPage"/>
        <remove keyForRemoval="waitForLoadingMaskOfStagingSection"/>
        <click selector="{{CmsNewPagePiwSection.header}}" stepKey="clickPageInWebsites" after="waitForPageLoad3"/>
        <waitForElementVisible selector="{{CmsNewPagePiwSection.selectStoreView(storeViewName)}}" stepKey="waitForStoreGridReload"/>
        <clickWithLeftButton selector="{{CmsNewPagePiwSection.selectStoreView(storeViewName)}}" stepKey="clickStoreView"/>
        <click selector="{{CmsNewPagePageActionsSection.expandSplitButton}}" stepKey="expandButtonMenu"/>
        <waitForElementVisible selector="{{CmsNewPagePageActionsSection.splitButtonMenu}}" stepKey="waitForSplitButtonMenuVisible"/>
        <click selector="{{CmsNewPagePageActionsSection.savePage}}" stepKey="clickSavePage"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
        <see userInput="You saved the page." stepKey="seeMessage"/>
    </actionGroup>

    <actionGroup name="saveAndCloseCMSBlockWithSplitButton">
        <annotations>
            <description>Clicks on the Save and Close button.</description>
        </annotations>

        <waitForElementVisible selector="{{BlockNewPagePageActionsSection.expandSplitButton}}" stepKey="waitForExpandSplitButtonToBeVisible"/>
        <click selector="{{BlockNewPagePageActionsSection.expandSplitButton}}" stepKey="expandSplitButton"/>
        <click selector="{{BlockNewPagePageActionsSection.saveAndClose}}" stepKey="clickSaveBlock"/>
        <waitForPageLoad stepKey="waitForPageLoadAfterClickingSave"/>
        <see userInput="You saved the block." stepKey="assertSaveBlockSuccessMessage"/>
    </actionGroup>

    <actionGroup name="navigateToStorefrontForCreatedPage">
        <annotations>
            <description>Goes to the provided Page on the Storefront.</description>
        </annotations>
        <arguments>
            <argument name="page" type="string"/>
        </arguments>

        <amOnPage url="{{page}}" stepKey="goToStorefront"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
    </actionGroup>

    <actionGroup name="saveCMSBlock">
        <annotations>
            <description>Clicks on the Save button. Validates that the Save message is present and correct. PLEASE NOTE: The message is Hardcoded.</description>
        </annotations>

        <waitForElementVisible selector="{{CmsNewBlockBlockActionsSection.savePage}}" stepKey="waitForSaveButton"/>
        <click selector="{{CmsNewBlockBlockActionsSection.savePage}}" stepKey="clickSaveButton"/>
        <waitForPageLoad stepKey="waitForPageLoad"/>
        <see userInput="You saved the block." stepKey="seeSuccessfulSaveMessage"/>
    </actionGroup>

    <actionGroup name="saveAndContinueEditCmsPage">
        <annotations>
            <description>Clicks on the Save and Continue button.</description>
        </annotations>

        <waitForElementVisible time="10" selector="{{CmsNewPagePageActionsSection.saveAndContinueEdit}}" stepKey="waitForSaveAndContinueVisibility"/>
        <click selector="{{CmsNewPagePageActionsSection.saveAndContinueEdit}}" stepKey="clickSaveAndContinueEditCmsPage"/>
        <waitForPageLoad stepKey="waitForCmsPageLoad"/>
        <waitForElementVisible time="1" selector="{{CmsNewPagePageActionsSection.cmsPageTitle}}" stepKey="waitForCmsPageSaveButton"/>
        <waitForLoadingMaskToDisappear stepKey="waitForLoadingMask"/>
    </actionGroup>

    <actionGroup name="saveCmsPage">
        <annotations>
            <description>Clicks on the Save button. Validates that the Success Message is present.</description>
        </annotations>

        <waitForElementVisible selector="{{CmsNewPagePageActionsSection.expandSplitButton}}" stepKey="waitForSplitButton"/>
        <click selector="{{CmsNewPagePageActionsSection.expandSplitButton}}" stepKey="expandSplitButton"/>
        <waitForElementVisible selector="{{CmsNewPagePageActionsSection.savePage}}" stepKey="waitForSaveCmsPage"/>
        <click selector="{{CmsNewPagePageActionsSection.savePage}}" stepKey="clickSaveCmsPage"/>
        <waitForElementVisible time="1" selector="{{CmsPagesPageActionsSection.addNewPageButton}}" stepKey="waitForCmsPageSaveButton"/>
        <see userInput="You saved the page." selector="{{CmsPagesPageActionsSection.savePageSuccessMessage}}" stepKey="assertSavePageSuccessMessage"/>
    </actionGroup>

    <actionGroup name="setLayout">
        <annotations>
            <description>Sets the provided option for 'Layout', under 'Design' on the Page creation/edit page. </description>
        </annotations>
        <arguments>
            <argument name="designSection"/>
            <argument name="layoutOption"/>
        </arguments>

        <waitForElementVisible selector="{{designSection.DesignTab}}" stepKey="waitForDesignTabVisible"/>
        <conditionalClick selector="{{designSection.DesignTab}}" dependentSelector="{{designSection.LayoutDropdown}}" visible="false" stepKey="clickOnDesignTab"/>
        <waitForPageLoad stepKey="waitForPageLoadDesignTab"/>
        <waitForElementVisible selector="{{designSection.LayoutDropdown}}" stepKey="waitForLayoutDropDown"/>
        <selectOption selector="{{designSection.LayoutDropdown}}" userInput="{{layoutOption}}" stepKey="selectLayout"/>
    </actionGroup>
</actionGroups>
