<?php

namespace ReloadifyScoped\Faker\ORM\Propel;

class ColumnTypeGuesser
{
    protected $generator;
    public function __construct(\ReloadifyScoped\Faker\Generator $generator)
    {
        $this->generator = $generator;
    }
    /**
     * @return \Closure|null
     */
    public function guessFormat(\ReloadifyScoped\ColumnMap $column)
    {
        $generator = $this->generator;
        if ($column->isTemporal()) {
            if ($column->isEpochTemporal()) {
                return static function () use($generator) {
                    return $generator->dateTime;
                };
            }
            return static function () use($generator) {
                return $generator->dateTimeAD;
            };
        }
        $type = $column->getType();
        switch ($type) {
            case \ReloadifyScoped\PropelColumnTypes::BOOLEAN:
            case \ReloadifyScoped\PropelColumnTypes::BOOLEAN_EMU:
                return static function () use($generator) {
                    return $generator->boolean;
                };
            case \ReloadifyScoped\PropelColumnTypes::NUMERIC:
            case \ReloadifyScoped\PropelColumnTypes::DECIMAL:
                $size = $column->getSize();
                return static function () use($generator, $size) {
                    return $generator->randomNumber($size + 2) / 100;
                };
            case \ReloadifyScoped\PropelColumnTypes::TINYINT:
                return static function () use($generator) {
                    return $generator->numberBetween(0, 127);
                };
            case \ReloadifyScoped\PropelColumnTypes::SMALLINT:
                return static function () use($generator) {
                    return $generator->numberBetween(0, 32767);
                };
            case \ReloadifyScoped\PropelColumnTypes::INTEGER:
                return static function () use($generator) {
                    return $generator->numberBetween(0, 2147483647);
                };
            case \ReloadifyScoped\PropelColumnTypes::BIGINT:
                return static function () use($generator) {
                    return $generator->numberBetween(0, \PHP_INT_MAX);
                };
            case \ReloadifyScoped\PropelColumnTypes::FLOAT:
            case \ReloadifyScoped\PropelColumnTypes::DOUBLE:
            case \ReloadifyScoped\PropelColumnTypes::REAL:
                return static function () use($generator) {
                    return $generator->randomFloat();
                };
            case \ReloadifyScoped\PropelColumnTypes::CHAR:
            case \ReloadifyScoped\PropelColumnTypes::VARCHAR:
            case \ReloadifyScoped\PropelColumnTypes::BINARY:
            case \ReloadifyScoped\PropelColumnTypes::VARBINARY:
                $size = $column->getSize();
                return static function () use($generator, $size) {
                    return $generator->text($size);
                };
            case \ReloadifyScoped\PropelColumnTypes::LONGVARCHAR:
            case \ReloadifyScoped\PropelColumnTypes::LONGVARBINARY:
            case \ReloadifyScoped\PropelColumnTypes::CLOB:
            case \ReloadifyScoped\PropelColumnTypes::CLOB_EMU:
            case \ReloadifyScoped\PropelColumnTypes::BLOB:
                return static function () use($generator) {
                    return $generator->text;
                };
            case \ReloadifyScoped\PropelColumnTypes::ENUM:
                $valueSet = $column->getValueSet();
                return static function () use($generator, $valueSet) {
                    return $generator->randomElement($valueSet);
                };
            case \ReloadifyScoped\PropelColumnTypes::OBJECT:
            case \ReloadifyScoped\PropelColumnTypes::PHP_ARRAY:
            default:
                // no smart way to guess what the user expects here
                return null;
        }
    }
}
