<?php

namespace LaraBB\Application;

/**
 *
 */
class Button
{
    /**
     * @var string|null
     */
    private ?string $strText = null;
    /**
     * @var bool
     */
    private bool $boolAutoFocus = false;
    /**
     * @var bool
     */
    private bool $boolDisabled = false;
    /**
     * @var string|null
     */
    private ?string $strFormId = null;
    /**
     * @var string|null
     */
    private ?string $strFormAction = null;
    /**
     * @var string|null
     */
    private ?string $strFormMethod = null;
    /**
     * @var string|null
     */
    private ?string $strFormEncType = null;
    /**
     * @var bool|null
     */
    private ?bool $boolFormNoValidate = null;
    /**
     * @var string|null
     */
    private ?string $strFormTarget = null;
    /**
     * @var string|null
     */
    private ?string $strName = null;
    /**
     * @var string
     */
    private string $strType = 'submit';
    /**
     * @var string|null
     */
    private ?string $strValue = null;
    /**
     * @var string|null
     */
    private ?string $strTarget = null;
    /**
     * @var bool
     */
    private bool $boolDropdown = false;
    /**
     * @var array
     */
    private array $arrDropdownItems = [];
    /**
     * @var string|null
     */
    private ?string $strDropDirection = null;
    /**
     * @var string|null
     */
    private ?string $strClass = null;
    /**
     * @var string|null
     */
    private ?string $strId = null;
    /**
     * @var string|null
     */
    private ?string $strIcon = null;
    /**
     * @var string|null
     */
    private ?string $strHref = null;
    /**
     * @var bool|null
     */
    private ?bool $boolPermission = true;

    /**
     * @var array
     */
    private array $arrData;

    /**
     * @param string $strText
     */
    public function __construct(string $strText = '')
    {
        $this->text($strText);
        $this->arrData = [];
    }

    /**
     * @param string $strText
     * @return Button
     */
    public function text(string $strText): static
    {
        $this->strText = $strText;

        return $this;
    }

    /**
     * @param bool $boolAutoFocus
     * @return Button
     */
    public function autofocus(bool $boolAutoFocus = true): static
    {
        $this->boolAutoFocus = $boolAutoFocus;

        return $this;
    }

    /**
     * @param bool $boolDisabled
     * @return Button
     */
    public function disabled(bool $boolDisabled = true): static
    {
        $this->boolDisabled = $boolDisabled;

        return $this;
    }

    /**
     * @param string $strFormId
     * @return Button
     */
    public function form(string $strFormId): static
    {
        $this->strFormId = $strFormId;

        return $this;
    }

    /**
     * @param string $strFormAction
     * @return Button
     */
    public function formaction(string $strFormAction): static
    {
        $this->strFormAction = $strFormAction;

        return $this;
    }

    /**
     * @param string $strFormEncType
     * @return $this
     */
    public function formenctype(string $strFormEncType): static
    {
        $this->strFormEncType = $strFormEncType;

        return $this;
    }

    /**
     * @param string $strFormMethod
     * @return Button
     */
    public function formmethod(string $strFormMethod): static
    {
        $this->strFormMethod = $strFormMethod;

        return $this;
    }

    /**
     * @param bool $boolFormNoValidate
     * @return Button
     */
    public function formnovalidate(bool $boolFormNoValidate = true): static
    {
        $this->boolFormNoValidate = $boolFormNoValidate;

        return $this;
    }

    /**
     * @param string $strFormTarget
     * @return Button
     */
    public function formtarget(string $strFormTarget): static
    {
        $this->strFormTarget = $strFormTarget;

        return $this;
    }

    /**
     * @param string $strId
     * @return Button
     */
    public function id(string $strId): static
    {
        $this->strId = $strId;

        return $this;
    }

    /**
     * @param string $strClass
     * @return Button
     */
    public function class(string $strClass): static
    {
        $this->strClass = $strClass;

        return $this;
    }

    /**
     * @param string $strType
     * @return Button
     */
    public function type(string $strType): static
    {
        $this->strType = $strType;

        return $this;
    }

    /**
     * @param string $strName
     * @param string $strValue
     * @return $this
     */
    public function data(string $strName, string $strValue): static
    {
        $this->arrData[$strName] = $strValue;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getData(): ?string
    {
        if (count($this->arrData)) {
            $strRet = '';
            foreach ($this->arrData as $strIndex => $strValue) {
                $strRet .= sprintf(' data-%s="%s" ', $strIndex, $strValue);
            }
            return $strRet;
        }
        return '';
    }

    /**
     * @param string $strName
     * @return Button
     */
    public function name(string $strName): static
    {
        $this->strName = $strName;

        return $this;
    }

    /**
     * @param string $strValue
     * @return Button
     */
    public function value(string $strValue): static
    {
        $this->strValue = $strValue;

        return $this;
    }

    /**
     * @param string $strDropDirection
     * @param array $arrDropdownItems
     * @return Button
     */
    public function dropdown(array $arrDropdownItems, string $strDropDirection = 'down'): static
    {
        $this->boolDropdown     = true;
        $this->strDropDirection = $strDropDirection === 'down' ? null : $strDropDirection;
        $this->arrDropdownItems = $arrDropdownItems;

        return $this;
    }

    /**
     * @param string $strIcon
     * @return Button
     */
    public function icon(string $strIcon): static
    {
        $this->strIcon = $strIcon;

        return $this;
    }

    /**
     * @param string $strHref
     * @return $this
     */
    public function href(string $strHref): static
    {
        $this->strHref = $strHref;

        return $this;
    }

    /**
     * @param string $strTarget
     * @return $this
     */
    public function target(string $strTarget): static
    {
        $this->strTarget = $strTarget;
        return $this;
    }

    /**
     * @param $boolPermission
     * @return $this
     */
    public function permission($boolPermission): static
    {
        $this->boolPermission = $boolPermission;
        return $this;
    }

    /**
     * @return string|null
     */
    public function getText(): ?string
    {
        return $this->strText;
    }

    /**
     * @return bool
     */
    public function getAutoFocus(): bool
    {
        return $this->boolAutoFocus;
    }

    /**
     * @return bool
     */
    public function getDisabled(): bool
    {
        return $this->boolDisabled;
    }

    /**
     * @return string|null
     */
    public function getForm(): ?string
    {
        return $this->strFormId;
    }

    /**
     * @return string|null
     */
    public function getFormAction(): ?string
    {
        return $this->strFormAction;
    }

    /**
     * @return string|null
     */
    public function getFormEnctype(): ?string
    {
        return $this->strFormEncType;
    }

    /**
     * @return string|null
     */
    public function getFormMethod(): ?string
    {
        return $this->strFormMethod;
    }

    /**
     * @return bool|null
     */
    public function getFormNoValidate(): ?bool
    {
        return $this->boolFormNoValidate;
    }

    /**
     * @return string|null
     */
    public function getFormTarget(): ?string
    {
        return $this->strFormTarget;
    }

    /**
     * @return string|null
     */
    public function getName(): ?string
    {
        return $this->strName;
    }

    /**
     * @return string|null
     */
    public function getType(): ?string
    {
        return $this->strType;
    }

    /**
     * @return string|null
     */
    public function getValue(): ?string
    {
        return $this->strValue;
    }

    /**
     * @return string|null
     */
    public function getClass(): ?string
    {
        return $this->strClass;
    }

    /**
     * @return string|null
     */
    public function getId(): ?string
    {
        return $this->strId;
    }

    /**
     * @return string|null
     */
    public function getHref(): ?string
    {
        return $this->strHref;
    }

    /**
     * @return string|null
     */
    public function getTarget(): ?string
    {
        return $this->strTarget;
    }


    /**
     * @return bool
     */
    public function hasDropdown(): bool
    {
        return $this->boolDropdown;
    }

    /**
     * @return string|null
     */
    public function getDropDirection(): ?string
    {
        return $this->strDropDirection;
    }

    /**
     * @return array
     */
    public function getDropdownItems(): array
    {
        return $this->arrDropdownItems;
    }

    /**
     * @return string|null
     */
    public function getIcon(): ?string
    {
        return $this->strIcon;
    }

    /**
     * @return bool|null
     */
    public function getPermission(): ?bool
    {
        return $this->boolPermission;
    }

    /**
     * @return string
     */
    public function render(): string
    {
        return view('components.atomic.button', [
            'btn' => $this
        ])->render();
    }

    /**
     * @return string
     */
    public function __toString()
    {
        return $this->render();
    }
}
