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

use Illuminate\Database\Eloquent\Builder;
use Eae\Exception\InvalidConfigurationException;
use LaravelLocalization;
use View;
use Input;
use ContentTypes;
use Config;
             
class ColorPickerField extends BaseField{
    
    public function __construct($config){   
        parent::__construct($config);
    }

    public function toForm($model, $enabled = true, $hidden = false){
        $html = "";
        if($this->isMultiLanguage()){
            $locales = LaravelLocalization::getSupportedLocales();
            $defaultLocale = LaravelLocalization::getDefaultLocale();
            
            foreach($locales as $locale => $localeData){
                $fieldName = $this->getName();
                $name = $fieldName . "[{$locale}]";
                $id = $fieldName . "_{$locale}";
                $errorKey = $fieldName . "." . $locale; 
                $value = null;
                
                if($model->exists){
                    $languageModel = $model->languages->findByLocale($locale);
                   
                    if(!empty($languageModel)){
                        $value = $languageModel->$fieldName;
                    }
                }
                
                $html .= View::make(
                    "admin.generic.components.forms.colorpicker", 
                    array(
                        "id"            => $id,
                        "errorKey"      => $errorKey,
                        "name"          => $name,
                        "label"         => $this->getLabel(),
                        "value"         => $value,
                        "language"      => $locale,
                        "defaultLang"   => $defaultLang,
                        "hide"          => ($hidden || $locale != $defaultLocale),
                        "enabled"       => $enabled,
                        "parentHidden"  => ($hidden && !empty($this->parent)),
                        "parent"        => $this->parent,
                        "info"          => $this->getInfo() 
                    ));    
            }    
        }else{
            $fieldName = $this->getName();
            $value = null;
            
            if($model->exists){
                $value = $model->$fieldName;    
            }
     
            $html .= View::make(
                    "admin.generic.components.forms.colorpicker", 
                    array(
                        "id"            => $this->getName(),
                        "errorKey"      => $this->getName(),
                        "name"          => $this->getName(),
                        "label"         => $this->getLabel(),
                        "value"         => $value,
                        "hide"          => $hidden,
                        "enabled"       => $enabled,
                        "parentHidden"  => ($hidden && !empty($this->parent)),
                        "parent"        => $this->parent,
                        "info"          => $this->getInfo()   
                    ));     
        }
        
        return $html; 
    }
    
    public function getValidation(){
        $validation = parent::getValidation();
        $rule = 'regex:/^#?[a-fA-F0-9]{3,6}$/';

        if(empty($validation)){
            return $rule;
        }else{
            return $validation . "|" . $rule;    
        }
    }

    public static function getScript(){
        $scripts = parent::getScript();
        $scripts["colorPicker"] = Config::get('path.backoffice.js_path') . '/bootstrap-colorpicker.min.js';
        $scripts["colorPickerField"] = Config::get('path.backoffice.js_path') . '/view/generic/backend-color-picker-field.js';
        
        return $scripts;
        
    }
 
}
  

