<?php

namespace lib\Controllers\DBMS;

use lib\Helpers\ServiceHelper;
use lib\Services\ClientService;
use lib\Utils\Pagination;

class NavigatorController extends DBMSController
{

    private ?ClientService $_clientService = null;
    public function __construct()
    {
        parent::__construct();
        $this->getView()->setPath('navigator');
        $this->helper = new ServiceHelper();
    } //
    public function getClientService(): ClientService
    {
        if ($this->_clientService === null) {
            $this->_clientService = new ClientService();
        }
        return $this->_clientService;
    }

    //부가서비스 : 닷디펜더,딥파인더 등, deepfinder_list.php,dotdefender_list.php 
    //CLI 접속방법 : php index.php site/navigator/ipsearch
    //WEB 접속방법 : http://localhost/site/navigator/ipsearch
    public function ipsearch(array $params): string
    {
        $ip = array_key_exists('ip', $params) ?? null;
        if (!$ip) {
            $ip = $this->request->get('ip') ?? null;
        }
        //전체 고객정보
        $this->clients = $this->getClientService()->getEntities();
        //IP형식이 ipv4인지 확인 후 값가져오기
        $this->curPage = intval($params['curPage'] ?? $this->request->get('curPage') ?? 1);
        $this->perPage = intval($params['perPage'] ?? $this->request->get('perPage') ?? VIEW_LIST_PERPAGE);
        [$this->total, $this->entities] = $this->getServiceService()->getEntitiesForIpSearch($this->curPage, $this->perPage, $ip, $this->helper->isIPAddress($ip));
        $this->ip  = $ip;
        $this->pagination = new Pagination($this->total, (int)$this->curPage, (int)$this->perPage);
        return $this->render(__FUNCTION__);
    }
} //Class
