<?php

namespace App\Controllers\Admin\Customer;

use App\Entities\Customer\ServiceItemEntity;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

use App\Helpers\Customer\ServiceHelper;
use App\Services\Customer\ServiceService;
use App\Services\Customer\ServiceItemService;
use App\Services\Equipment\CodeService;

class ServiceController extends CustomerController
{
    private ?ServiceItemService $_serviceItemService = null;
    private ?CodeService $_codeService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->content_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);
        }
        return $this->_helper;
    }
    public function getCodeService(): CodeService
    {
        if (!$this->_codeService) {
            $this->_codeService = new CodeService($this->request);
        }
        return $this->_codeService;
    }
    public function getServiceItemService(): ServiceItemService
    {
        if (!$this->_serviceItemService) {
            $this->_serviceItemService = new ServiceItemService($this->request);
        }
        return $this->_serviceItemService;
    }
    protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        switch ($this->getAction()) {
            case 'index':
                $result = parent::getResultSuccess($message, $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'service');
                break;
            default:
                $result = parent::getResultSuccess($message, $actionTemplate);
                break;
        }
        return $result;
    }

    protected function setWordConditionForList(): void
    {
        $this->word = $this->request->getVar('word');
        if ($this->word !== null && $this->word !== '') {
            if ($this->getHelper()->isIPAddress($this->word, 'ipv4')) {
                $this->getService()->setSearchIp($this->word);
            } else {
                $this->getService()->getModel()->setList_WordFilter($this->word);
            }
        }
    }
    //Index,FieldForm관련
    protected function index_process(): array
    {
        //LINE,IP,SERVER등 추가 FilterOption 셋팅용
        foreach (SERVICE_ITEM_TYPES as $item_type => $label) {
            $options = $this->getService()->getServiceItemLinkService($item_type)->getEntities();
            $this->setFilterFieldOption($item_type, $options);
        }
        // dd($this->getFilterFieldOptions());
        $entities = [];
        foreach (parent::index_process() as $entity) {
            foreach (SERVICE_ITEM_TYPES as $item_type => $label) {
                $entity->setItemEntities(
                    $item_type,
                    $this->getServiceItemService()->getEntities(['serviceinfo_uid' => $entity->getPK(), 'item_type' => $item_type])
                );
            }
            $entities[] = $entity;
        }
        // $entities = parent::index_process();
        return $entities;
    }
}
