<?php

namespace lib\Controllers\DBMS;

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

class ServerController extends DBMSController
{
    private ?ServerService $_serverService = null;
    private ?GearlistService $_gearlistService = null;
    public function __construct()
    {
        parent::__construct();
        $this->getView()->setPath('server');
    } //
    public function getServerService(): ServerService
    {
        if ($this->_serverService === null) {
            $this->_serverService = new ServerService();
        }
        return $this->_serverService;
    }
    public function getGearlistSrvice(): GearlistService
    {
        if ($this->_gearlistService === null) {
            $this->_gearlistService = new GearlistService();
        }
        return $this->_gearlistService;
    }

    //방어 server_use.php
    //CLI 접속방법 : php index.php site/server/use
    //WEB 접속방법 : http://localhost site/server/use
    private function use_getGearlistEntity(GearlistEntity $entity): GearlistEntity
    {
        $lineup_explode = explode('.', $entity->getSpec());
        $spec           = $lineup_explode[0];
        $cpu            = $entity->getCPUName();
        $this->getServerService()->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        $entity->all = $this->getServerService()->getCount();

        $this->getServerService()->getModel()->where("server_use_status", "n");
        $this->getServerService()->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        $entity->use = $this->getServerService()->getCount();

        $this->getServerService()->getModel()->where("server_use_status", "y");
        $this->getServerService()->getModel()->like(["server_cpuname" => "%$cpu%", "server_spec" => "%$spec%"]);
        $entity->empty = $this->getServerService()->getCount();

        $this->getServerService()->getModel()->where("server_use_status", "y");
        $this->getServerService()->getModel()->where("server_fomat_date !='NULL'");
        $this->getServerService()->getModel()->like(["server_cpuname" => "%$cpu%"]);
        $entity->format = $this->getServerService()->getCount();
        return $entity;
    }
    public function use(array $params): string
    {
        $temps              = [];
        $gearlineupEntities = $this->getGearlistSrvice()->getLineUpEntities();
        foreach ($gearlineupEntities as $idx => $entity) {
            $entity = $this->use_getGearlistEntity($entity);
        }
        $oldServers = [
            ['process' => "INTEL i5(구세대)", 'spec' => "i5-2.xx", "cpuname" => "i5-2", 'price' => "23"],
            ['process' => "INTEL i7(구세대)", 'spec' => "i7-2.xx", "cpuname" => "i7-2", 'price' => "45"],
            ['process' => "INTEL i7(4세대)", 'spec' => "i7-4.xx", "cpuname" => "i7-4", 'price' => "45"],
        ];
        foreach ($oldServers as $oldServer) {
            $temps[] = $this->use_getGearlistEntity(new GearlistEntity($oldServer));
        }
        $this->gearlineupEntities = $temps;
        return $this->render(__FUNCTION__);
    }
} //Class
