<?php namespace Eae\Html;

use Illuminate\Http\Request;
use Illuminate\Html\FormBuilder as IlluminateFormBuilder;


class FormBuilder extends IlluminateFormBuilder{

	/**
     * The input implementation.
     *
     * @var \Illuminate\Http\Request
     */
    protected $input;

	/**
	 * Get the input implementation.
	 *
	 * @return  \Illuminate\Http\Request  $input
	 */
	public function getInput()
	{
		return $this->input;
	}

	/**
	 * Set the input implementation.
	 *
	 * @param  \Illuminate\Http\Request  $input
	 * @return $this
	 */
	public function setInput(Request $input)
	{
		$this->input = $input;

		return $this;
	}
    
    /**
     * Get the value that should be assigned to the field.
     *
     * @param  string  $name
     * @param  string  $value
     * @return string
     */
    public function getValueAttribute($name, $value = null)
    {
        if (is_null($name)) return $value;

        if ( ! is_null($this->old($name)))
        {
            return $this->old($name);
        } 
        
        if ( ! is_null($this->submitted($name)))
        {
            return $this->submitted($name);
        }

        if ( ! is_null($value)) return $value;

        if (isset($this->model))
        {
            return $this->getModelValueAttribute($name);
        }
    }
    
    /**
     * Get a value from the input.
     *
     * @param  string  $name
     * @return string
     */
    public function submitted($name)
    {
        if (isset($this->input))
        {    
            return $this->input->input($this->transformKey($name));
        }
    }
    
    /**
     * Get the check state for a checkbox input.
     *
     * @param  string  $name
     * @param  mixed  $value
     * @param  bool  $checked
     * @return bool
     */
    protected function getCheckboxCheckedState($name, $value, $checked)
    {
        if (isset($this->session) && 
            ! $this->oldInputIsEmpty() && 
            is_null($this->old($name)) 
            && is_null($this->submitted($name))){
            
            return false;        
        } 

        if ($this->missingOldSubmittedAndModel($name)) return $checked;

        $posted = $this->getValueAttribute($name);

        return is_array($posted) ? in_array($value, $posted) : (bool) $posted;
    }

    /**
     * Get the check state for a radio input.
     *
     * @param  string  $name
     * @param  mixed  $value
     * @param  bool  $checked
     * @return bool
     */
    protected function getRadioCheckedState($name, $value, $checked)
    {
        if ($this->missingOldSubmittedAndModel($name)) return $checked;

        return $this->getValueAttribute($name) == $value;
    }

    /**
     * Determine if old input or model input exists for a key.
     *
     * @param  string  $name
     * @return bool
     */
    protected function missingOldSubmittedAndModel($name)
    {
        return (is_null($this->old($name)) && 
            is_null($this->submitted($name)) && 
            is_null($this->getModelValueAttribute($name)));
    }

}
