<?php

namespace App\Helpers\Equipment;

use RuntimeException;

class ServerPartHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'billing':
                $forms = [];
                // dd($viewDatas['formOptions']);
                array_shift($viewDatas['formOptions'][$field]['options']);
                foreach ($viewDatas['formOptions'][$field]['options'] as $key => $label)
                    $forms[] = form_radio($field, $key, $key == $value, $extras) . $label;
                $form = implode(" ", $forms);
                break;
            case 'part_uid':
                // dd($viewDatas);
                $type = array_key_exists('entity', $viewDatas) ? $viewDatas['entity']->getType() : $viewDatas['formDatas']['type'];
                // echo "Type:" . $type;
                // dd($viewDatas['formOptions'][$field]['options'][$type]);
                $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')";
                $form = $this->form_dropdown_custom($field, $viewDatas['formOptions'][$field]['options'][$type], $value, $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 'part_uid':
                if (!array_key_exists($value, $viewDatas['formOptions'][$field]['options'][$viewDatas['entity']->getType()])) {
                    throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$field}에서 {$value}에 해당하는 값이 존재하지 않습니다.");
                }
                $value = $viewDatas['formOptions'][$field]['options'][$viewDatas['entity']->getType()][$value]['text'];
                break;
            case 'SERVER':
                $value = form_label(
                    sprintf(
                        "[%s] %s",
                        lang("Equipment/Server.TYPE")[$viewDatas['serverEntity']->getType()],
                        $viewDatas['serverEntity']->getCode(),
                    ),
                    $field,
                    [
                        "data-src"       => "/admin/equipment/server/modify/{$viewDatas['serverEntity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            case 'CPU':
            case 'RAM':
            case 'DISK':
            case 'SOFTWARE':
            case 'IP':
            case 'CS':
                //파트 Entity
                $title =  $viewDatas['entity']->getTitle();
                $title .= $viewDatas['entity']->getCnt() > 1 ? "*" . $viewDatas['entity']->getCnt() . "개" : "";
                $title .= $viewDatas['entity']->getExtra() ? "[" . $viewDatas['entity']->getExtra() . "]" : "";
                if (array_key_exists('return',  $extras) && $extras['return'] == 'onlyText') {
                    $value = $title;
                } else {
                    $billing = $viewDatas['entity']->getBilling() === "" ? "" : ($viewDatas['entity']->getBilling() === PAYMENT['BILLING']["ONETIME"] ? ICONS['ONETIME'] : ($viewDatas['entity']->getBilling() === PAYMENT['BILLING']["MONTH"] ? ICONS['MONTH'] : ""));
                    $value = form_label(
                        $billing . $title,
                        $field,
                        [
                            "data-src"       => "/admin/equipment/serverpart/modify/{$viewDatas['entity']->getPK()}?type={$viewDatas['entity']->getType()}&ActionTemplate=popup",
                            "data-bs-toggle" => "modal",
                            "data-bs-target" => "#modal_action_form",
                            "class"          => "btn btn-sm form-label-sm text-primary",
                            ...$extras,
                        ]
                    );
                }
                break;
            default:
                $value = parent::getFieldView($field, $value, $viewDatas, $extras);
                break;
        }
        if (is_array($value)) {
            throw new RuntimeException(static::class . "->" . __FUNCTION__ . "에서 오류발생:{$field}에 해당하는 Return 값이 배열형식입니다.\n" . var_export($value, true));
        }
        return $value;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'IP':
            case 'CS':
            case 'CPU':
            case 'RAM':
            case 'DISK':
            case 'SOFTWARE':
                $action = form_label(
                    $label ? $label : ICONS["SERVER_ITEM_{$action}"],
                    $action,
                    [
                        "data-src"       => "/admin/equipment/serverpart/create?serverinfo_uid={$viewDatas['serverinfo_uid']}&type={$action}&ActionTemplate=popup",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class"          => "btn btn-sm form-label-sm text-primary",
                        ...$extras,
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
