<?php

namespace KupShop\AutomationConfiguratorBundle\Configurator\Generic\Triggers;

use KupShop\AutomationConfiguratorBundle\Configurator\Interfaces\Config\TriggerConfig;
use KupShop\AutomationConfiguratorBundle\Configurator\Interfaces\Triggers\Trigger;
use KupShop\AutomationConfiguratorBundle\Configurator\Orders\OrdersContext;
use KupShop\AutomationConfiguratorBundle\Configurator\Sales\SalesContext;
use KupShop\AutomationConfiguratorBundle\Configurator\Users\UsersContext;

class Period extends Trigger implements OrdersContext, SalesContext, UsersContext
{
    public const NAME = 'period';

    public const PERIOD_FREQUENT = '0';
    public const PERIOD_NORMAL = '1';
    public const PERIOD_EXPENSIVE = '2';
    public const PERIOD_ALL = '10';

    public function getConfig()
    {
        return [
            'label' => 'Pouštět periodicky',
            'tooltip' => 'Autopilot se periodicky spouští na všech objednávkách.',
            'fields' => [
                [
                    'name' => 'period',
                    'title' => 'Perioda',
                    'type' => 'select',
                    'values' => [
                        ['key' => self::PERIOD_FREQUENT, 'title' => 'Každých 20 minut', 'default' => true],
                        ['key' => self::PERIOD_NORMAL, 'title' => 'Každé 2 hodiny'],
                        ['key' => self::PERIOD_EXPENSIVE, 'title' => 'Jednou denně v noci'],
                    ],
                ],
            ],
        ];
    }

    public function getName()
    {
        return self::NAME;
    }

    public function isRunnable(TriggerConfig $triggerConfig, $triggerContext)
    {
        $period = $triggerConfig->getField(self::NAME);

        return $period && ($period->getValue() == $triggerContext || $triggerContext == self::PERIOD_ALL);
    }
}
