<?php

namespace App\Services\Registries;

use App\Models\Pages\Page;
use App\Models\Publications\PublicationType;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;

class NewsImportService extends RegistryImportService {
    public function test() {

    }

    public function import() {
        $page = Page::byUrl('/press-tsentr/novosti');
        $nodes = $this->dom->find('article.pressRoomNews_article')->toArray();
        foreach ($nodes as $node) {
            $pre = $node->find('pre', 0);
            $img = $node->find('img', 0);
            $asset = $this->download(Str::replace('http://faufcc.ru.opt-images.1c-bitrix-cdn.ru', 'https://faufcc.ru', $img->src), 'publications/news');
            $link = $node->find('header a', 0);
            $serialized = $pre->text;
            $name = trim(explode('[', explode('[NAME] =>', $serialized)[1])[0]);
            $published_at = trim(explode('[', explode('[ACTIVE_FROM] =>', $serialized)[1] ?? null)[0] ?? null);
            $excerpt = trim(explode('[', explode('[PREVIEW_TEXT] =>', $serialized)[1] ?? null)[0] ?? null);
            $content = $this->parseContent("https://faufcc.ru{$link->href}");
            $model = $page->publications()->firstOrCreate(['name' => $name]);
            $model->update(['type' => PublicationType::NEWS, 'published_at' => $published_at ? Date::create($published_at) : null,
                'excerpt' => $excerpt, 'slug' => Str::slug($name), 'poster_id' => $asset->id ?? null, 'is_published' => true]);
            $section = $model->getObject('page-section-html', 'sections');
            $section->setValue('html-required', $content);
        }
    }

    public function parseContent($url) {
        $dom = new Dom;
        $dom->loadFromUrl($url);
        $node = $dom->find('.user-container', 0);
        if (($v = $node->find('h1')) && $v->count()) $v->delete();
        if (($v = $node->find('img')) && $v->count()) $v->delete();
        return trim($node->innerHTML);
    }


}