<?php namespace Eae\Backoffice\Generic\Fields;

use Illuminate\Database\Eloquent\Builder;
use Eae\Database\Eloquent\Relations\BelongsToManyWebsite;
use Eae\Support\Contracts\ExternalFieldInterface;
use View;
use DB;
use Input;
use WebsiteObject;
use Eae\Backoffice\Generic\ContentType;
use Config;
use ContentTypes;

class SinglePickerField extends BaseField implements ExternalFieldInterface{
    protected $order = false;
    protected $objectType = false;
    protected $maxSize = false;
    protected $pivotTable = false;

    public function __construct($config){
        unset($config["multiLanguage"]);
        unset($config["inList"]);
        unset($config["search"]);
        unset($config["sort"]);

        parent::__construct($config);

        $this->relationField = true;

        if(empty($config["objectType"])){
            throw new InvalidConfigurationException("The {$this->name} configuration is missing the object type");
        }

        $this->objectType = $config["objectType"];

        if(!empty($config["pivotTable"])){
            $this->pivotTable = $config["pivotTable"];
        }

        if(!empty($config["order"])){
            $this->order = $config["order"];
        }

        if(!empty($config["maxSize"])){
            $this->maxSize = $config["maxSize"];
        }

    }

    public function isOrderable(){
        return $this->order;
    }

    public function setOrderable($order){
        $this->order = $order;
    }

    public function getPivotTable(){
        return $this->pivotTable;
    }

    public function setPivotTable($tableName){
        $this->pivotTable = $tableName;
    }

    public function getObjectType(){
        return $this->objectType;
    }

    public function getContentType(){
        return ContentTypes::find($this->objectType);
    }

    public function setObjectType($objectType){
        $this->objectType = $objectType;
    }

    public function getMaxSize(){
        return $this->maxSize;
    }

    public function setMaxSize($maxSize){
        $this->maxSize = $maxSize;
    }

    public function toForm($model, $enabled = true, $hidden = false){
        if(!$model->exists){
            return "";
        }

        $relationName = $this->getName();
        $relation = $model->$relationName();

        $contentType = $this->getContentType();
        $relationModelType = $contentType->getModelType();

        $relationModel = new $relationModelType;

        $languageRelationship = $relationModel->languages();
        $languageModel = $languageRelationship->getRelated();
        $websiteRelationship = $relationModel->websites();

        $websiteObjectModel = $websiteRelationship->getRelated();

        $query = $relationModel->full();
        if (!$this->pivotTable) {
            $query->where($relation->getForeignKey() , $model->getKey());
        } else {
            $query->join($this->pivotTable , $relation->getOtherKey(), '=', $relationModel->getTableName() . '.' . $relationModel->getKeyName());
            $query->where($relation->getForeignKey(),  $model->getKey());
        }

        if($this->isOrderable()){
            $query->orderBy("{$websiteObjectModel->getTable()}.position", "asc");
        }else{
            $query->orderBy("{$languageModel->getTable()}.title", "asc");
        }

        if (!$this->pivotTable) {
            $query->selectRaw("{$relationModel->getTable()}.{$relationModel->getKeyName()}, {$languageModel->getTable()}.title, {$websiteObjectModel->getTable()}.website_object_id");
        } else {
            $query->selectRaw("{$relationModel->getTable()}.{$relationModel->getKeyName()}, {$languageModel->getTable()}.title, {$websiteObjectModel->getTable()}.website_object_id");
        }
        $list = $query->get();
        return View::make(
            "admin.generic.components.forms.singlepicker",
            array(
                "id"                => $this->getName(),
                "name"              => $this->getName(),
                "label"             => $this->getLabel(),
                "order"             => $this->isOrderable(),
                "maxSize"           => $this->getMaxSize(),
                "list"              => $list,
                "hide"              => $hidden,
                "enabled"           => $enabled,
                "childContentType"  => $contentType,
                "parentId"          => $model->getKey(),
                "info"              => $this->getInfo(),
                "parentContentType" => ($this->pivotTable ? $model->content_type : false),
                "parentHidden"      => ($hidden && !empty($this->parent)),
                "parent"            => $this->parent,
            ));

    }



    public function getValidationData($copyAllLang = false){
        return null;
    }

    public function save($model, $values){
     /*   if($this->isWebsiteFiltered()){
            return $this->_saveWebsite($model, $values);
        }else{*/
            return $this->_saveNoWebsite($model, $values);
     //   }
    }

    protected function _saveNoWebsite($model, $values){
        $relationName = $this->getName();
        $relation = $model->$relationName();

        $ids = array_flatten($values);

        if($this->isOrderable()){
            $syncData = array();
            $position = 1;
            foreach($ids as $id){
                $syncData[$id] = array("position" => $position);
                $position = $position + 1;
            }

            $model->$relationName()->sync($syncData);
        }else{
            $model->$relationName()->sync($ids);
        }
    }

    protected function _saveWebsite($model, $values){
        if(empty($this->objectTypes)){
            return;
        }

        $type = $this->getName();
        $websiteObject = $model->getActiveWebsiteEntry();
        $relation = $websiteObject->related();

        $ids = array_flatten($values);

        if($this->isOrderable()){
            $syncData = array();
            $position = 1;
            foreach($ids as $id){
                $syncData[$id] = array("position" => $position, "type" => $type);
                $position = $position + 1;
            }
            $relation->wherePivot("type", $type)->sync($syncData);
        }else{
            $syncData = array();
            foreach($ids as $id){
                $syncData[$id] = array("type" => $type);
            }
            $relation->wherePivot("type", $type)->sync($syncData);
        }
    }

    public static function getScript(){
        return array(
            "singlePicker" => Config::get('path.backoffice.js_path') . '/view/generic/backend-single-picker.js'
            );

    }
}


