<?php
namespace front;

class App
{
  static public function pageTitle()
  {
    $moduleName = implode( '', array_map( 'ucfirst', explode( '_', \Shared\Helpers\Helpers::get( 'module' ) ) ) );
    $action = \Shared\Helpers\Helpers::get( 'action' );
    $actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );

    $controllerClass = '\front\Controllers\\' . $moduleName . 'Controller';
    if ( class_exists( $controllerClass ) and property_exists( $controllerClass, 'title' ) and isset( $controllerClass::$title[$actionCamel] ) )
      return $controllerClass::$title[$actionCamel];

    $class = '\front\controls\\' . $moduleName;
    if ( class_exists( $class ) and property_exists( new $class, 'page_title' ) )
      return $class::$title[$action];
  }

  static public function title()
  {
    global $settings;

    $moduleName = implode( '', array_map( 'ucfirst', explode( '_', \Shared\Helpers\Helpers::get( 'module' ) ) ) );
    $action = \Shared\Helpers\Helpers::get( 'action' );
    $actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );

    $controllerClass = '\front\Controllers\\' . $moduleName . 'Controller';
    if ( class_exists( $controllerClass ) and property_exists( $controllerClass, 'title' ) and isset( $controllerClass::$title[$actionCamel] ) )
      return $controllerClass::$title[$actionCamel] . ' | ' . $settings['firm_name'];

    $class = '\front\controls\\' . $moduleName;
    if ( class_exists( $class ) and property_exists( new $class, 'title' ) )
      return $class::$title[$action] . ' | ' . $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 )
    {
      ( new \Domain\Product\ProductRepository( $GLOBALS['mdb'] ) )->addVisit( (int)$product['id'] );
      return \Shared\Tpl\Tpl::view( 'shop-product/product', [
                'product' => $product,
                'settings' => $settings,
                'lang_id' => $lang_id,
                'settings' => $settings
              ] );
    }

    if ( $category )
      return \front\Views\ShopCategory::categoryView( $category, $lang_id, (int)\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]();
      $actionCamel = lcfirst( implode( '', array_map( 'ucfirst', explode( '_', $action ) ) ) );
      if ( method_exists( $controller, $actionCamel ) )
        return $controller->$actionCamel();
    }

    // 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() );

    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\LayoutEngine::contact();
          return $out;
        break;
      }
    }
  }

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

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

    switch ( $a )
    {
      case 'page':
        global $lang_id;
        $pagesRepo = new \Domain\Pages\PagesRepository( $GLOBALS['mdb'] );
        $page = $pagesRepo->frontPageDetails( \Shared\Helpers\Helpers::get( 'id' ), $lang_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 )
        );
      },
      'ShopBasket' => function() {
        global $mdb;
        return new \front\Controllers\ShopBasketController(
          new \Domain\Order\OrderRepository( $mdb ),
          new \Domain\PaymentMethod\PaymentMethodRepository( $mdb )
        );
      },
      'ShopClient' => function() {
        global $mdb;
        return new \front\Controllers\ShopClientController(
          new \Domain\Client\ClientRepository( $mdb )
        );
      },
      'ShopCoupon' => function() {
        global $mdb;
        return new \front\Controllers\ShopCouponController(
          new \Domain\Coupon\CouponRepository( $mdb )
        );
      },
      'ShopOrder' => function() {
        global $mdb;
        $orderRepo = new \Domain\Order\OrderRepository( $mdb );
        $cronJobRepo = new \Domain\CronJob\CronJobRepository( $mdb );
        return new \front\Controllers\ShopOrderController(
          $orderRepo,
          new \Domain\Order\OrderAdminService( $orderRepo, null, null, null, $cronJobRepo )
        );
      },
      'ShopProducer' => function() {
        global $mdb;
        return new \front\Controllers\ShopProducerController(
          new \Domain\Producer\ProducerRepository( $mdb )
        );
      },
      'ShopProduct' => function() {
        global $mdb;
        return new \front\Controllers\ShopProductController(
          new \Domain\Category\CategoryRepository( $mdb )
        );
      },
      'Search' => function() {
        return new \front\Controllers\SearchController();
      },
    ];
  }
}
