<?php namespace App\Libs\Composers;

use HpTemplateConfiguration;
use Project;
use DB;
use Config;
use Lang;

class HomeTemplateComposer
{

    public function compose($view)
    {
        $data = $view->getData();
        $page = $data['object'];
        $website = \Localization::getWebsite();
        /* $templateData = HpTemplateConfiguration::with('homepageSlides.testimonial', 'homepageSlides.image')
            ->where('entry_id', $page->id)->first();
        */

        $templateData = HpTemplateConfiguration::with(array(
            'homepageSlides' => function ($q) {
                $q->full()->with('testimonial')->orderBy("website_objects.position", "asc");
            }, 'homepageTiles' => function ($q) {
                $q->full()->with('cobenefit')->orderBy("website_objects.position", "asc");
            }
        ))->where('entry_id', $page->id)->first();

        $projectGroups = array(
            'africa' => array(
                'markerType' => 'projectGroup',
                'id' => 'africa',
                'icon' => Config::get('path.frontend.images_path').'/projects_africa.jpg',
                'location_lat' => 7.710991655433229,
                'location_lng' => 19.86328125,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.africa')
            ),
            'europe' => array(
                'markerType' => 'projectGroup',
                'id' => 'europe',
                'icon' => Config::get('path.frontend.images_path').'/projects_europe.jpg',
                'location_lat' => 46.98025235521883,
                'location_lng' => 8.72314453125,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.europe')
            ),
            'north_america' => array(
                'markerType' => 'projectGroup',
                'id' => 'north_america',
                'icon' => Config::get('path.frontend.images_path').'/projects_north_america.jpg',
                'location_lat' => 38.27268853598097,
                'location_lng' => -105.29296875,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.north_america')
            ),
            'south_america' => array(
                'markerType' => 'projectGroup',
                'id' => 'south_america',
                'icon' => Config::get('path.frontend.images_path').'/projects_south_america.jpg',
                'location_lat' => -14.604847155053886,
                'location_lng' => -59.58984375,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.south_america')
            ),
            'asia' => array(
                'markerType' => 'projectGroup',
                'id' => 'asia',
                'icon' => Config::get('path.frontend.images_path').'/projects_asia.jpg',
                'location_lat' => 27.683528083787756,
                'location_lng' => 91.93359375,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.asia')
            ),
            'oceania' => array(
                'markerType' => 'projectGroup',
                'id' => 'oceania',
                'icon' => Config::get('path.frontend.images_path').'/projects_oceania.jpg',
                'location_lat' => -12.89748918375589,
                'location_lng' => 142.91015625,
                'zoom' => 4,
                'count' => 0,
                'label' => Lang::get('frontend.oceania')
            )
        );

        $projectsCount = DB::table('projects')
            ->select(DB::raw('count(*) as continent_count, countries.continent'))
            ->join('countries', 'countries.id', '=', 'projects.country_id')
            ->join('website_objects', 'website_objects.object_id', '=', 'projects.id')
            ->where('website_objects.website_id', '=', $website->id)
            ->where('website_objects.status', 'published')
            ->where('website_objects.object_type', 'Project')
            ->groupBy('countries.continent')
            ->get();

        foreach ($projectsCount as $count) {
            $projectGroups[$count->continent]['count'] = $count->continent_count;
        }

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