<?php

use Eae\Backoffice\Generic\ContentType;
use Eae\Database\Eloquent\Collection\ContentTypeCollection;

abstract class ContentTypeModel extends BaseModel {
    
    protected $websiteAttributes = array("website_object_id", "slug", "uri", "lang_slug", "lang_uri", "template", "status", "position", "content_type");
    protected $related = array();
    
    public function websites(){
        return $this->morphMany('WebsiteObject', "objectable", "object_type", "object_id");        
    }
    
    public function scopeFull($query){
        $query->setEagerLoads(array());
        $languageRelation = $this->languages();
        $languageTable = $languageRelation->getRelated()->getTable();
        $languageFK = $languageRelation->getForeignKey();
        $lang = LaravelLocalization::getCurrentLocale();
        $query->join($languageTable, function($join) use ($languageTable, $languageFK, $lang){
            $join->on("{$this->getTable()}.id", "=", "{$languageFK}")
                ->where("{$languageTable}.language_id", "=", $lang);
        });
        
        $websiteRelationship = $this->websites(); 
        $websiteObjectModel = $websiteRelationship->getRelated();
        $websiteObjectFK = $websiteRelationship->getForeignKey();
        $model = $this;

        $query->join($websiteObjectModel->getTable(), 
            function($join) use ($model, $websiteObjectFK, $websiteRelationship){
                $join->on($model->getQualifiedKeyName(), "=", $websiteObjectFK)
                    ->where($websiteRelationship->getMorphType(), "=", class_basename($model));      
            });
            
        $query->join('website_objects_lang', 
            function($join) use ($lang){
                $join->on('website_objects_lang.website_object_id', "=", 'website_objects.website_object_id')
                    ->where('website_objects_lang.wo_language_id', "=", $lang); 
            });
        
        return $query;
    }
    
    public function scopeWebsite($query){
        $query->setEagerLoads(array_except($query->getEagerLoads(), "websites"));
        
        
        $websiteRelationship = $this->websites(); 
        $websiteObjectModel = $websiteRelationship->getRelated();
        $websiteObjectFK = $websiteRelationship->getForeignKey();
        $model = $this;
        
        $query->join($websiteObjectModel->getTable(), 
            function($join) use ($model, $websiteObjectFK, $websiteRelationship){
                $join->on($model->getQualifiedKeyName(), "=", $websiteObjectFK)
                    ->where($websiteRelationship->getMorphType(), "=", class_basename($model));      
            });
        
        return $query;
            
    }

    public function getAttribute($key){ 
        if(parent::keyExists($key)){
            return parent::getAttribute($key);    
        }

        $inWebsiteAttributes = in_array($key, $this->websiteAttributes);
        if($this->exists && $inWebsiteAttributes){
            $websiteEntry = $this->getActiveWebsiteEntry();

            if(!empty($websiteEntry) && !empty($websiteEntry->$key)){
                return $websiteEntry->$key;        
            }
            
           
        }
        
        return null;   
    }
    
    public function getActiveWebsiteEntry(){
        $websitesData = $this->websites; 
        if(empty($websitesData)){
            return null;
        }
        return $websitesData->first(); 
    } 
    
    public function related($type, $order = "position", $withPerType = array()){
        if(isset($this->related[$type])){
            return $this->related[$type];
        }
        
        $activeWebsite = $this->getActiveWebsiteEntry();
        
        if(empty($activeWebsite)){
            $this->related[$type] = null;    
            return $this->related[$type];
        }
        $query = $activeWebsite->related()->wherePivot("type", $type);
        if($order == "position"){
            $query->orderBy("related.position", "asc");        
        }
        
        $objects = $query->get(); 
        
        $objects->load("objectable");
        
        foreach($withPerType as $objectType => $with){    
            load_polymorphic_dependencies($objects, "objectable", $objectType, $with,"object_id", "object_type");
        }
        
        $objects = $objects->getObjectables(); 

        if($order != "position"){
            $objects = $objects->sortBy(function($model){
                return $model->title;    
            });
        }

        $this->related[$type] = $objects;
        return $this->related[$type]; 

    }
    
    public function setRelated($type, $collection){
        $this->related[$type] = $collection;    
    }
    
    public function toArray()
    {
        $array = parent::toArray();
        if(isset($this->relations["websites"])){
            foreach($this->websiteAttributes as $websiteAttribute){
                if(!in_array($websiteAttribute, $this->hidden)){
                    $array[$websiteAttribute] = $this->$websiteAttribute; 
                    if($array[$websiteAttribute] instanceof DateTime){
                        $array[$websiteAttribute] = $array[$websiteAttribute]->format($this->getDateFormat());
                    }       
                }
            }
        }
        return $array;
    }
    
    public function newCollection(array $models = array()){
        return new ContentTypeCollection($models);        
    }
    
    public function keyExists($key){
        return parent::keyExists($key) || in_array($key, $this->websiteAttributes);
    } 

    

}

