<?php
namespace Adianti\Widget\Container;

use Adianti\Widget\Base\TElement;
use Adianti\Widget\Base\TScript;

/**
 * Expander Widget
 *
 * This class represents an expander widget, which is a dropdown component 
 * that can be used to expand and display additional content.
 *
 * @version    7.5
 * @package    widget
 * @subpackage container
 * @author     Pablo Dall'Oglio
 * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
 * @license    http://www.adianti.com.br/framework-license
 */
class TExpander extends TElement
{
    private $container;
    private $button;
    private $caret_side;
    private $label;
    
    /**
     * Class Constructor
     *
     * Initializes the expander widget with a label.
     *
     * @param string $label The text label for the expander button.
     */
    public function __construct($label = '')
    {
        parent::__construct('div');
        $this->{'id'}    = 'texpander_'.mt_rand(1000000000, 1999999999);
        $this->{'class'} = 'dropdown';
        
        $this->button = new TElement('button');
        $this->button->{'class'} = 'btn btn-default dropdown-toggle';
        $this->button->{'type'} = 'button';
        $this->button->{'id'}   = 'button_'.mt_rand(1000000000, 1999999999);
        $this->button->{'data-toggle'} = 'dropdown';
        $this->label = $label;
        
        $this->container = new TElement('ul');
        $this->container->{'class'} = 'dropdown-menu texpander-container';
        
        $this->container->{'aria-labelledby'} = $this->button->{'id'};
        
        parent::add($this->button);
        parent::add($this->container);
    }
    
    /**
     * Set caret side
     *
     * Defines the position of the caret indicator.
     *
     * @param string $caret_side The caret side (options: 'left', 'right').
     */
    public function setCaretSide($caret_side)
    {
        $this->caret_side = $caret_side;
    }
    
    /**
     * Define the pull side
     *
     * Sets the side where the dropdown will appear.
     *
     * @param string $side The side position ('left' or 'right').
     */
    public function setPullSide($side)
    {
        $this->container->{'class'} = "dropdown-menu texpander-container pull-{$side}";
    }
    
    /**
     * Define a button property
     *
     * Sets a property for the button element.
     *
     * @param string $property The property name (e.g., 'style', 'class').
     * @param mixed  $value    The value to assign to the property.
     */
    public function setButtonProperty($property, $value)
    {
        $this->button->$property = $value;
    }
    
    /**
     * Define a container property
     *
     * Sets a property for the container element.
     *
     * @param string $property The property name (e.g., 'style', 'class').
     * @param mixed  $value    The value to assign to the property.
     */
    public function setProperty($property, $value)
    {
        $this->container->$property = $value;
    }
    
    /**
     * Add content to the expander
     *
     * Adds an element to the expander container.
     *
     * @param TElement $content An object that implements the show() method.
     */
    public function add($content)
    {
        $this->container->add($content);
    }
    
    /**
     * Shows the expander
     *
     * Renders the expander widget, including its button, container, and caret positioning.
     */
    public function show()
    {
        if ($this->caret_side == 'left')
        {
            $this->button->add(TElement::tag('span', '', array('class'=>'caret')));
            $this->button->add($this->label);
        }
        else if ($this->caret_side == 'right')
        {
            $this->button->add($this->label);
            $this->button->add('&nbsp');
            $this->button->add(TElement::tag('span', '', array('class'=>'caret')));
        }
        else
        {
            $this->button->add($this->label);
        }
        
        parent::show();
        TScript::create('texpander_start();');
    }
}
