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

use Eae\Support\Contracts\SavableFieldInterface;
use Config;
use View;
use RequestContext;
use Input;
use Lang;

class LocationField extends BaseField implements SavableFieldInterface{
    
    protected $required = false;
    
    public function __construct($config){
        unset($config["multiLanguage"]);
        unset($config["validation"]);
        unset($config["preValidation"]);
        unset($config["inList"]);
        unset($config["search"]);
        unset($config["sort"]);
        
        parent::__construct($config);
        
        if(!empty($config["required"])){
            $this->required = $config["required"]; 
        }
    }
    
    public function isRequired(){
        return $this->required;    
    }
    
    public function setRequired($required){
        $this->required = $required;
    }
    
    public function getLatName(){
        return $this->getName() . "_lat";
    }
    
    public function getLngName(){
        return $this->getName() . "_lng";
    }
    
    public function getZoomName(){
        return $this->getName() . "_zoom";
    }
    
    public function getCurrentValue($model){
        $fieldName = $this->getName();
        
        $value = array("lat" => null, "lng" => null, "zoom" => 1);
        
        $latName = $this->getLatName();
        $lngName = $this->getLngName();
        $zoomName = $this->getZoomName();
        
        if(Input::has($latName)){  
            $value["lat"] = Input::get($latName);    
        }elseif($model->exists){
            $value["lat"] = $model->$latName;
        }
        
        if(Input::has($lngName)){  
            $value["lng"] = Input::get($lngName);    
        }elseif($model->exists){
            $value["lng"] = $model->$lngName;
        }
        
        if(Input::has($zoomName)){  
            $value["zoom"] = Input::get($zoomName);    
        }elseif($model->exists){
            $value["zoom"] = $model->$zoomName;
        }
        
        return $value;
    }

    public function toForm($model, $enabled = true, $hidden = false){
        $html = "";
        
        $fieldName = $this->getName();
        $value = array("lat" => null, "lng" => null, "zoom" => 1); 
        
        if($model->exists){
            $latName = $this->getLatName();
            $lngName = $this->getLngName();
            $zoomName = $this->getZoomName();
            $value["lat"] = $model->$latName; 
            $value["lng"] = $model->$lngName; 
            if(!is_null($model->$zoomName)){  
                $value["zoom"] = $model->$zoomName;   
            }
        }
        
        $errors = RequestContext::get("errors");
        $hasErrors = false;
        if(!empty($errors) && !$errors->isEmpty()){
            if($errors->has("{$this->getName()}.lat") || 
                $errors->has("{$this->getName()}.lng") || 
                $errors->has("{$this->getName()}.zoom")){
                    $hasErrors = true;    
            }
        }
        
        
        $html .= View::make(
                "admin.generic.components.forms.location", 
                array(
                    "id"            => $this->getName(),
                    "errorLatKey"   => $this->getName() . ".lat",
                    "errorLngKey"   => $this->getName() . ".lng",
                    "errorZoomKey"  => $this->getName() . ".zoom",
                    "hasErrors"     => $hasErrors,
                    "name"          => $this->getName(),
                    "label"         => $this->getLabel(),
                    "value"         => $value,
                    "hidden"        => $hidden,
                    "enabled"       => $enabled,
                    "required"      => $this->isRequired(),
                    "parentHidden"  => ($hidden && !empty($this->parent)),
                    "parent"        => $this->parent,
                    "info"          => $this->getInfo()    
                )); 
        
        
        return $html;
    }
    
    public function save($model, $values){
        
        $latName = $this->getLatName();
        $lngName = $this->getLngName();
        $zoomName = $this->getZoomName();
 
        if(empty($values["lat"]) || empty($values["lng"])){
            $model->$latName = null; 
            $model->$lngName = null;
            $model->$zoomName = null;           
        }else{
            $model->$latName = $values["lat"]; 
            $model->$lngName = $values["lng"];
            $model->$zoomName = $values["zoom"];
        }
    }
    
    public function getValidation($isZoom = false){
        $rules = "";
        if($this->isRequired()){
            $rules = "required|";    
        }
        
        if($isZoom){
            $rules .= "integer|min:0";        
        }else{
            $rules .= "numeric";     
        }
       
        
        return $rules;
    }
    
    public function getValidationData($copyAllLang = false){
        $validation = array(
            "rules" => array(), 
            "values" => array(),
            "labels" => array());
            
        $rules = $this->getValidation(); 
        $fieldName = $this->getName();
        $validation["values"][$fieldName] = Input::get($fieldName);
        $validation["rules"][$fieldName . ".lat"] = $this->getValidation();
        $validation["rules"][$fieldName . ".lng"] = $this->getValidation();
        $validation["rules"][$fieldName . ".zoom"] = $this->getValidation(true);
        $validation["labels"][$fieldName. ".lat"] = strtolower(Lang::get("backend.latitude"));
        $validation["labels"][$fieldName. ".lng"] = strtolower(Lang::get("backend.longitude"));
        $validation["labels"][$fieldName. ".zoom"] = "zoom";
        
        return $validation;    
    }
    
    public static function getScript(){
        $googleKey = Config::get("google.apiKey");
        if(!empty($googleKey)){
            return array("locationPicker" => 
                Config::get('path.backoffice.js_path') . '/view/generic/backend-location-picker.js',
                "googleMap" => 'http://maps.googleapis.com/maps/api/js?key=' . $googleKey);
        }else{
            return null;
        }
    }
    
    public function toArray(){
        $array = parent::toArray();
        $array["required"] = $this->required;
        return $array;  
    }
}
  

