<?php
/**
 * Created by PhpStorm.
 * User: danielknudsen
 * Date: 20.11.19
 * Time: 19:42
 */

namespace App\EntityVirtual;


use Symfony\Component\Config\Definition\Exception\Exception;

class ServiceData
{

    const SERVICE_DATA_TYPE_CUSTOMERS = 'customers';
    const SERVICE_DATA_TYPE_CUSTOMER_CONTACTS = 'customerContacts';
    const SERVICE_DATA_TYPE_CUSTOMER_MEETINGS = 'customerMeetings';
    const SERVICE_DATA_TYPE_INTERNAL_MEETINGS = 'internalMeetings';
    const SERVICE_DATA_TYPE_OPERATORS = 'operators';
    const SERVICE_DATA_TYPE_OPERATOR_CONTACTS = 'operatorContacts';
    const SERVICE_DATA_TYPE_OPERATOR_MEETINGS = 'operatorMeetings';
    const SERVICE_DATA_TYPE_PRODUCTIONS = 'productions';
    const SERVICE_DATA_TYPE_PRODUCTION_CONTACTS = 'productionContacts';
    const SERVICE_DATA_TYPE_PRODUCTION_MEETINGS = 'productionMeetings';
    const SERVICE_DATA_TYPE_SERVICES = 'services';
    const SERVICE_DATA_TYPE_SERVICES_CONTACTS = 'serviceContacts';
    const SERVICE_DATA_TYPE_SERVICES_MEETINGS = 'serviceMeetings';

    const ACTION_ADD = 'add';
    const ACTION_EDIT = 'edit';
    const ACTION_DELETE = 'delete';

    private static $validServiceDataTypes = [
        self::SERVICE_DATA_TYPE_CUSTOMERS => 1,
        self::SERVICE_DATA_TYPE_CUSTOMER_CONTACTS => 1,
        self::SERVICE_DATA_TYPE_CUSTOMER_MEETINGS => 1,
        self::SERVICE_DATA_TYPE_INTERNAL_MEETINGS => 1,
        self::SERVICE_DATA_TYPE_OPERATORS => 1,
        self::SERVICE_DATA_TYPE_OPERATOR_CONTACTS => 1,
        self::SERVICE_DATA_TYPE_OPERATOR_MEETINGS => 1,
        self::SERVICE_DATA_TYPE_PRODUCTIONS => 1,
        self::SERVICE_DATA_TYPE_PRODUCTION_CONTACTS => 1,
        self::SERVICE_DATA_TYPE_PRODUCTION_MEETINGS => 1,
        self::SERVICE_DATA_TYPE_SERVICES => 1,
        self::SERVICE_DATA_TYPE_SERVICES_CONTACTS => 1,
        self::SERVICE_DATA_TYPE_SERVICES_MEETINGS => 1,
    ];

    private static $validActions = [
        self::ACTION_ADD => 1,
        self::ACTION_EDIT => 1,
        self::ACTION_DELETE => 1,
    ];

    private $serviceData;

    /**
     * ServiceData constructor.
     */
    public function __construct()
    {
        $this->serviceData = [];
        foreach (self::$validServiceDataTypes as $serviceDataType => $dummy) {
            $this->serviceData[$serviceDataType] = null;
        }
    }

    public function getServiceData()
    {
        return $this->serviceData;
    }

    /**
     * @param $serviceDataType
     * @param $action
     * @param $data
     */
    public function addServiceData($serviceDataType, $action, $data)
    {
        if (!$this->isValidServiceDataType($serviceDataType)) {
            throw new Exception('invalid service data type');
        }

        if (!$this->isValidAction($action)) {
            throw new Exception('invalid action');
        }

        if (is_null($this->serviceData[$serviceDataType])) {
            $this->serviceData[$serviceDataType] = [
                self::ACTION_ADD => [],
                self::ACTION_EDIT => [],
                self::ACTION_DELETE => [],
            ];
        }
        $this->serviceData[$serviceDataType][$action][] = $data;
    }

    /**
     * @param $serviceDataType
     * @return bool
     */
    private function isValidServiceDataType($serviceDataType)
    {
        return array_key_exists($serviceDataType, self::$validServiceDataTypes);
    }

    /**
     * @param $action
     * @return bool
     */
    private function isValidAction($action)
    {
        return array_key_exists($action, self::$validActions);
    }

}