<?php

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

use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AjaxElementInterface;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;

class OnOffAjaxSwitch extends CustomActionButton implements AjaxElementInterface
{
    use \ModulesGarden\HostingQuotaNotifications\Core\UI\Traits\RequestObjectHandler;

    protected $id             = 'onOffAjaxSwitch';
    protected $class          = ['btn btn-circle btn-outline btn-inverse btn-success btn-icon-only'];
    protected $icon           = 'fa fa-plus';
    protected $title          = 'onOffAjaxSwitch';
    protected $htmlAttributes = [];
    protected $switchModel    = null;
    protected $switchColumn   = 'enable_value';
    protected $switchOnValue  = 'on';
    protected $switchOffValue = 'off';
    protected $actionIdColumn = 'id';
    protected $lang           = null;


    public function __construct($baseId = null)
    {
        parent::__construct($baseId);

        $this->initContent();

        $this->loadRequestObj();
    }

    public function returnAjaxData()
    {
        $actionId    = $this->getRequestValue('actionElementId', false);
        $switchValue = $this->getRequestValue('value', false);

        if (!$this->switchModel || !$this->switchColumn || !$this->switchOnValue || !$actionId || !$switchValue)
        {
            return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessageAndTranslate('SavingError')->setCallBackFunction($this->callBackFunction);
        }

        try
        {
            $model = new $this->switchModel();
            $query = $model->newQuery();
            $query->where($this->actionIdColumn, '=', $actionId);
            if ($query->count() > 0)
            {
                $query = $model->newQuery();
                $query->where($this->actionIdColumn, '=', $actionId)
                        ->update([$this->switchColumn => ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue)]);
            }
            else
            {
                $model->{$this->actionIdColumn} = $actionId;
                $model->{$this->switchColumn}   = ($switchValue == 'on' ? $this->switchOnValue : $this->switchOffValue);
                $model->save();
            }
        }
        catch (\Exception $exc)
        {
            return (new ResponseTemplates\DataJsonResponse())->setStatusError()->setMessage($exc->getMessage())->setCallBackFunction($this->callBackFunction);
        }
        $lang = 'Changes has been saved successfully';
        if($this->lang)
        {
            $lang = $this->lang;
        }
        return (new ResponseTemplates\DataJsonResponse())->setMessageAndTranslate($lang)->setCallBackFunction($this->callBackFunction);
    }

    public function initContent()
    {
        $this->htmlAttributes['@change'] = 'onOffSwitch($event, \'' . $this->id . '\')';
    }

    public function getValue()
    {
        return $this->value;
    }

    public function setValue($value)
    {
        return $this->value;
    }

    public function setSwitchTarget($model = null, $columnName = null)
    {
        if ($model instanceof \ModulesGarden\HostingQuotaNotifications\Core\Models\ExtendedEloquentModel || $model instanceof \Illuminate\Database\Eloquent\model)
        {
            $this->switchModel = $model;
        }

        if ($columnName !== null && is_string($columnName))
        {
            $this->switchColumn = $columnName;
        }

        return $this;
    }

    public function getSwitchColumnName()
    {
        return $this->switchColumn;
    }

    public function setSwitchOnValue($value)
    {
        if (is_string($value) || is_numeric($value))
        {
            $this->switchOnValue = $value;
        }

        return $this;
    }

    public function getSwitchOnValue()
    {
        return $this->switchOnValue;
    }

    public function setActionIdColumn($colName)
    {
        if ($colName !== '' && is_string($colName))
        {
            $this->actionIdColumn = $colName;
        }

        return $this;
    }
}
