<?php


namespace App\Tables;


class CStructArrayExporter extends BaseExporter
{
    /**
     * @return string - mime type for the downloaded file
     */
    protected function getMimeType()
    {
        return  $this->wantDownload ? 'text/x-csrc' : 'text/plain';
    }

    /**
     * @return string - file extension for the downloaded file
     */
    protected function getFileExtension()
    {
        return 'c';
    }

    protected function cify($name)
    {
        $name = preg_replace('/[^a-z0-9_]/i', '_', $name);
        if (ctype_digit($name[0])) {
            $name = '_' . $name;
        }
        return $name;
    }

    /**
     * Write the document to stdout ('php://output')
     */
    protected function writeDocument()
    {
        $table = $this->table;

        $any_bool = false;
        $fields = array_map(function (Column $c) use (&$any_bool) {
            $type = $c->type;
            if ($type == 'string') {
                $type = 'const char *';
            }

            $name = $this->cify($c->name);

            if ($c->type == 'bool') $any_bool = true;

            return (object)['name' => $c->name, 'type' => $c->type, 'ctype' => $type, 'cname' => $name];
        }, $this->columns);

        $ctablename = $this->cify($table->name);

        // preamble
        if ($any_bool) {
            echo "#include <stdbool.h>\n\n";
        }

        echo "/*\n" . $this->getHeaderComment(' ') . "*/\n\n";

        echo "struct " . $ctablename . " {\n";
        foreach ($fields as $field) {
            echo "    " . $field->ctype . " " . $field->cname . ";\n";
        }
        echo "};\n\n";

        // Now the table itself
        echo "const struct ".$ctablename.' tbl_'.$ctablename."[] = {\n";

        $first = true;
        foreach ($this->iterateRows() as $row) {
            if ($first) {
                $first = false;
            } else {
                echo ",\n";
            }

            echo "    { ";
            $this->dumpRow($fields, $row);
            echo " }";
        }

        echo "\n};\n";
    }

    protected function dumpRow($fields, $row, $noQuoteCols=[], $spaces = true)
    {
        $firstf = true;
        foreach ($fields as $field) {
            if ($firstf) {
                $firstf = false;
            } else {
                if ($spaces)
                    echo ", ";
                else
                    echo ",";
            }

            $val = $row[$field->name];

            // export to C format
            switch ($field->type) {
                case 'string':
                    if (in_array($field->name, $noQuoteCols)) {
                        echo $val;
                    } else {
                        echo '"' . addcslashes($val, "\0..\37!@\@\177..\377") . '"';
                    }
                    break;

                case 'bool':
                    echo (int)$val;
                    break;

                case 'int':
                case 'float':
                    echo $val;
                    break;
            }
        }
    }
}