<?php

namespace Tests\Unit;

use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Str;

use App\Models\Page;
use App\Models\ContentElement;
use App\Models\TextBlock;
use App\Models\User;

use Tests\Unit\FieldLinkTestTrait;

trait PageLinkTestTrait
{
    use WithFaker;
    use FieldLinkTestTrait;
    abstract protected function getModel();
    abstract protected function getLinkFields();

    public function test_if_a_page_is_displayed_in_the_front_end_we_convert_page_id_links_with_content_links()
    {
        $content = $this->getModel();
        $content_element = $content->contentElement;
        $this->assertInstanceOf(ContentElement::class, $content_element);
        $page = Page::factory()->create();
        $page->publish();
        $this->assertNotNull($page->full_slug);
        $page->contentElements()->attach($content_element, ['sort_order' => 1, 'unlisted' => false, 'randomize' => false, 'version_id' => $page->getDraftVersion()->id]);

        $content2 = $this->getModel();
        $content_element2 = $content2->contentElement;
        $content_element2->pages()->detach();
        $page->contentElements()->attach($content_element2, ['sort_order' => 1, 'unlisted' => false, 'randomize' => false, 'version_id' => $page->getDraftVersion()->id]);
        $user = User::factory()->create();
        $user->addRole('pages-editor'); // I dont think we need to do this
        $this->signIn($user);

        foreach ($this->getLinkFields() as $link_field) {
            $body = '<p>'.$this->faker->sentence.' <a class="button float-right" href="'.$page->id.'#c-'.$content_element->uuid.'" target="__blank" rel="noopener noreferrer nofollow">'.$page->name.'</a></p>';
            $body .= '<p>'.$this->faker->sentence.' <a class="button float-right" href="'.$page->id.'#c-'.$content_element2->uuid.'" target="__blank" rel="noopener noreferrer nofollow">'.$page->name.'</a></p>';

            $content->{$link_field} = $body;
            $content->save();

            $content->refresh();

            $this->enableEditing();
            $this->assertTrue(editing());

            $this->assertTrue(Str::contains($content->{$link_field}, 'href="'.$page->id.'#c-'.$content_element->uuid.'"'));
            $this->assertFalse(Str::contains($content->{$link_field}, 'href="/'.$page->full_slug.'#c-'.$content_element->uuid.'"'));

            $this->assertTrue(Str::contains($content->{$link_field}, 'href="'.$page->id.'#c-'.$content_element2->uuid.'"'));
            $this->assertFalse(Str::contains($content->{$link_field}, 'href="/'.$page->full_slug.'#c-'.$content_element2->uuid.'"'));

            $this->disableEditing();
            $this->assertFalse(editing());

            // if not editing, the links should be parsed for the frontend
            $this->assertFalse(Str::contains($content->{$link_field}, 'href="'.$page->id.'#c-'.$content_element->uuid.'"'));
            //$this->assertTrue(Str::contains($content->{$link_field}, 'href="/'.$page->full_slug.'#c-'.$content_element->uuid.'"'));

            $this->assertFalse(Str::contains($content->{$link_field}, 'href="'.$page->id.'#c-'.$content_element2->uuid.'"'));
            //$this->assertTrue(Str::contains($content->{$link_field}, 'href="/'.$page->full_slug.'#c-'.$content_element2->uuid.'"'));
        }
    }

    /*
     *
     *
     *
     https://regex101.com/r/vcNxS1/1

     \<a.*?(href="(\d+)(\?.*?)?(#c-([^"]+))?").*?\>(?:[^\<]*)\<\/a>

    <a class="" href="191?modal=true#c-d8c99773-b205-404c-81dc-700da02b9c9d">Velit praesentium quia.</a>.
    <a class="" href="191?modal=true&foo=bar#c-d8c99773-b205-404c-81dc-700da02b9c9d">Velit praesentium quia.</a>.
    <a class="" href="16?modal=true#c-44688285-04ea-4f09-af4a-11c1634f1ffd">foobar</a>.
    <a class="" href="16#c-44688285-04ea-4f09-af4a-11c1634f1ffd">foobar</a>.
    <a class="" href="16?modal=true">foobar</a>.


    public function a_modal_can_be_linked()
    {
        $page = Page::factory()->create();

        $content_element_link = $this->createContentElement(TextBlock::factory(), $page);
        $pivot = $page->contentElements()->first()->pivot->toArray();
        $pivot['expandable'] = 1;
        $page->contentElements()->updateExistingPivot($content_element_link->id, $pivot);

        $content_element = $this->createContentElement(TextBlock::factory(), $page);

        $text_block = $content_element->content;

        $text_block->body = '<p>'.$this->faker->sentence.' <a class="" href="'.$page->id.'?modal=true#c-'.$content_element_link->uuid.'">'.$text_block->header.'</a></p>';
        $text_block->save();

        $page->publish();
        $page->refresh();
        $content_element->refresh();
        $text_block->refresh();

        $this->assertTrue(Str::contains($text_block->body, '@click.prevent="$store.dispatch(\'toggleModal\', \''.$content_element_link->uuid.'\')"'));
    }

    public function page_links_have_a_click_event_for_vue_to_expand_any_expanders()
    {
        $content = $this->getModel();
        $content_element1 = $content->contentElement;
        $this->assertInstanceOf(ContentElement::class, $content_element1);
        $page = Page::factory()->create();
        $page->publish();
        $this->assertNotNull($page->full_slug);
        $page->contentElements()->attach($content_element1, ['sort_order' => 1, 'unlisted' => false, 'expandable' => false, 'version_id' => $page->getDraftVersion()->id]);

        $content2 = $this->getModel();
        $content_element2 = $content2->contentElement;
        $content_element2->pages()->detach();
        $page->contentElements()->attach($content_element2, ['sort_order' => 1, 'unlisted' => false, 'expandable' => false, 'version_id' => $page->getDraftVersion()->id]);

        $page->refresh();

        $this->assertEquals(2, $page->contentElements->count());

        foreach ($this->getLinkFields() as $link_field) {
            $body = '<p>'.$this->faker->sentence.' <a class="button float-right" href="'.$page->id.'#c-'.$content_element1->uuid.'" rel="noopener noreferrer nofollow">'.$page->name.'</a></p>';
            $body .= '<p>'.$this->faker->sentence.' <a class="button float-right" href="'.$page->id.'#c-'.$content_element2->uuid.'" rel="noopener noreferrer nofollow">'.$page->name.'</a></p>';

            $content->{$link_field} = $body;
            $content->save();

            $content->refresh();

            $this->enableEditing();

            $this->assertFalse(Str::contains($content->{$link_field}, '@click.prevent="$store.dispatch(\'checkForExpander\', { uuid: \''.$content_element1->uuid.'\', slug: \''.$page->full_slug.'\' })"'));
            $this->assertFalse(Str::contains($content->{$link_field}, '@click.prevent="$store.dispatch(\'checkForExpander\', { uuid: \''.$content_element2->uuid.'\', slug: \''.$page->full_slug.'\' })"'));

            $this->disableEditing();

            // if not editing, the links should be parsed for the frontend
            $this->assertTrue(Str::contains($content->{$link_field}, '@click.prevent="$store.dispatch(\'checkForExpander\', { uuid: \''.$content_element1->uuid.'\', slug: \''.$page->full_slug.'\' })"'));
            $this->assertTrue(Str::contains($content->{$link_field}, '@click.prevent="$store.dispatch(\'checkForExpander\', { uuid: \''.$content_element2->uuid.'\', slug: \''.$page->full_slug.'\' })"'));
        }
    }
     */
}
