<?php

namespace App\Http\Controllers\Theming;

use App\Http\Controllers\Controller;
use App\Interfaces\PageRepositoryInterface;
use App\Services\AuthService;
use App\Services\SortingService;
use App\Services\AssetService;

class ThemeController extends Controller
{
    protected PageRepositoryInterface $pages;
    protected AuthService $auth;
    protected AssetService $assets;
    protected SortingService $sort;

    public function __construct(
        PageRepositoryInterface $pageRepository,
        AuthService $authService,
        AssetService $assetService,
        SortingService $sortService,
    ) {
        $this->pages  = $pageRepository;
        $this->auth   = $authService;
        $this->assets = $assetService;
        $this->sort   = $sortService;
    }

    public function getView($view = 'index')
    {
        //move assets to theme testing dir
        $this->assets->moveToTheme();
        $currentTheme = $this->assets->getCurrentTheme();
        $template;
        $pageData = [];
        switch ($view) {
            case "index":
            case "page":
                $view == 'index' ?
                    $template = $currentTheme . '.index' :
                    $template = $currentTheme . '.page';
                $page         = $this->pages->getById('532E2250-F8CB-4E87-9782-8AFBEE88DD5E');
                $data         = $this->sort->page($page);
                $pageData     = [
                    "debug"         => "true",
                    "theme"         => $currentTheme,
                    "status"        => $this->auth::status(),
                    "title"         => "THEME PAGE",
                    "meta"          => $data['meta'],
                    "menu"          => $data['menu'],
                    "info"          => $data['info'],
                    "media"         => $data['media'],
                    "files"         => $data['files'],
                    "content"       => $data['content'],
                    "recent"        => $data['recent'],
                    "feature"       => $data['featured'],
                    "tags"          => $data['tags'],
                    "dynamicRender" => $data['dynamicRender'],
                ];
                break;
            case "tags":
                $template = $currentTheme . '.tags';
                $data     = $this->sort->tags();
                $pageData = [
                    'debug'         => true, // for theme kit
                    'theme'         => $currentTheme, // for theme kit
                    'title'         => 'Pages Tagged as Tag',
                    'dynamicRender' => $data['info']['dynamicRender'],
                    '$pages'        => $data['info']['tags'][3]['pages'],
                    'info'          => $data['info'],
                    'menu'          => $data['info']['menu'],
                    'media'         => [
                        ['file'    => $data['info']['image'],
                            'type' => trim(pathinfo($data['info']['image'], PATHINFO_EXTENSION))]
                    ],
                ];
                break;
            case "archives":
            case "archive":
                $template = $currentTheme . '.archive';
                $data     = $this->sort->archive();
                $pageData = [
                    'debug'         => true, // for theme kit
                    'theme'         => $currentTheme, // for theme kit
                    'title'         => 'Archives',
                    'dynamicRender' => $data['info']['dynamicRender'],
                    'archive'       => $data['archive'],
                    'info'          => $data['info'],
                    'menu'          => $data['info']['menu'],
                    'media'         => [
                        ['file'    => $data['info']['image'],
                            'type' => trim(pathinfo($data['info']['image'], PATHINFO_EXTENSION))]
                    ],
                ];
                break;
        }
        if ($this->auth::status()) {
            return view($template, $pageData);
        } else {
            return redirect('dashboard/start');
        }
    }
}