<?php

namespace App\Cells\Equipment;

use App\Helpers\Equipment\ServerPartHelper;
use App\Services\Equipment\ServerService;

class ServerCell extends EquipmentCell
{

  public function __construct()
  {
    parent::__construct(service('equipment_serverservice'));
  }

  //서비스 방식에 따른 서비스별 Count
  final public function totalCountDashboard(array $params): string
  {
    $totalCounts = [
      'all' => [
        'normal' => ['tokyo' => 0, 'chiba' => 0],
        'defence' => ['tokyo' => 0, 'chiba' => 0],
        'dedicated' => ['tokyo' => 0, 'chiba' => 0],
        'alternative' => ['tokyo' => 0, 'chiba' => 0],
        'vpn' => ['tokyo' => 0, 'chiba' => 0],
        'event' => ['tokyo' => 0, 'chiba' => 0],
        'test' => ['tokyo' => 0, 'chiba' => 0],
        'summary_tokyo' => 0,
        'summary_chiba' => 0,
        'summary_all' => 0,
      ]
    ];
    foreach (array_keys(SITES) as $site) {
      $totalCounts[$site] = $this->getService()->getTotalServiceCount(['serviceinfo.site' => $site]);
      $totalCounts['all']['normal']['tokyo'] += $totalCounts[$site]['normal']['tokyo'];
      $totalCounts['all']['normal']['chiba'] += $totalCounts[$site]['normal']['chiba'];
      $totalCounts['all']['defence']['tokyo'] += $totalCounts[$site]['defence']['tokyo'];
      $totalCounts['all']['defence']['chiba'] += $totalCounts[$site]['defence']['chiba'];
      $totalCounts['all']['dedicated']['tokyo'] += $totalCounts[$site]['dedicated']['tokyo'];
      $totalCounts['all']['dedicated']['chiba'] += $totalCounts[$site]['dedicated']['chiba'];
      $totalCounts['all']['alternative']['tokyo'] += $totalCounts[$site]['alternative']['tokyo'];
      $totalCounts['all']['alternative']['chiba'] += $totalCounts[$site]['alternative']['chiba'];
      $totalCounts['all']['vpn']['tokyo'] += $totalCounts[$site]['vpn']['tokyo'];
      $totalCounts['all']['vpn']['chiba'] += $totalCounts[$site]['vpn']['chiba'];
      $totalCounts['all']['event']['tokyo'] += $totalCounts[$site]['event']['tokyo'];
      $totalCounts['all']['event']['chiba'] += $totalCounts[$site]['event']['chiba'];
      $totalCounts['all']['test']['tokyo'] += $totalCounts[$site]['test']['tokyo'];
      $totalCounts['all']['test']['chiba'] += $totalCounts[$site]['test']['chiba'];
      $totalCounts['all']['summary_tokyo'] += $totalCounts[$site]['tokyo_summary'];
      $totalCounts['all']['summary_chiba'] += $totalCounts[$site]['chiba_summary'];
      $totalCounts['all']['summary_all'] = $totalCounts['all']['summary_tokyo'] + $totalCounts['all']['summary_chiba'];
    }
    $template = array_key_exists('template', $params) ? $params['template'] : __FUNCTION__;
    return view('cells/server/' . $template, [
      'serviceCellDatas' => [
        'totalCounts' => $totalCounts,
      ]
    ]);
  }
  public function detail(array $params): string
  {
    $this->getService()->getActionForm()->form_init_process(__FUNCTION__);
    if (!array_key_exists('serviceEntity', $params)) {
      return static::class . '->' . __FUNCTION__ . "에서 오류발생: 서비스 정보가 정의되지 않았습니다.";
    }
    $entities = $this->getService()->getEntities(['serviceinfo_uid' => $params['serviceEntity']->getPK()]);
    $template = array_key_exists('template', $params) ? $params['template'] : __FUNCTION__;
    return view('cells/server/' . $template, [
      'serverCellDatas' => [
        'formFilters' => $this->getService()->getActionForm()->getFormFilters(),
        'formOptions' => $this->getService()->getActionForm()->getFormOptions(),
        'helper' => $this->getService()->getHelper(),
        'entities' => $entities,
        'serviceEntity' => $params['serviceEntity'],
        'serverPartHelper' => new ServerPartHelper(),
      ]
    ]);
  }
}
