<?php

namespace App\Services\Customer;

use App\Entities\Customer\ClientEntity;
use App\Services\CommonService;

use App\Services\Customer\ClientService;
use App\Services\Equipment\Part\CpuService;
use App\Services\Equipment\Part\DefenceService;
use App\Services\Equipment\Part\DomainService;
use App\Services\Equipment\Part\IpService;
use App\Services\Equipment\Part\LineService;
use App\Services\Equipment\Part\RamService;
use App\Services\Equipment\Part\SoftwareService;
use App\Services\Equipment\Part\StorageService;
use App\Services\Equipment\ServerService;
use App\Services\UserService;

abstract class CustomerService extends CommonService
{
    private ?UserService $_userService = null;
    private ?ClientService $_clientService = null;
    private $_equipmentService = [];
    protected function __construct()
    {
        parent::__construct();
        $this->addClassName('Customer');
    }
    final public function getClientService(): ClientService
    {
        if (!$this->_clientService) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }
    final public function getUSerService(): UserService
    {
        if (!$this->_userService) {
            $this->_userService = new UserService();
        }
        return $this->_userService;
    }
    final public function getServiceService(): ServiceService
    {
        if (!$this->_serviceService) {
            $this->_serviceService = new ServiceService();
        }
        return $this->_serviceService;
    }
    //ServiceItemController,ServiceController에서 사용
    final public function getServiceItemLinkService(string $key): mixed
    {
        if (!array_key_exists($key, $this->_equipmentService)) {
            switch ($key) {
                case 'SERVER':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new ServerService();
                    }
                    break;
                case 'CPU':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new CpuService();
                    }
                    break;
                case 'RAM':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new RamService();
                    }
                    break;
                case 'STORAGE':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new StorageService();
                    }
                    break;
                case 'LINE':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new LineService();
                    }
                    break;
                case 'IP':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new IpService();
                    }
                    break;
                case 'DEFENCE':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new DefenceService();
                    }
                    break;
                case 'SOFTWARE':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new SoftwareService();
                    }
                    break;
                case 'DOMAIN':
                    if (!array_key_exists($key, $this->_equipmentService)) {
                        $this->_equipmentService[$key] = new DomainService();
                    }
                    break;
                default:
                    throw new \Exception(__FUNCTION__ . "에서 사용하지않는 Service를 요청하였습니다.: {$key}");
            }
        }
        return $this->_equipmentService[$key];
    }
    //기본기능
    //FieldForm관련용
    public function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'clientinfo_uid':
            case 'ownerinfo_uid':
                $options = $this->getClientService()->getEntities();
                break;
            case 'serviceinfo_uid':
                $options = $this->getServiceService()->getEntities();
                break;
            case 'user_uid':
                $options = $this->getUserService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //ItemType에 따른 FilterOption 설정용
    final public function getFilterOptionsByItemType(string $item_type): array
    {
        return $this->getServiceItemLinkService($item_type)->getEntities();
    }
    final public function getClient(int $uid): ClientEntity
    {
        $entity = $this->getClientService()->getEntity($uid);
        if (!$entity) {
            throw new \Exception("{$uid}에 해당하는 고객/관리자 정보가 존재하지 않습니다. uid: {$uid}");
        }
        return $entity;
    }
}
