<?php

namespace Sargilla\FlatLab\Services\ViewComposers;

use Illuminate\View\View;
use Illuminate\Support\Str;
use Illuminate\Http\Request;



class ActiveLinkComposer
{
    /**
     * @var \Illuminate\Http\Request
     */
    private $request;

    /**
     * @var string
     */
    private $activeClass = 'active';


    /**
     * @var string
     */
    //private $template = ' class=active';
    private $template = ' class={class}';

    /**
     * Create a new profile composer.
     *
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('link', $this);
    }


    public function activarLink($patron, $activeClass = null, $template = null)
    {
       	if($this->comparar($patron,$this->request->decodedPath())){
       		
       		if($activeClass){
       			$this->class($activeClass);
       		}

       		if($template){
       			$this->template($template);
       		}

    		return str_replace('{class}', $this->activeClass, $this->template);
    	}
    }


    /**
     * Determine if any of the provided patterns match the given value.
     *
     * @param $patterns string|array A single or list of patterns to match against.
     * @param $value string The string to match the patterns against.
     * @return bool
     */
    private function comparar($patterns, $value)
    {
        foreach ((array) $patterns as $pattern) {
        	if (Str::is($pattern, $value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Set the active class to be used on future calls.
     *
     * @param $activeClass
     * @return $this
     */
    public function class($activeClass)
    {
        $this->activeClass = $activeClass;

        return $this;
    }


    /**
     * Set the template to be used on future calls.
     *
     * @param $activeTemplate
     * @return $this
     */
    public function template($activeTemplate)
    {
        $this->template = $activeTemplate;

        return $this;
    }

    /**
     *  activeClass
     */
    public function activeClass()
    {
        return $this->activeClass;
    }
    
   
}