<?php


namespace CodeGenerator\Code;


use Symfony\Component\Filesystem\Filesystem;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\DocBlock\Tag\GenericTag;
use Zend\Code\Generator\DocBlockGenerator;
use Zend\Code\Generator\FileGenerator;

abstract class AbstractClassFile extends FileGenerator
{
    /**
     * @var string
     */
    protected $kubernetesNamespace = 'Kubernetes\\';

    /**
     * @var ClassGenerator
     */
    protected $ClassGenerator;

    /**
     * @var string
     */
    protected $sourceFileDirectory;

    protected static $internalPhpTypes = [
        'void',
        'int',
        'float',
        'string',
        'bool',
        'array',
        'callable',
        'iterable',
        'object'
    ];


    protected function parseDescription(string $description): string
    {
        return preg_replace('/\*\//', '*\\/', $description);
    }

    protected function checkAndAddDeprecatedTag($description, DocBlockGenerator &$DocBlockGenerator): void
    {
        if (preg_match('/deprecated/', $description)) {
            $DocBlockGenerator->setTag(new GenericTag('deprecated'));
        }
    }

    protected function getUseAlias(string $fullClassName): string
    {
        if (0 !== strpos($fullClassName, '\\')) {
            $fullClassName = '\\' . $fullClassName;
        }

        $classInfo = explode('\\', $fullClassName);
        $className = array_pop($classInfo);

        if ($className == $this->ClassGenerator->getName()) {
            $className = 'The' . $className;
        }

        $classGeneratorUses = $this->ClassGenerator->getUses();
        $uses               = [];

        foreach ($classGeneratorUses as $use) {
            $useInfo = explode(' as ', $use);

            if(!isset($useInfo[1])){
                $useInfo[1] = $useInfo[0];
            }

            $uses[$useInfo[0]] = $useInfo[1];
        }

        if ($this->ClassGenerator->hasUse($fullClassName)) {
            return $uses[$fullClassName];
        }

        while (in_array($className, $uses)) {
            $className .= array_pop($classInfo);
        }

        $this->ClassGenerator->addUse($fullClassName, $className);

        return $className;
    }


    public function initFilename()
    {
        if (!$this->filename) {
            $this->setFilename($this->getSourceFileDirectory() .
                               $this->getNamespaceDirectory() .
                               $this->ClassGenerator->getName() .
                               '.php');
        }

        return $this->filename;
    }

    public function write()
    {
        $FileSystem = new Filesystem();
        $FileSystem->mkdir($this->getSourceFileDirectory() . $this->getNamespaceDirectory(), 0755);

        return parent::write(); // TODO: Change the autogenerated stub
    }


    /**
     * @return string
     */
    public function getSourceFileDirectory(): string
    {
        if (!$this->sourceFileDirectory) {
            $this->setSourceFileDirectory(realpath(__DIR__ . '/../../src/') . DIRECTORY_SEPARATOR);
        }

        return $this->sourceFileDirectory;
    }

    /**
     * @param string $sourceFileDirectory
     *
     * @return self
     */
    public function setSourceFileDirectory(string $sourceFileDirectory)
    {
        $this->sourceFileDirectory = $sourceFileDirectory;

        return $this;
    }

    protected function getNamespaceDirectory()
    {
        return str_replace('\\', DIRECTORY_SEPARATOR, ltrim($this->getNamespace(), '\\Kubernetes\\')) .
               DIRECTORY_SEPARATOR;
    }


}