<?php

namespace App\Services\Registries;

use App\Models\Registries\Category;
use App\Models\Registries\Entry;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use PHPHtmlParser\Dom;

class RulesetImportService extends RegistryImportService {

    public function test() {
        $res = [];
        $nodes = $this->dom->find('#sp_entyity *')->toArray();
        foreach ($nodes as $node) {
            if ($node->tag->name() === 'div') {
                $items = $node->find('li');
                foreach ($items as $k => $item) {
                    $res[] = trim($item->text, '«» ');
                    //if ($k >= 5) return;
                }
            }
        }
        $res = collect(array_count_values($res))->filter(function($count) {
            return $count > 1;
        });

        var_dump($res);
    }

    public function import() {
        $category = null;
        $subcategory = null;
        $nodes = $this->dom->find('#sp_entyity *')->toArray();
        foreach ($nodes as $node) {
            $name = trim($node->text, '«» ');
            if ($node->tag->name() === 'h2') {
                $category = $this->registry->addCategory($name);
                $subcategory = null;
            } elseif ($node->tag->name() === 'h3') {
                if ($category) $subcategory = $category->addCategory($name);
            }
            elseif ($node->tag->name() === 'div') {
                $items = $node->find('li');
                foreach ($items as $k => $item) {
                    $entryName = trim($item->text, '«» ');
                    if (!$this->registry->entries()->where(['name' => $entryName])->exists()) {
                        echo ("Importing {$entryName}\n");
                        $link = $item->find('a', 0);
                        $this->importItem("{$this->url}{$link->href}", $subcategory, $entryName);
                    } else echo("Already imported {$entryName}\n");
                    //if ($k >= 5) return;
                }
            }
        }
    }

    public function importItem(string $url, Category $category, $name) {
        $dom = new Dom;
        $dom->loadFromUrl($url);
        $link = $dom->find('p.ACTUALNAME a', 0);
        list($number) = explode('«', $link->text);
        $number = trim($number);
        $entry = $category->entries()->firstOrCreate(['registry_id' => $category->registry_id, 'number' => $number]);
        $entry->update(['name' => $name]);
        $this->importOperations($entry, array_slice($dom->find('table.inc1 tr')->toArray(), 1));
    }

    public function importOperations(Entry $entry, array $operations) {
        foreach ($operations as $operation) {
            $this->importOperation($entry, $operation);
        }
        $entry->sortOperations();
    }

    public function importOperation(Entry $entry, $node) {
        list($col1, $col2, $col3, $col4, $col5) = $node->find('td')->toArray();

        $type = trim(Str::replace(explode(' ', $entry->number), '', $col1->text ?? ''), ': ');
        $data['operation-type'] = ['title' => explode(' ', $type)[0]];
        $data['active-since'] = $col3->text ? Date::create($col3->text) : null;
        $data['active-till'] = $col4->text ? Date::create($col4->text) : null;
        $data['developer'] = Str::replace('&quot;', '"', $col5->text ?? null);
        $data['listings'] = [];
        $data['order-name'] = $data['order-date'] = $data['order-document'] = null;
        $data = $this->parseColumn2($col2, $data);

        $object = $entry->operations()->applyFilters(['order-name' => $data['order-name'] ?? null])->first();
        if (!$object) $object = $entry->createObject($entry->registry->options['operations'] ?? null, null, 'operations');
        $object->setValues($data);
    }

    public function parseColumn2($node, $data): array {
        $links = $node->find('a')->toArray();
        if ($links) {
            foreach ($node->find('a')->toArray() as $link) {
                if (count(explode(' от ', $link->text)) === 2) {
                    list($orderName, $orderDate) = explode(' от ', $link->text);
                    $data['order-name'] = $orderName;
                    $data['order-date'] = $orderDate ? Date::create($orderDate) : null;
                    $filename = $orderName ? Str::slug("Приказ {$orderName} от {$orderDate}") : null;
                    $data['order-document'] = $this->download($link->href, 'registries/ruleset', $filename);
                } else $data['listings'][] = ['name' => Str::replace('Постановление правительства №', 'pp', $link->text)];
            }
        } else {
            list($orderName, $orderDate) = explode(' от ', $node->text);
            $data['order-name'] = $orderName;
            $data['order-date'] = $orderDate ? Date::create($orderDate) : null;
        }
        return $data;
    }


}