<?php

namespace App\Controllers\Admin\Customer;

use App\Controllers\Admin\Customer\CustomerController;
use App\Helpers\Customer\ServiceHelper;
use App\Services\Customer\ServiceService;
use App\Services\Equipment\IpService;

use App\Services\Equipment\LineService;

use App\Services\Equipment\ServerService;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;


class ServiceController extends CustomerController
{
    private ?LineService $_lineService = null;
    private ?ServerService $_serverService = null;
    private ?IpService $_ipService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->title = lang("{$this->getService()->getClassName()}.title");
        $this->class_path .= $this->getService()->getClassName();
        $this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
        // $this->view_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;

    }
    public function getService(): ServiceService
    {
        if (!$this->_service) {
            $this->_service = new ServiceService($this->request);
        }
        return $this->_service;
    }
    public function getHelper(): ServiceHelper
    {
        if (!$this->_helper) {
            $this->_helper = new ServiceHelper($this->request);
        }
        // dd($this->_helper);
        return $this->_helper;
    }
    final public function getLineService(): LineService
    {
        if (!$this->_lineService) {
            $this->_lineService = new LineService();
        }
        return $this->_lineService;
    }
    final public function getServerService(): ServerService
    {
        if (!$this->_serverService) {
            $this->_serverService = new ServerService();
        }
        return $this->_serverService;
    }
    final public function getIpService(): IpService
    {
        if (!$this->_ipService) {
            $this->_ipService = new IpService();
        }
        return $this->_ipService;
    }
    protected function getFormFieldOption(string $field, array $options): array
    {
        switch ($field) {
            case 'lineinfo_uid':
                $temps = [];
                foreach ($this->getLineService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            case 'serverinfo_uid':
                $temps = [];
                foreach ($this->getServerService()->getEntities() as $entity) {
                    $temps[$entity->getPK()] = $entity->getTitle();
                }
                $options[$field] = $temps;
                // dd($options);
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        // dd($options);
        return $options;
    }
    //Index,FieldForm관련

    protected function create_process(): mixed
    {
        $entity = parent::create_process();
        //사용중인 서버로 변경
        $this->getServerService()->setOccupied($this->getServerService()->getEntity($entity->getServerInfoUID()));
        return $entity;
    }
    protected function index_process(): array
    {
        $fields = [
            'fields' => ['clientinfo_uid', 'type', 'billing_at', 'rack', 'LINE', 'SERVER', 'IP', 'CPU', 'RAM',  'DISK', 'SOFTWARE', 'DEFENCE', 'start_at', 'status'],
        ];
        $this->init('index',  $fields);
        // $this->modal_type = 'modal_fetch_v2'; //기본은 modal_iframe임
        $entities = [];
        // foreach (parent::index_process() as $entity) {
        //     $entity->setPartEntities("CPU", $this->getCpuService()->getPartEntities($entity));
        //     $entity->setPartEntities("RAM", $this->getRamService()->getPartEntities($entity));
        //     $entity->setPartEntities("DISK", $this->getDiskService()->getPartEntities($entity));
        //     $entities[] = $entity;
        // }
        return $entities;
    }
}
