<?php
namespace Ecomnova\Tests;

use Ecomnova\EcomnovaPage;
use Ecomnova\ProductPageInterface;
use PHPUnit\Framework\TestCase;

class ProductPageBasicTest extends TestCase
{
    private $tester;
    private $testPageUrl;
    public function setUp() : void
    {
        $this->tester = EcomnovaPage::getTesterInstance(ProductPageInterface::class);
        $this->tester->init();
        $this->testPageUrl = "https://www.appleyardflowers.com/flowers/all-flowers/bluebelle";
    }
    public function testClickingSizes()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $sizeElementRadio = $this->tester->selectSizeOptionByLabel(" Chic"); // Very important, enter the label text as
        // it is in the DOM with all the white space
        $this->assertTrue($sizeElementRadio->isSelected());
        $sizeElementRadio = $this->tester->selectSizeOptionByLabel(" Grande");
        $this->assertTrue($sizeElementRadio->isSelected());
        $sizeElementRadio = $this->tester->selectSizeOptionByLabel(" Bijou");
        $this->assertTrue($sizeElementRadio->isSelected());
    }

    public function testGetTotalPrice()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $sizeElementRadio = $this->tester->selectSizeOptionByLabel(" Chic");
        $this->assertTrue($sizeElementRadio->isSelected());
        $expectedPrice = "£49.98";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
        $sizeElementRadio = $this->tester->selectSizeOptionByLabel(" Grande");
        $this->assertTrue($sizeElementRadio->isSelected());
        $expectedPrice = "£67.98";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
    }

    public function testClickingGifts()
    {
        
        $this->tester->openProductPage($this->testPageUrl);
        $giftCheckboxElement = $this->tester->selectGiftOptionByLabel("8 Milk Truffles Tray");
        $this->assertTrue($giftCheckboxElement->isSelected());
        $expectedPrice = "£48.97";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
        $giftCheckboxElement = $this->tester->selectGiftOptionByLabel("Nigella Vase");
        $this->assertTrue($giftCheckboxElement->isSelected());
        $expectedPrice = "£56.96";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
    }
    
    public function testEnteringRecipientName()
    {
        $recipientName = "John Doe";
        $this->tester->openProductPage($this->testPageUrl);
        $recipientTextBox = $this->tester->enterRecipientName($recipientName);
        $this->assertTrue($recipientTextBox->getAttribute('value') == $recipientName);
    }
    public function testEnteringPersonalMessage()
    {
        $personalMessage = "This is a test message";
        $this->tester->openProductPage($this->testPageUrl);
        $personalMessageTextArea = $this->tester->enterRecipientName($personalMessage);
        $this->assertTrue($personalMessageTextArea->getAttribute('value') == $personalMessage);
    }

    public function testClickingGiftCard()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $giftCardCheckboxElement = $this->tester->selectGiftCardByLabel("Happy Birthday ");
        $this->assertTrue($giftCardCheckboxElement->isSelected());
        $expectedPrice = "£44.97";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
    }

    public function enterPostCodeTest()
    {
        $postcode = "SW1A 1AA";
        $this->tester->openProductPage($this->testPageUrl);
        $recipientAddress = $this->tester->selectRecipientAddress($postcode);
        $expectedCompanyName = "Buckingham Palace";
        $this->assertTrue($recipientAddress['companyName'] == $expectedCompanyName,
            "Expected company name is ".$expectedCompanyName." but got " . $recipientAddress['companyName']);
        $expectedAddressLine1 = "";
        $this->assertTrue($recipientAddress['addressLine1'] == $expectedAddressLine1,
            "Expected address line 1 is ".$expectedAddressLine1." but got " . $recipientAddress['addressLine1']);
        $expectedAddressLine2 = "";
        $this->assertTrue($recipientAddress['addressLine2'] == $expectedAddressLine2,
            "Expected address line 2 is ".$expectedAddressLine2." but got " . $recipientAddress['addressLine2']);
        $expectedTown = "London";
        $this->assertTrue($recipientAddress['town'] == $expectedTown,
            "Expected town is ".$expectedTown." but got " . $recipientAddress['town']);
        $expectedCounty = "";
        $this->assertTrue($recipientAddress['county'] == $expectedCounty,
            "Expected county is ".$expectedCounty." but got " . $recipientAddress['county']);
    }

    /**
     * Note, this test is using real calendar data and can fail depending on the date and the time of the test
     * @return void
     */
    public function testSelectingDeliveryDateDynamic()
    {
        $date = new \DateTime();
        //get tomorrows date
        $date->add(new \DateInterval('P2D'));
        $deliveryDay = $date->format('d');
        $deliveryDate = $date->format('d/m/Y');
        $this->tester->openProductPage($this->testPageUrl);
        $dateElement = $this->tester->selectDeliveryDate((int)$deliveryDay);
        $this->assertTrue($dateElement->getAttribute('value') == $deliveryDate,
            "Expected date is ".$deliveryDate." but got " . $dateElement->getAttribute('value'));
    }

    public function testSelectingDeliveryDateNextAvailable()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $dateElement = $this->tester->selectDeliveryDate();
        //create a date from $dateElement->getAttribute('value') and assert it is greater than today
        $date = \DateTime::createFromFormat('d/m/Y', $dateElement->getAttribute('value'));
        $today = new \DateTime();
        $this->assertTrue($date > $today,
            "Expected date is greater than today but got " . $date->format('d/m/Y'));
    }
    
    public function testSelectingPrivilegeOption()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $this->tester->selectPrivilegeOption();
        $expectedPrice = "£48.99";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
        $this->tester->unselectPrivilegeOption();
        $expectedPrice = "£42.98";
        $this->assertTrue($this->tester->getTotalPrice() == $expectedPrice,
            "Expected price is ".$expectedPrice." but got " . $this->tester->getTotalPrice());
    }

    public function testSelectDeliveryMethod()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $deliveryMethod = "Priority Tracked Delivery By 5pm ";
        $checkedDeliveryElement = $this->tester->selectDeliveryMethodByName($deliveryMethod);
        $this->assertTrue($checkedDeliveryElement->isSelected(),
            "Expected delivery method is selected but it is not");
        $expectedClass = "rmtracked24v2";
        //explode by " ", trim each value
        $classes = explode(" ", $checkedDeliveryElement->getAttribute('class'));
        $this->assertTrue(in_array($expectedClass, $classes),
            "Expected class ".$expectedClass." is not in " . $checkedDeliveryElement->getAttribute('class'));

    }

    /**
     * Currently untestable, returned element is <ul class="chosen-results"></ul> with no li elements, li elements added by chosen
     */
  /*  #[Test]
    public function testSelectingOccasionOption()
    {
        $this->tester->openProductPage($this->testPageUrl);
        $occasion = "Birthday";
        $checkedOccasionElement = $this->tester->selectOccasionOptionByName($occasion);

        $classes = explode(" ",$checkedOccasionElement->getAttribute('class'));
        $this->assertTrue(in_array('result-selected', $classes),
            "Expected occasion is selected but it is not");

    }*/


}