<?php

namespace App\Helpers\Equipment;

use App\Models\Equipment\ServerModel;

class ServerHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServerModel::TITLE);
    }
    private function getServerPartForm(string $field, mixed $value,  array $viewDatas, array $extras, string $type): string
    {
        $serverpartEntity = null;
        if (array_key_exists('entity', $viewDatas)) {
            $serverpartEntity = $viewDatas['entity']->getServerPartEntity($type);
        }
        $form = "";
        if ($serverpartEntity !== null) { //수정시 사용할 hidden uid
            $form = form_hidden("serverinfopartinfo_uid_{$type}", $serverpartEntity->getPK());
        }
        //기존 입력화면에서 return 된것인지?
        if ($value === null && $serverpartEntity !== null) {
            $value = $serverpartEntity->getPartInfoUID();
        }
        $form .= $this->form_dropdown_custom($field, $value, $viewDatas, $extras);
        return $form;
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'manufactur_at':
            case 'format_at':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' calender' : 'calender';
                $form = form_input($field, $value ?? "", $extras);
                break;
            case 'partinfo_uid_CPU':
                $form = $this->getServerPartForm($field, $value, $viewDatas, $extras, 'CPU');
                break;
            case 'partinfo_uid_RAM':
                $form = $this->getServerPartForm($field, $value, $viewDatas, $extras, 'RAM',);
                break;
            case 'partinfo_uid_DISK':
                $form = $this->getServerPartForm($field, $value, $viewDatas, $extras, 'DISK');
                break;
            case 'partinfo_uid_OS':
                $form = $this->getServerPartForm($field, $value, $viewDatas, $extras, 'OS');
                break;
            case 'partinfo_uid_DB':
                $form = $this->getServerPartForm($field, $value,  $viewDatas, $extras, 'DB');
                break;
            case 'partinfo_uid_SOFTWARE':
                $form = $this->getServerPartForm($field, $value,  $viewDatas, $extras, 'SOFTWARE');
                break;
            case 'ipinfo_uid':
            case 'csinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = $this->form_dropdown_custom($field, $value, $viewDatas, $extras);
                break;
            default:
                $form = parent::getFieldForm($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    }
    public function getFieldView(string $field, mixed $value, array $viewDatas, array $extras = []): string|null
    {
        switch ($field) {
            case 'manufactur_at':
            case 'format_at':
                $value = $value ? date("Y-m-d", strtotime($value)) : "";
                break;
            case 'serverpartinfo':
                $value = "";
                foreach (SERVER['PARTTYPES'] as $partType) {
                    $serverPartEntity = $viewDatas['entity']->getServerPartEntity($partType);
                    $value .= sprintf(
                        "<div>%s : %s%s %s</div>",
                        $serverPartEntity->getType(),
                        $serverPartEntity->getTitle(),
                        $serverPartEntity->getCnt() > 1 ? "*" . $serverPartEntity->getCnt() . "개" : "",
                        $serverPartEntity->getExtra() ?? ""
                    );
                }
                break;
            case 'ipinfo_uid':
                $value = "";
                foreach ($viewDatas['entity']->getIPEntities() as $ipEntity) {
                    $value .= sprintf("<div>%s%s %s</div>", $ipEntity->getTitle(), $ipEntity->getAmount());
                }
                break;
            case 'csinfo_uid':
                $value = "";
                foreach ($viewDatas['entity']->getCSEntities() as $csEntity) {
                    $value .= sprintf("<div>%s%s %s</div>", $csEntity->getTitle(), $csEntity->getAmount());
                }
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
    public function getListFilter(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'csinfo_uid':
            case 'ipinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = parent::getListFilter($field, $value, $viewDatas, $extras);
                break;
            default:
                $form = parent::getListFilter($field, $value, $viewDatas, $extras);
                break;
        }
        return $form;
    }

    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'modify':
                if (!$this->getMyAuth()->isAccessRole(['security'])) {
                    $action = $viewDatas['entity']->getCode();
                } else {
                    $action = parent::getListButton($action, $viewDatas['entity']->getCode(), $viewDatas, $extras);
                }
                break;
            case 'create':
            case 'delete':
            case 'batchjob':
            case 'batchjob_delete':
                $action = !$this->getMyAuth()->isAccessRole(['security']) ? "" : parent::getListButton($action, $label, $viewDatas, $extras);
                break;
            case 'history':
                $extras = ["class" => "btn btn-outline btn-primary btn-circle", "target" => "_self", ...$extras];
                $action = form_label(
                    ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/clienthistory?clientinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
