<?php

namespace Shm\ShmUtils\ShmDoctor\ClassGenerator;

use Shm\Collection\Collection;
use Shm\ShmUtils\ShmInit;


class FieldClassGenerator
{

    private static function getShmStructures($dir, $namespacePrefix): array
    {
        $classes = [];

        if (is_dir($dir)) {;
            $files = scandir($dir);

            foreach ($files as $file) {
                if (!in_array($file, ['.', '..']) && pathinfo($file, PATHINFO_EXTENSION) === 'php') {
                    $className = str_replace('.php', '', $file);
                    $fullClassName = $namespacePrefix . '\\' . $className;


                    if (!class_exists($fullClassName)) {
                        continue;
                    }

                    $class = new $fullClassName();
                    if (!$class instanceof Collection) {
                        continue;
                    }

                    if ($class->isShmCollection) {
                        continue;
                    }

                    $classes[] = $class;
                }
            }

            return $classes;
        }

        return [];
    }

    public static function _shmFieldClassGenerator(): void
    {

        $dirs = [
            '/ShmPayment/TBankPayment/TBankCollections' => 'Shm\ShmPayment\TBankPayment\TBankCollections',
            '/ShmSocial' => 'Shm\ShmSocial',
            '/ShmChat/SchemaChat' => 'Shm\ShmChat\SchemaChat',
            '/ShmAddons/EventTicketAddon/Types/HallType' => 'Shm\ShmAddons\EventTicketAddon\Types\HallType',
            '/ShmAdmin/SchemaCollections' => 'Shm\ShmAdmin\SchemaCollections'

        ];

        foreach ($dirs as $dir => $namespacePrefix) {
            $_dir = ShmInit::$shmDir . $dir;

            $structures = self::getShmStructures($_dir, $namespacePrefix);

            foreach ($structures as $structure) {
                $fieldFileGenerator = new FieldFileGenerator();
                $fieldFileGenerator->setDir($_dir);
                $fieldFileGenerator->setNamespace($namespacePrefix);
                $fieldFileGenerator->setCollectionClass($structure::class);
                $fieldFileGenerator->setStructure($structure::structure());
                $fieldFileGenerator->generate();
            }
        }

        //  echo ShmInit::$shmDir;
    }
    /**
     * Generate field classes for all structures
     */

    public static function generateAll(): void
    {
        $structures = \Shm\ShmUtils\ShmDoctor\Utils\StructureHelper::getStructures();
        $dir = ShmInit::$rootDir . '/app/FieldClasses/';

        foreach ($structures as $structure) {
            echo $structure::class . PHP_EOL;
            $fieldFileGenerator = new FieldFileGenerator();
            $fieldFileGenerator->setDir($dir);
            $fieldFileGenerator->setNamespace('App\FieldClasses');
            $fieldFileGenerator->setCollectionClass($structure::class);
            $fieldFileGenerator->setStructure($structure::structure());
            $fieldFileGenerator->generate();
        }
    }
}
