<?php

use App\Libs\Facades\Localization;
use Illuminate\Support\Facades\DB;

class Project extends ContentTypeModel implements CardableModel
{
    use LanguageSupportTrait;
    use BlocksTrait;

    protected $hidden = array('languages', "websites");
    protected $with = array("websites.languages", "languages");

    public function country()
    {
        return $this->belongsTo("Country");
    }

    public function technology()
    {
        return $this->belongsTo("Technology");
    }

    public function standard()
    {
        return $this->belongsTo("Standard");
    }

    public function overviewImage()
    {
        return $this->belongsTo("MediaItem");
    }

    public function brochure()
    {
        return $this->belongsTo("Publication");
    }

    public function cobenefits()
    {
        return $this->hasMany("ProjectCobenefit");
    }

    public function standards()
    {
        return $this->belongsToMany("Standard", "project_standards");
    }

    public function technologies()
    {
        return $this->belongsToMany("Technology", "project_technologies");
    }

    public function slides()
    {
        return $this->belongsToMany("MediaItem", "project_slides");
    }

    public function testimonials()
    {
        return $this->belongsToMany("Testimonial", "project_testimonial");
    }

    public function blocks()
    {
        return $this->belongsToMany(Block::class, 'project_blocks');
    }

    public function projectImages()
    {
        return $this->hasMany(ProjectImage::class);
    }

    public function projectLinks()
    {
        return $this->hasMany(ProjectLink::class);
    }

    public function scopeFilterByTopics($query, $topics)
    {
        return $query->whereExists(function ($query) use ($topics) {
            $query->select(DB::raw(1))
                ->from("related")
                ->join("website_objects as wo", "related.related_id", "=", "wo.website_object_id")
                ->join("sustainability_topics", "wo.object_id", "=", "sustainability_topics.id")
                ->whereRaw("related.website_object_id = website_objects.website_object_id")
                ->whereIn("sustainability_topics.id", $topics)
                ->where("related.type", "sustainability");
        });
    }

    public function scopeFilterByCobenefits($query, $cobenefits)
    {
        return $query->whereExists(function ($query) use ($cobenefits) {
            $query->select(DB::raw(1))
                ->from("project_cobenefits")
                ->whereRaw("project_cobenefits.project_id = projects.id")
                ->whereIn("project_cobenefits.cobenefit_id", $cobenefits);
        });
    }

    public function scopeFilterByTechnologies($query, $technologies)
    {
        return $query->whereExists(function ($query) use ($technologies) {
            $query->select(DB::raw(1))
                ->from("project_technologies")
                ->whereRaw("project_technologies.project_id = projects.id")
                ->whereIn("project_technologies.technology_id", $technologies);
        });
        //return $query->whereIn('technology_id', $technologies);
    }

    public function scopeFilterByCountries($query, $countries)
    {
        return $query->whereIn('country_id', $countries);
    }

    public function scopeFilterByStandards($query, $standards)
    {
        return $query->whereExists(function ($query) use ($standards) {
            $query->select(DB::raw(1))
                ->from("project_standards")
                ->whereRaw("project_standards.project_id = projects.id")
                ->whereIn("project_standards.standard_id", $standards);
        });
        //return $query->whereIn('standard_id', $standards);
    }

    public function scopeFilterByLocations($query, $locations)
    {
        return $query->whereExists(function ($query) use ($locations) {
            $query->select(DB::raw(1))
                ->from("countries")
                ->whereRaw("countries.id = projects.country_id")
                ->whereIn("countries.continent", $locations);
        });
    }

    public function scopeFilterByBounds($query, $bounds)
    {
        $swLat = floatval($bounds[0]);
        $swLng = floatval($bounds[1]);
        $neLat = floatval($bounds[2]);
        $neLng = floatval($bounds[3]);
        if ($neLng >= $swLng) {
            $query->where('location_lng', '>=', $swLng)
                ->where('location_lng', '<=', $neLng);
        } else {
            $query->where(function ($query) use ($neLng, $swLng) {
                $query->orWhere(function ($query) use ($swLng) {
                    $query->where('location_lng', '>=', $swLng)
                        ->where('location_lng', '<=', 180);
                });
                $query->orWhere(function ($query) use ($neLng) {
                    $query->where('location_lng', '>=', -180)
                        ->where('location_lng', '<=', $neLng);
                });
            });

        }
        $query->where('location_lat', '>=', $swLat)
            ->where('location_lat', '<=', $neLat);

        return $query;
    }

    public function scopeFilterByCfType($query, $cfType)
    {
        $query->where(function ($query) use ($cfType) {
            if (in_array('au', $cfType)) {
                $query->orwhere('cf_au', 1);
            }
            if (in_array('carbon', $cfType)) {
                $query->orwhere('cf_carbon', 1);
            }
            if (in_array('gold', $cfType)) {
                $query->orwhere('cf_goldpower', 1);
            }
        });
        return $query;
    }

    public function scopeFilterByLetter($query, $letter)
    {
        return $query->where("title", 'like', "{$letter}%");
    }

    public function scopeFilterByCountryLetter($query, $letter)
    {
        $lang = Localization::getActiveLocale();
        return $query->whereExists(function ($query) use ($letter, $lang) {
            $query->select(DB::raw(1))
                ->from("countries_lang")
                ->whereRaw("countries_lang.country_id = projects.country_id")
                ->where("countries_lang.language_id", $lang)
                ->where("countries_lang.title", 'like', "{$letter}%");
        });
    }

    public function sidebarWidgets()
    {
        return $this->belongsToMany('SidebarWidget', 'project_sb_widgets');
    }

    public function getCardFields()
    {
        $wo = $this->websites()->first();
        if ($wo->status != 'published' || $wo->lang_status != 'published') {
            return null;
        }
        $fieldsObject = new stdClass();
        $fieldsObject->subject = '';
        $fieldsObject->title = $this->title;
        $fieldsObject->excerpt = '<p>' . $this->reference_summary . '</p>';
        $fieldsObject->image = $this->overviewImage;
        $fieldsObject->image_alt = $this->title;
        $fieldsObject->cta = generateObjectLinkJson($this);
        $fieldsObject->cta2 = null;

        return $fieldsObject;
    }
}
