<?php

class FormManager
{
  public array $Classes;
  public array $FormTypes;
  public Database $Database;
  public Type $Type;
  public string $RootURL;
  public bool $ShowRelation;

  function __construct(Database $Database)
  {
    $this->Database = &$Database;
    $this->Classes = array();
    $this->FormTypes = array();
    $this->Type = new Type($this);
    $this->ShowRelation = false;
  }

  function RegisterClass(string $Name, array $Class): void
  {
    $this->Classes[$Name] = $Class;
  }

  function UnregisterClass(string $Name): void
  {
    unset($this->Classes[$Name]);
  }

  function RegisterFormType(string $Name, array $Class): void
  {
    $this->FormTypes[$Name] = $Class;
  }

  function UnregisterFormType(string $Name): void
  {
    unset($this->FormTypes[$Name]);
  }

  function UpdateSQLMeta(): void
  {
    $this->Database->query('DELETE FROM ModelField');
    $this->Database->query('DELETE FROM Model');
    $this->Database->query('DELETE FROM DataType WHERE Parent IS NOT NULL');
    $this->Database->query('DELETE FROM DataType');

    foreach ($this->Type->TypeDefinitionList as $Name => $Type)
    {
      $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Name.'"');
      if ($DbResult->num_rows == 0)
      {
        $this->Database->insert('DataType', array('Name' => $Name,
          'Title' => $Type['Class']));
      } else
      {
        $DbRow = $DbResult->fetch_assoc();
        $this->Database->update('DataType', 'Id='.$DbRow['Id'], array('Name' => $Name,
          'Title' => $Type['Class']));
      }
    }

    foreach ($this->Classes as $Class)
    if (!array_key_exists('SQL', $Class) and ($Class['Table'] != ''))
    {
      if (!$this->Database->TableExists($Class['Table'])) continue;

      echo($Class['Table'].'<br/>');
      $Module = 1;
      $DbResult = $this->Database->select('Model', 'Id', 'Name="'.$Class['Table'].'"');
      if ($DbResult->num_rows == 0)
      {
        $this->Database->insert('Model', array('Name' => $Class['Table'], 'Title' => $Class['Title'], 'Module' => $Module));
        $Model = $this->Database->insert_id;
      } else
      {
        $DbRow = $DbResult->fetch_assoc();
        $Model = $DbRow['Id'];
        $this->Database->update('Model', 'Id='.$DbRow['Id'], array('Name' => $Class['Table'],
          'Title' => $Class['Title'], 'Module' => $Module));
      }

      foreach ($Class['Items'] as $Name => $Field)
      {
        echo($Name.', ');
        $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Field['Type'].'"');
        if ($DbResult->num_rows > 0)
        {
          $DbRow = $DbResult->fetch_assoc();
          $Type = $DbRow['Id'];
        } else {
          $Type = $this->FormTypes[$Field['Type']];

          // Search parent type
          $DbResult = $this->Database->select('DataType', 'Id', 'Name="'.$Type['Type'].'"');
          if ($DbResult->num_rows > 0)
          {
            $DbRow = $DbResult->fetch_assoc();
            $ParentType = $DbRow['Id'];
          } else $ParentType = null;

          $this->Database->insert('DataType', array('Name' => $Field['Type'],
            'Title' => '', 'Parent' => $ParentType));
          $Type = $this->Database->insert_id;
        }

        $DbResult = $this->Database->select('ModelField', 'Id', '(Name="'.$Name.'") AND (Model='.$Model.')');
        if ($DbResult->num_rows == 0)
        {
          $this->Database->insert('ModelField', array('Name' => $Name,
            'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
        } else
        {
          $DbRow = $DbResult->fetch_assoc();
          $this->Database->update('ModelField', 'Id='.$DbRow['Id'], array('Name' => $Name,
            'Title' => $Field['Caption'], 'Model' => $Model, 'Type' => $Type));
        }
      }
      echo('<br/>');
    }
  }
}
