<?php

namespace App\Helpers\Equipment;

use App\Entities\Equipment\PartEntity;
use App\Models\Equipment\ServerPartModel;

class ServerPartHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
        $this->setTitleField(field: ServerPartModel::TITLE);
    }
    protected function form_dropdown_common_process(string $field, mixed $value, array $viewDatas, array $extras = [], array $attributes = []): string
    {
        $html = "";
        switch ($field) {
            case 'part_uid':
                $attribute = "";
                foreach ($viewDatas['control']['field_optons'][$field] as $key => $entity) {
                    $isSelected = $key == $value ? ' selected' : '';
                    $isDisabled  = "";
                    if (in_array($viewDatas['control']['action'], ['create_form', 'index'])) {
                        if ($entity->getStatus() != $entity::DEFAULT_STATUS)
                            $isDisabled = " disabled";
                    }
                    $attribute = "";
                    foreach ($attributes as $attribute_tag => $attribute_value) {
                        $attribute .= sprintf(" %s=\"%s\"", $attribute_tag, $entity instanceof PartEntity ? $entity->$attribute_value() : $attribute_value);
                    }
                    $title = $entity->getStatus() != $entity::DEFAULT_STATUS ? "서비스중." : "";
                    $title .= $entity->getCustomTitle();
                    $html .= sprintf("<option value=\"%s\"%s%s%s>%s</option>", $key, $isSelected, $isDisabled, $attribute, $title);
                }
                break;
            default:
                $html = parent::form_dropdown_common_process($field, $value, $viewDatas, $extras, $attributes);
                break;
        }
        return $html;
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'SWITCH':
            case 'IP':
            case 'CS':
            case 'CPU':
            case 'RAM':
            case 'DISK':
            case 'DB':
            case 'OS':
            case 'SOFTWARE':
            case 'part_uid':
                if ($value === null && array_key_exists('entity', $viewDatas)) {
                    $value = $viewDatas['entity']->getServerEntity()->getPK();
                }
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $extras['onChange'] = "document.querySelector('input[name=\'amount\']').value = this.options[this.selectedIndex].getAttribute('data-price')";
                $attributes = ['data-type' => 'getType', 'data-price' => 'getPrice'];
                $form = $this->form_dropdown_common($field, $value, $viewDatas, $extras, $attributes);
                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 'CPU':
            case 'RAM':
            case 'DISK':
            case 'OS':
            case 'DB':
            case 'SOFTWARE':
            case 'SWITCH':
            case 'IP':
            case 'CS':
            case 'part_uid':
                if (!array_key_exists($field, $viewDatas['entities'])) {
                    return "";
                }
                $temps = [];
                foreach ($viewDatas['entities'][$field] as $entity) {
                    $title = $entity->getTitle();
                    $title .= $entity->getCnt() > 1 ? "*" . $entity->getCnt() . "개" : "";
                    $title .= $entity->getExtra() !== "" ? "/" . $entity->getExtra() : "";
                    if (array_key_exists('return_type', $extras) && $extras['return_type'] === 'text') {
                        $temps[] = $title;
                    } else {
                        $temps[] = form_label(
                            $title,
                            $field,
                            [
                                "data-src"       => "/admin/equipment/serverpart/modify/{$entity->getPK()}?type={$entity->getType()}&ActionTemplate=popup",
                                "data-bs-toggle" => "modal",
                                "data-bs-target" => "#index_action_form",
                                "class" => "btn btn-sm btn-outline btn-circle",
                                "target" => "_self"
                            ]
                        );
                    }
                }
                $value = implode(",", $temps);
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            echo __METHOD__ . "에서 오류: {$field}의 값이 Array형태입니다";
            exit;
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'SWITCH':
            case 'IP':
            case 'CS':
            case 'CPU':
            case 'RAM':
            case 'DISK':
            case 'OS':
            case 'DB':
            case 'SOFTWARE':
                $extras = [
                    "class" => "btn btn-sm btn-outline btn-circle",
                    "target" => "_self",
                    ...$extras,
                ];
                $action = form_label(
                    $label ? $label : ICONS["SERVER_ITEM_{$action}"],
                    $action,
                    [
                        "data-src"       => "/admin/equipment/serverpart?serverinfo_uid={$viewDatas['serverinfo_uid']}&type={$action}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#index_action_form",
                        ...$extras,
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
