<?php


namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Snapshot\Providers;


use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\Enum\JobPeriod;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;

class JobProvider  extends BaseModelDataProvider implements ClientArea
{
    use ProductService;

    public function __construct()
    {
        parent::__construct(SnapshotJob::class);
    }

    protected function isModelProper($model)
    {
        if (in_array(get_parent_class($model), [
            'ModulesGarden\Servers\ProxmoxVps\Core\Models\ExtendedEloquentModel',
            'ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel',
            'Illuminate\Database\Eloquent\Model'
        ]))
        {
            return true;
        }

        return false;
    }

    public function read()
    {
        //period
        $period = $this->configuration()->getPermissionSnapshotJobPeriod();
        if($period && in_array(JobPeriod::HOURLY, $period)){
            $this->availableValues['period'][JobPeriod::HOURLY] = sl("lang")->tr(JobPeriod::HOURLY );
        }
        if($period && in_array(JobPeriod::DAILY, $period)){
            $this->availableValues['period'][JobPeriod::DAILY] = sl("lang")->tr(JobPeriod::DAILY);
        }
        if(!$period){
            $this->availableValues['period'] = [
                JobPeriod::HOURLY => sl("lang")->tr(JobPeriod::HOURLY ),
                JobPeriod::DAILY => sl("lang")->tr(JobPeriod::DAILY )
            ];
        }
         //days
        $this->availableValues['days'] = [];
        foreach (JobPeriod::DAYS_OF_WEEK as $day){
            $this->availableValues['days'][$day] =  sl("lang")->tr($day);
        }
        if($this->formData){
            $this->data = $this->formData;
            return;
        }
        parent::read();
        $vmstate = $this->data['vmstate']==1 ? "on":"off" ;
        $this->data['vmstate']= $vmstate;
        //start_time
        if($this->data['start_time']){
            $this->data['start_time'] = substr($this->data['start_time'], 0,-3);
        }
    }

    public function create()
    {
        //vmstate
        if(isset($this->formData['vmstate'])){
            $vmstate = $this->formData['vmstate']=="on" ? 1:0;
            $this->formData['vmstate']= $vmstate ;
        }
        //hosting_id
        $this->formData['hosting_id']= $this->getWhmcsParamByKey('serviceid');
        //fill from data
        $this->model->fill($this->formData)->save();
        sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has been created successfully')
            ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
            ->setCallBackFunction('pmToggleCreateJobButton');
    }

    public function update(){

        //vmstate
        if(isset($this->formData['vmstate'])){
            $vmstate = $this->formData['vmstate']=="on" ? 1:0;
            $this->formData['vmstate']= $vmstate ;
        }
        parent::update();
        sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has updated successfully');
    }

    public function deleteMass()
    {
        foreach ($this->getRequestValue('massActions') as $id)
        {
            $this->model->where('id', $id)->delete();
        }
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('The selected snapshot jobs have been deleted successfully')
            ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
            ->setCallBackFunction('pmToggleCreateJobButton');
    }


    public function delete()
    {
        parent::delete();
        sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('Snapshot Job :name: has been deleted successfully')
            ->addData('createJobButtonStatus', $this->resourceGuard()->hasSnapshotJobLimit())
            ->setCallBackFunction('pmToggleCreateJobButton');
    }
}