<?php
namespace front\controls;

class Site
{
  static public function page_title()
  {
    $class = '\front\controls\\';

    $results = explode( '_', \Shared\Helpers\Helpers::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $property = \Shared\Helpers\Helpers::get( 'action' );
    if ( class_exists( $class ) and property_exists( new $class, 'page_title' ) )
      return $class::$title[$property];
  }

  static public function title()
  {
    global $settings;

    $class = '\front\controls\\';

    $results = explode( '_', \Shared\Helpers\Helpers::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $property = \Shared\Helpers\Helpers::get( 'action' );
    if ( class_exists( $class ) and property_exists( new $class, 'title' ) )
      return $class::$title[$property] . ' | ' . $settings['firm_name'];
  }

  public static function route( $product = '', $category = '' )
  {
    global $page, $lang_id, $settings;

    $articleRepo = new \Domain\Article\ArticleRepository( $GLOBALS['mdb'] );

    if ( \Shared\Helpers\Helpers::get( 'article' ) )
      return \front\Views\Articles::fullArticle( $articleRepo->articleDetailsFrontend( (int)\Shared\Helpers\Helpers::get( 'article' ), $lang_id ) );

    // wyświetlenie pojedynczego produktu
    if ( $product )
    {
      \shop\Product::add_visit( $product -> id );
      return \Shared\Tpl\Tpl::view( 'shop-product/product', [
                'product' => $product,
                'settings' => $settings,
                'lang_id' => $lang_id,
                'settings' => $settings
              ] );
    }

    if ( $category )
      return \front\view\ShopCategory::category_view( $category, $lang_id, \Shared\Helpers\Helpers::get( 'bs' ) );

    // nowe kontrolery z DI
    $module = \Shared\Helpers\Helpers::get( 'module' );
    $action = \Shared\Helpers\Helpers::get( 'action' );
    $controllerFactories = self::getControllerFactories();

    $moduleName = implode( '', array_map( 'ucfirst', explode( '_', $module ) ) );
    if ( isset( $controllerFactories[$moduleName] ) and $action )
    {
      $controller = $controllerFactories[$moduleName]();
      if ( method_exists( $controller, $action ) )
        return $controller->$action();
    }

    // stare klasy
    $class = '\front\controls\\' . $moduleName;

    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );

    // klasy sklepowe
    $class = '\shop\\';

    $results = explode( '_', \Shared\Helpers\Helpers::get( 'module' ) );
    if ( is_array( $results ) ) foreach ( $results as $row )
      $class .= ucfirst( $row );

    $action = \Shared\Helpers\Helpers::get( 'action' );

    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );

    if ( $page['id'] )
    {
      $bs = (int)\Shared\Helpers\Helpers::get( 'bs' );
      $pageArticlesResult = $articleRepo->pageArticles( $page, $lang_id, $bs ?: 1 );
      $articlesForPage = [];
      if ( is_array( $pageArticlesResult['articles'] ) ) {
        foreach ( $pageArticlesResult['articles'] as $aid ) {
          $articlesForPage[] = $articleRepo->articleDetailsFrontend( (int)$aid, $lang_id );
        }
      }

      switch ( $page['page_type'] )
      {
        /* pełne artykuły */
        case 0:
          return \front\Views\Articles::fullArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
        break;

        /* wprowadzenia */
        case 1:
          return \front\Views\Articles::entryArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
        break;

        /* miniaturki */
        case 2:
          return \front\Views\Articles::miniatureArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
        break;

        /* strona kontaktu */
        case 4:
          $out = \front\Views\Articles::fullArticlesList( $articlesForPage, $pageArticlesResult['ls'], $bs ?: 1, $page );
          $out .= \front\view\Site::contact();
          return $out;
        break;
      }
    }
  }

  public static function check_url_params()
  {
    global $lang, $config;

    $a = \Shared\Helpers\Helpers::get( 'a' );

    switch ( $a )
    {
      case 'page':
        $page = \front\factory\Pages::page_details( \Shared\Helpers\Helpers::get( 'id' ) );
        \Shared\Helpers\Helpers::set_session( 'page', $page );
      break;

      case 'change_language':
        \Shared\Helpers\Helpers::set_session( 'current-lang', \Shared\Helpers\Helpers::get( 'id' ) );
        header( 'Location: /' );
        exit;
      break;
    }

    if ( \Shared\Helpers\Helpers::get( 'lang' ) )
      \Shared\Helpers\Helpers::set_session( 'current-lang', \Shared\Helpers\Helpers::get( 'lang' ) );

    if ( file_exists( 'modules/actions.php' ) )
      include 'modules/actions.php';
  }

  public static function getControllerFactories()
  {
    return [
      'Newsletter' => function() {
        global $mdb;
        return new \front\Controllers\NewsletterController(
          new \Domain\Newsletter\NewsletterRepository( $mdb )
        );
      },
    ];
  }
}
?>
