<?php

namespace App\Services\Registries;

use Illuminate\Support\Str;

class DevelopmentsImportService extends RegistryImportService {

    public function test() {
    }

    public function import() {
        $nodes = $this->dom->find('#part2 tbody tr')->toArray();
        foreach ($nodes as $node) {
            list($num, $name, $year, $developer, $funding) = $node->find('td')->toArray();
            $type = $this->getOperationType($name->text);
            $category = $this->registry->addCategory(trim($year->text));
            $entry = $category->entries()->firstOrCreate(['registry_id' => $category->registry_id, 'name' => trim($name->text)]);
            $entry->properties->setValues(['operation-type' => ['title' => $type], 'primary-developer' => trim($developer->text),
                'funding-source' => ['title' => trim($funding->text)], 'plan-year' => intval($year->text)]);
        }
    }

    public function getOperationType($name) {
        $types = ['Пересмотр', 'Изменение'];
        $result = 'Разработка';
        foreach ($types as $type) {
            if (str_contains(Str::lower($name), Str::lower($type))) $result = $type;
        }
        return $result;
    }


}