<?php namespace App\Libs\Composers;

use LandingPageCfg;

class LandingPageComposer
{

    public function compose($view)
    {
        $viewData = $view->getData();
        $page = $viewData['object'];

        $data = LandingPageCfg::with(array(
            'landingPageTiles' => function ($q) {
                $q->full()->with(['cobenefit', 'contact'])->orderBy("website_objects.position", "asc");
            }, 'bgImage', 'leftBoxBgImage', 'rightBoxBgImage'
        ))->where('entry_id', $page->id)->first();


        //Inline styles
        $data->highlightBgStyles = "";
        if ($data->bgImage) {
            $data->highlightBgStyles .= "background: url('".getMediaUrl($data->bgImage->filename,
                    '1920x670')."') no-repeat; background-size: cover; background-position: center;";
        } else {
            if ($data->background_color) {
                $data->highlightBgStyles .= "background-color: ".$data->background_color.";";
            }
        }

        $data->leftBoxBgStyles = "";
        if ($data->leftBoxBgImage) {
            $data->leftBoxBgStyles .= "background: url('".getMediaUrl($data->leftBoxBgImage->filename,
                    '660x520')."') no-repeat; background-size: cover; background-position: center;";
        } else {
            if ($data->left_box_bg_color) {
                $data->leftBoxBgStyles .= "background-color: ".$data->left_box_bg_color.";";
            }
        }
        if ($data->left_box_opacity != null) {
            $data->leftBoxBgStyles .= 'opacity: '.($data->left_box_opacity / 100).';';
        }

        $data->rightBoxBgStyles = "";
        if ($data->rightBoxBgImage) {
            $data->rightBoxBgStyles .= "background: url('".getMediaUrl($data->rightBoxBgImage->filename,
                    '660x520')."') no-repeat; background-size: cover; background-position: center;";
        } else {
            if ($data->right_box_bg_color) {
                $data->rightBoxBgStyles .= "background-color: ".$data->right_box_bg_color.";";
            }
        }
        if ($data->right_box_opacity != null) {
            $data->rightBoxBgStyles .= 'opacity: '.($data->right_box_opacity / 100).';';
        }

        $cssStyles = "";
        $elements = ['p', 'h1', 'h2', 'h3', 'h4', 'h5'];
        $applyTo = ".landing-hero .landing-hero__box";
        if ($data->left_box_forecolor) {
            foreach ($elements as $i => $element) {
                if ($i > 0) {
                    $cssStyles .= ',';
                }
                $cssStyles .= $applyTo.'.landing-hero__box--left '.$element;
            }
            $cssStyles .= ' { color: '.$data->left_box_forecolor.'; } ';
        }
        if ($data->right_box_forecolor) {
            foreach ($elements as $i => $element) {
                if ($i > 0) {
                    $cssStyles .= ',';
                }
                $cssStyles .= $applyTo.'.landing-hero__box--right '.$element;
            }
            $cssStyles .= ' { color: '.$data->right_box_forecolor.'; } ';
        }

        $sections = [];
        $activeSection = null;

        foreach ($data->landingPageTiles as $tile) {
            if ($tile->type === 'content' || $tile->type === 'iframe') {
                if ($activeSection) {
                    $sections[] = $activeSection;
                    $activeSection = null;
                }
                $sections[] = (object) [
                    'type' => $tile->type,
                    'tile' => $tile
                ];
                continue;
            } else if ($tile->type === 'gallery') {
                if (!$activeSection || $activeSection->type !== 'gallery') {
                    if ($activeSection) {
                        $sections[] = $activeSection;
                    }
                    $activeSection = (object) [
                        'type' => 'gallery',
                        'items' => []
                    ];
                }
                $activeSection->items[] = $tile;
            } else {
                if (!$activeSection || $activeSection->type !== 'tiles') {
                    if ($activeSection) {
                        $sections[] = $activeSection;
                    }
                    $activeSection = (object) [
                        'type' => 'tiles',
                        'tiles' => []
                    ];
                }
                $tile->cols = 'single';
                $tile->colsSm = 'md-single';
                if ($tile->type === 'header') {
                    $activeSection->header = $tile;
                } else {
                    $activeSection->tiles[] = $tile;
                }
            }
        }
        if ($activeSection) {
            $sections[] = $activeSection;
        }

        foreach($sections as $section) {
            if ($section->type === 'content' || $section->type === 'gallery' || $section->type === 'iframe') {
                continue;
            }
            $tilesCount = sizeof($section->tiles);
            if ($tilesCount === 0) {
                continue;
            }
            $remaining = $tilesCount % 3;
            if ($remaining === 0) {
                $section->tiles[$tilesCount - 1]->colsSm = 'md-double';
            }
            if ($remaining === 1) {
                $section->tiles[$tilesCount - 1]->cols = 'triple';
                $section->tiles[$tilesCount - 1]->colsSm = 'md-double';
            }
            if ($remaining === 2) {
                $section->tiles[$tilesCount - 1]->cols = 'double';
                $section->tiles[$tilesCount - 2]->cols = 'double';
            }
        }

        $view->with(array(
            'data' => $data,
            'sections' => $sections,
            'css' => $cssStyles
            //  'metaData'       => $metaData
        ));
    }
}
