<?php

namespace Tests\Browser\Content;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\Browser\Traits\AuthTrait;
use Tests\DuskTestCase;

class BlockTest extends DuskTestCase
{
    use DatabaseMigrations;
    use AuthTrait;

    public function testShouldFailValidationOnCreateBlockWithMissingRequiredFields()
    {
        $this->browse(function (Browser $browser) {

            $this->loginToAdminBackend();

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->click('#backend_save_btn')

                ->assertAttribute('#title_en', "data-original-title", "The Block Title field is required.")

                ->click('#lang_tab_pt')
                ->assertAttribute('#title_pt', "data-original-title", "The Block Title field is required.")
                ->assertAttribute('#lang_slug_pt', "data-original-title", "The lang slug.pt field is required.")

                ->click('#lang_tab_en')
                ->assertAttribute('#lang_slug_en', "data-original-title", "The lang slug.en field is required.");
        });
    }

    public function testShouldCreateBlockWithStatusDraftAndThenPublishIt()
    {
        $this->browse(function (Browser $browser) {

            // Session remains within TestCase

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->type('#title_en', 'Test Block EN')
                ->select('#type', 'blockWysiwyg')
                ->type('#lang_slug_en', 'test-block-en')
                ->type('#block_title_en', 'Test Block Editor Title EN')
                ->type('#redactor-uuid-0', 'Test Block Editor EN content')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test Block PT')
                ->type('#lang_slug_pt', 'test-block-pt')
                ->type('#block_title_pt', 'Test Block Editor Title PT')
                ->type('#redactor-uuid-1', 'Test Block Editor PT content')
                ->select('#status', 'draft')
                ->click('#backend_save_btn')

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertInputValue('#redactor-uuid-0', 'Test Block Editor EN content')
                ->assertInputValue('#block_title_en', 'Test Block Editor Title EN')
                ->click('#lang_tab_pt')
                ->assertInputValue('#redactor-uuid-1', 'Test Block Editor PT content')
                ->assertSelected('#status', 'draft')

                ->select('#status', 'published')
                ->click('#backend_save_btn')

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertInputValue('#redactor-uuid-0', 'Test Block Editor EN content')
                ->assertInputValue('#block_title_en', 'Test Block Editor Title EN')
                ->click('#lang_tab_pt')
                ->assertInputValue('#redactor-uuid-1', 'Test Block Editor PT content')
                ->assertSelected('#status', 'published');
        });
    }

    public function testShouldCreateEditorTypeBlock()
    {
        $this->browse(function (Browser $browser) {

            // Session remains within TestCase

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->type('#title_en', 'Test Block EN')
                ->select('#type', 'blockWysiwyg')
                ->type('#lang_slug_en', 'test-block-en')
                ->type('#block_title_en', 'Test Block Editor Title EN')
                ->type('#redactor-uuid-0', 'Test Block Editor EN content')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test Block PT')
                ->type('#lang_slug_pt', 'test-block-pt')
                ->type('#block_title_pt', 'Test Block Editor Title PT')
                ->type('#redactor-uuid-1', 'Test Block Editor PT content')
                ->click('#backend_save_btn')

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertInputValue('#redactor-uuid-0', 'Test Block Editor EN content')
                ->assertInputValue('#block_title_en', 'Test Block Editor Title EN')
                ->click('#lang_tab_pt')
                ->assertInputValue('#redactor-uuid-1', 'Test Block Editor PT content')
                ->assertSelected('#status', 'published');
        });
    }

    public function testShouldCreateLocationTypeBlock()
    {
        $this->browse(function (Browser $browser) {

            // Session remains within TestCase

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->type('#title_en', 'Test Block EN')
                ->select('#type', 'blockLocation')
                ->type('#lang_slug_en', 'test-block-en')
                ->type('#block_title_en', 'Test Block Editor Title EN')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test Block PT')
                ->type('#lang_slug_pt', 'test-block-pt')
                ->type('#block_title_pt', 'Test Block Editor Title PT')
                ->type('#location_address', 'rua garret, lisboa')
                ->click('#location_search_btn')
                ->pause(1000)
                ->click('#backend_save_btn')

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertInputValue('#location_lat', '38.7107931')
                // longitude is actually -9.14084439999999 , but it gets rounded
                ->assertInputValue('#location_lng', '-9.1408444');
        });
    }

    public function testShouldCreateSliderTypeBlock()
    {
        $this->browse(function (Browser $browser) {

            // Session remains within TestCase

            // Create Slider instance
            $browser->visit('/admin/content/form/sliderItems')
                ->assertSee('Title')
                ->type('#title_en', 'Test SliderItem EN')
                ->type('#description_en', 'Test Slider Description EN')
                ->type('#lang_slug_en', 'test-slider-en')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test SliderItem PT')
                ->type('#description_pt', 'Test Slider Description PT')
                ->type('#lang_slug_pt', 'test-slider-pt')
                ->click('.select-media')
                ->whenAvailable('#media_manager', function ($mediaModal) {
                    $mediaModal->attach('#upload_media_input', 'tests/Browser/resources/eae_for_tests.png')
                        ->pause(100)
                        ->assertPresent('#media_box_1')
                        ->click('.done-select');
                })
                ->pause(500)
                ->click('#backend_save_btn')
                ->pause(100)

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/sliderItems/1')
                ->assertSee('Changes saved with success.');

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->type('#title_en', 'Test Block Slider Title EN')
                ->select('#type', 'blocklocation') // need to act on some input to make #lang_slug _en enabled
                ->select('#type', 'blockSlider')
                ->type('#lang_slug_en', 'test-block-en')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test Block Slider Title PT')
                ->type('#lang_slug_pt', 'test-block-pt')
                ->typeSlowly('#slider_search', 'Test SliderItem')
                ->pause(300)
                ->assertSeeIn('.tt-suggestion', 'Test SliderItem EN')
                ->click('.tt-suggestion')
                ->pause(100)
                ->assertSeeIn('.picker-title', 'Test SliderItem EN')
                ->click('#backend_save_btn')
                ->pause(100)

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertSeeIn('.picker-title', 'Test SliderItem EN');
        });
    }

    /*public function testShouldCreateSliderTypeBlockWithSliderCreationInBlockForm()
    {
        $this->browse(function (Browser $browser) {

            // Session remains within TestCase

            $browser->visit('/admin/content/form/blocks')
                ->assertSee('Block Title')
                ->type('#title_en', 'Test Block Slider Title EN')
                ->select('#type', 'blocklocation') // need to act on some input to make #lang_slug _en enabled
                ->pause(50)
                ->select('#type', 'blockSlider')
                ->pause(50)
                ->type('#lang_slug_en', 'test-block-en')
                ->click('#lang_tab_pt')
                ->type('#title_pt', 'Test Block Slider Title PT')
                ->type('#lang_slug_pt', 'test-block-pt')
                ->click('#multiple-picker-slider .command-add-content')
                ->pause(100)
                ->whenAvailable('#generic_wrapper_sliderItems', function ($sliderModal) {
    // Something is preventing the inputs in the modal to be accessible
                    $sliderModal->assertSee('Title')
                        ->waitFor('#title_en')
                        ->type('#title_en', 'Test SliderItem EN')
                        ->click('#lang_tab_pt')
                        ->type('#title_pt', 'Test SliderItem PT')
                        ->click('#backend_save_btn')
                        ->pause(100);
                })
                ->pause(100)
                ->assertSeeIn('.picker-title', 'Test SliderItem EN')
                ->click('#backend_save_btn')
                ->pause(100)

                ->waitForText('Changes saved with success.')

                ->assertPathIs('/admin/content/form/blocks/1')
                ->assertSee('Changes saved with success.')

                ->assertSeeIn('.picker-title', 'Test SliderItem EN');
        });
    }*/
}
