<?php

namespace App\Controllers\Admin\Equipment;

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

class ServerController extends EquipmentController
{
  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(): ServerService
  {
    if (!$this->_service) {
      $this->_service = new ServerService();
    }
    return $this->_service;
  }
  //Index,FieldForm관련
  protected function getResultSuccess(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
  {
    switch ($this->getService()->getAction()) {
      case 'create_form':
      case 'modify_form':
      case 'index':
      case 'view':
        $this->service = $this->getService();
        $this->control = $this->getService()->getControlDatas();
        $this->getService()->getHelper()->setViewDatas($this->getViewDatas());
        $actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate ?? 'server';
        if ($actionTemplate) {
          $view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getService()->getAction();
        } else {
          $view_file = $this->view_path . $this->getService()->getAction();
        }
        $result = view($view_file, ['viewDatas' => $this->getViewDatas()]);
        break;
      default:
        $result = parent::getResultSuccess($message, $actionTemplate);
        break;
    }
    return $result;
  }
  private function pre_form_process(): void
  {
    //공통 처리
    $ips       = [];
    foreach ($this->getService()->getIPService()->getEntities(['status' => STATUS['AVAILABLE']]) as $ipEntity) {
      $ips[] = $ipEntity->getTitle();
    }
    $this->ips = $ips;
    $switchs       = [];
    foreach ($this->getService()->getSwitchService()->getEntities(['status' => STATUS['AVAILABLE']]) as $switchEntity) {
      $switchs[] = $switchEntity->getTitle();
    }
    $this->switchs = $switchs;
  }
  protected function create_form_process(): void
  {
    $formDatas = $this->getService()->getFormDatas();
    $formDatas['code'] = sprintf("%d%dXX-M%d", date("y"), ceil((int)date("m") / 3), $this->getService()->getLatestPK());
    $this->getService()->setFormDatas($formDatas);
    $this->pre_form_process();
    parent::create_form_process();
  }
  protected function modify_form_process(mixed $entity): ServerEntity
  {
    $this->pre_form_process();
    return parent::modify_form_process($entity);
  }
}
