<?php

namespace App\Helpers\Equipment;

use RuntimeException;

class ServerHelper extends EquipmentHelper
{
    public function __construct()
    {
        parent::__construct();
    }
    public function getFieldForm(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'clientinfo_uid':
            case 'serviceinfo_uid':
            case 'switchinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = form_dropdown($field, $viewDatas['formOptions'][$field]['options'], $value, $extras);
                break;
            case 'chassisinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $extras['onChange'] = "document.querySelector('input[name=\'title\']').value = this.options[this.selectedIndex].getAttribute('data-title'); document.querySelector('input[name=\'price\']').value = this.options[this.selectedIndex].getAttribute('data-price');";
                $form = $this->form_dropdown_custom($field, $viewDatas['formOptions'][$field]['options'], $value, $extras);
                break;
            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 'ip':
            case 'ilo_ip':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $form = parent::getFieldForm($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 'chassisinfo_uid':
                if (!array_key_exists($value, $viewDatas['formOptions'][$field]['options'])) {
                    throw new RuntimeException(static::class . '->' . __FUNCTION__ . "에서 오류발생: {$field}에서 {$value}에 해당하는 값이 존재하지 않습니다.");
                }
                $value = $viewDatas['formOptions'][$field]['options'][$value]['text'];
                break;
            case 'ip': //값 그대료 표시
            case 'ilo_ip': //값 그대료 표시
                break;
            case 'price':
                $value = number_format($value) . "원";
                break;
            case 'part':
                $value = view_cell("\App\Cells\Equipment\ServerPartCell::parttable", [
                    'serverinfo_uid' => $viewDatas['entity']->getPK(),
                    'types' => SERVERPART['ALL_PARTTYPES'],
                    'template' => 'serverlist',
                ]);
                break;
            case 'viewer':
                $value = $viewDatas['entity']->getViewer() ? $this->getListButton('console', "", $viewDatas) : "";
                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 getListFilter(string $field, mixed $value, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'switchinfo_uid':
            case 'ip':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $filter = parent::getListFilter($field, $value, $viewDatas, $extras);
                break;
            case 'chassisinfo_uid':
                $extras['class'] = array_key_exists('class', $extras) ? $extras['class'] . ' select-field' : 'select-field';
                $filter = $this->form_dropdown_custom($field, $viewDatas['formOptions'][$field]['options'], $value, $extras);
                break;
            default:
                $filter = parent::getListFilter($field, $value, $viewDatas, $extras);
                break;
        }
        return $filter;
    }
    public function getListLabel(string $field, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($field) {
            case 'part':
                $label = parent::getListLabel($field, $label, $viewDatas, $extras);
                $label .= "<span class=\"float-start rounded border border-primary\" style=\"cursor:pointer;\" onclick=\"copyServerPartsToClipboard()\">ALL 📋</span>";
                break;
            default:
                $label = parent::getListLabel($field, $label, $viewDatas, $extras);
                break;
        }
        return $label;
    }
    public function getListButton(string $action, string $label, array $viewDatas, array $extras = []): string
    {
        switch ($action) {
            case 'history':
                $action = $label ? $label : form_label(
                    $label ? $label : ICONS['HISTORY'],
                    $action,
                    [
                        "data-src" => "/admin/customer/clienthistory?clientinfo_uid={$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm btn-primary form-label-sm",
                        ...$extras
                    ]
                );
                break;
            case 'console':
                $action = $label ? $label : form_label(
                    $label ? $label : ICONS['CONSOLE'],
                    $action,
                    [
                        "data-src" => "/admin/equipment/server/console/{$viewDatas['entity']->getPK()}",
                        "data-bs-toggle" => "modal",
                        "data-bs-target" => "#modal_action_form",
                        "class" => "btn btn-sm btn-primary form-label-sm",
                        ...$extras
                    ]
                );
                break;
            default:
                $action = parent::getListButton($action, $label, $viewDatas, $extras);
                break;
        }
        return $action;
    }
}
