<?php

namespace ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Buttons;

use \ModulesGarden\HostingQuotaNotifications\Core\UI\Builder\BaseContainer;


class RedirectButton extends BaseContainer
{
    protected $id             = 'redirectButton';
    protected $class          = ['btn btn-circle btn-outline btn-inverse btn-success btn-icon-only'];
//    protected $class          = ['btn btn--sm btn--link btn--icon btn--plain lu-tooltip drop-target drop-element-attached-bottom drop-element-attached-center drop-target-attached-top drop-target-attached-center'];
    protected $icon           = 'zmdi zmdi-rotate-right';
//    protected $title          = 'redirectButton';
    protected $title          = 'baseModalButton';
    protected $htmlAttributes = [
        'href'        => 'javascript:;',
        'data-toggle' => 'tooltip',
    ];    
    
    protected $rawUrl = null;
    protected $redirectParams = [];

    public function initContent()
    {
        $this->htmlAttributes['@click'] = 'redirect($event, ' . $this->parseCustomParams() . ')';        
    }

    protected function parseCustomParams()
    {
        if (count($this->redirectParams) === 0 && $this->rawUrl === null)
        {
            return '{}';
        }

        return $this->parseListTOJsString($this->redirectParams);
    }
    
    protected function parseListTOJsString($params)
    {
        $jsString = '{';
        
        if ($this->rawUrl !== null)
        {
            $params['rawUrl'] = $this->rawUrl;
        }        
        
        foreach ($params as $key => $value)
        {
            $jsString .= ' ' . str_replace('-', '__', $key) . ': ' . (is_array($value) ? ($this->parseListTOJsString($value) . ',') : ("'" . (string) $value) . "',");
        }
            
        $jsString = trim($jsString, ',') . ' } ';
        
        return $jsString;
    }
    
    public function setRawUrl($url)
    {
        $this->rawUrl = $url;
        
        return $this;
    }
    
    public function addRedirectParam($key, $value)
    {
        $this->redirectParams[$key] = $value;
        
        $this->updateHtmlAttributesByRedirectParams();
        
        return $this;
    }
    
    public function setRedirectParams($paramsList)
    {
        $this->redirectParams = $paramsList;
        
        $this->updateHtmlAttributesByRedirectParams();
        
        return $this;
    }
    
    protected function updateHtmlAttributesByRedirectParams()
    {
        foreach ($this->redirectParams as $key => $value)
        {
            $this->updateHtmlAttribute($key, $value);
        }
    }
    
    protected function updateHtmlAttribute($key, $value)
    {
        if (strpos($value, ':') === 0)
        {
            $this->addHtmlAttribute(':data-' . $key , 'dataRow.' . trim($value, ':'));
        }
    }
}