<?php namespace App\Libs\Composers;

use App\Libs\Facades\Localization;
use App\Models\Hp2TemplateConfiguration;
use App\Models\PardotForm;

class Home2TemplateComposer
{
    public function compose($view)
    {
        $data = $view->getData();
        $page = $data['object'];
        $website = Localization::getWebsite();

        $templateData = Hp2TemplateConfiguration::with(array(
            'heroImages' => function ($q) {
                $q->orderByRaw('RAND()')->limit(1);
            }, 'mobileHeroImages' => function ($q) {
                $q->orderByRaw('RAND()')->limit(1);
            }, 'clientLogos' => function ($q) {
                $q->orderBy("hp2_client_logos.position", "asc");
            }, 'blocks1' => function ($q) {
                $q->full()->with('image')->orderBy("website_objects.position", "asc");
            }, 'blocks2' => function ($q) {
                $q->full()->with('image')->orderBy("website_objects.position", "asc");
            }, 'video', 'teamImage', 'teamMobileImage'
        ))->where('entry_id', $page->id)->first();

        $newsletterForm = PardotForm::full()->where('lang_slug', 'simple-newsletter-form')->first();

        $heroOverlayStyles = '';
        if ($templateData->overlay_opacity) {
            $opacity = $templateData->overlay_opacity / 100;
            $heroOverlayStyles .= "opacity: $opacity;";
            if ($templateData->overlay_color) {
                $heroOverlayStyles .= "background-color: $templateData->overlay_color;";
            }
        }

        $view->with(array(
            'homeData' => $templateData,
            'newsletterForm' => $newsletterForm,
            'heroOverlayStyles' => $heroOverlayStyles
        ));
    }
}
