<?php

namespace lib\Controllers;

use lib\Entities\GearlistEntity;
use lib\Services\GearlistService;
use lib\Services\ServerService;

class GearlistController extends DBMSController
{
    private ?GearlistService $_gearlistServicerService = null;
    private ?ServerService $_serverService = null;
    public function __construct(array $params = [])
    {
        parent::__construct($params);
        $this->setPath('gearlist');
    } //
    public function getGearlistService(): GearlistService
    {
        if ($this->_gearlistServicerService === null) {
            $this->_gearlistServicerService = new GearlistService();
        }
        return $this->_gearlistServicerService;
    }
    public function getServerService(): ServerService
    {
        if ($this->_serverService === null) {
            $this->_serverService = new ServerService();
        }
        return $this->_serverService;
    }
    //가용장비 현황 server_use.php
    //CLI 접속방법 : php index.php site/server/use
    //WEB 접속방법 : http://localhost site/server/use
    private function setMode(GearlistEntity $entity): GearlistEntity
    {
        $lineup_explode = explode('.', $entity->getSpec());
        $spec           = $lineup_explode[0];
        $cpu            = $entity->getCPUName();
        $entity->all = $this->getServerService()->getCountByMode("all", $cpu, $spec);
        $entity->use = $this->getServerService()->getCountByMode("use", $cpu, $spec);
        $entity->empty = $this->getServerService()->getCountByMode("empty", $cpu, $spec);
        $entity->format = $this->getServerService()->getCountByMode("format", $cpu, $spec);
        return $entity;
    }
    public function index()
    {
        $gearlistEntities = $this->getGearlistService()->getEntitiesForLineUp();
        //DB에 넣지않는 이유가 뭘까? DB에 넣으면 관리가 힘들어서?
        $gearlistEntities[] = new GearlistEntity(['process' => "INTEL i5(구세대)", 'spec' => "i5-2.xx", "cpuname" => "i5-2", 'price' => "23"]);
        $gearlistEntities[] = new GearlistEntity(['process' => "INTEL i7(구세대)", 'spec' => "i7-2.xx", "cpuname" => "i7-2", 'price' => "45"]);
        $gearlistEntities[] = new GearlistEntity(['process' => "INTEL i7(4세대)", 'spec' => "i7-4.xx", "cpuname" => "i7-4", 'price' => "45"]);
        $entities = [];
        foreach ($gearlistEntities as $idx => $gearlistEntity) {
            $entities[] = $this->setMode($gearlistEntity);
        }
        $this->entities = $entities;
        return $this->render(__FUNCTION__);
    }
} //Class
