<?php

namespace App\Controllers\Admin\Customer;

use App\Controllers\Admin\AdminController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

use App\Services\Customer\ClientService;
use App\Services\Equipment\Part\CpuService;
use App\Services\Equipment\Part\DefenceService;
use App\Services\Equipment\Part\StorageService;
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\ServerService;
use App\Services\Equipment\DomainService;

abstract class CustomerController extends AdminController
{
    private ?ClientService $_clientService = null;
    private $_equipmentService = [];
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
    }

    final public function getClientService(): ClientService
    {
        if (!$this->_clientService) {
            $this->_clientService = new ClientService($this->request);
        }
        return $this->_clientService;
    }
    final public function getEquipmentService(string $key): mixed
    {
        if (!array_key_exists($key, $this->_equipmentService)) {
            switch ($key) {
                case 'SERVER':
                    $this->_equipmentService[$key] = new ServerService();
                    break;
                case 'CPU':
                    $this->_equipmentService[$key] = new CpuService();
                    break;
                case 'RAM':
                    $this->_equipmentService[$key] = new RamService();
                    break;
                case 'STORAGE':
                    $this->_equipmentService[$key] = new StorageService();
                    break;
                case 'LINE':
                    $this->_equipmentService[$key] = new LineService();
                    break;
                case 'IP':
                    $this->_equipmentService[$key] = new IpService();
                    break;
                case 'DEFENCE':
                    $this->_equipmentService[$key] = new DefenceService();
                    break;
                case 'SOFTWARE':
                    $this->_equipmentService[$key] = new SoftwareService();
                    break;
                case 'DOMAIN':
                    $this->_equipmentService[$key] = new DomainService();
                    break;
                default:
                    throw new \Exception(__FUNCTION__ . "에서 사용하지않는 Service를 요청하였습니다.: {$key}");
            }
        }
        return $this->_equipmentService[$key];
    }
    protected function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            case 'clientinfo_uid':
                $temps = [];
                foreach ($this->getClientService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                break;
            case 'SERVER':
            case 'CPU':
            case 'RAM':
            case 'STORAGE':
            case 'LINE':
            case 'IP':
            case 'DEFENCE':
            case 'SOFTWARE':
            case 'DOMAIN':
                $temps = [];
                // throw new \Exception(__FUNCTION__ . "에서 item_type이 지정되지 않았습니다.->{$item_type}");
                foreach ($this->getEquipmentService($field)->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //Index,FieldForm관련
}
