<?php

namespace App\Services\Equipment;

use App\Entities\Equipment\IPEntity;
use App\Entities\Equipment\LineEntity;
use App\Entities\Equipment\ServerEntity;
use App\Models\Equipment\IPModel;
use App\Services\Equipment\EquipmentService;
use App\Services\Equipment\LineService;

class IPService extends EquipmentService
{
    private ?LineService $_lineService = null;
    public function __construct()
    {
        parent::__construct(new IPModel());
        $this->addClassName('IP');
    }
    public function getFormFields(): array
    {
        return [
            'fields' => [
                "lineinfo_uid",
                "serverinfo_uid",
                "ip",
                "price",
                "amount",
                "status",
            ],
            'filters' => [
                'old_clientinfo_uid',
                'clientinfo_uid',
                'serverinfo_uid',
                "lineinfo_uid",
                'status'
            ],
        ];
    }
    public function getIndexFields(): array
    {
        return [
            'fields' => [
                'lineinfo_uid',
                'ip',
                'price',
                "amount",
                'status',
                'clientinfo_uid',
                'serverinfo_uid',
                'old_clientinfo_uid'
            ],
            'filters' => [
                'old_clientinfo_uid',
                'clientinfo_uid',
                'serverinfo_uid',
                "lineinfo_uid",
                'status'
            ],
            'batchjob_fields' => ['status'],
        ];
    }
    public function getBatchjobFields(): array
    {
        return ['status'];
    }
    final public function getLineService(): LineService
    {
        if (!$this->_lineService) {
            $this->_lineService = new LineService();
        }
        return $this->_lineService;
    }
    //기본기능
    //FieldForm관련용
    public function getFormFieldOption(string $field, $options = []): array
    {
        switch ($field) {
            case 'lineinfo_uid':
                $options = $this->getLineService()->getEntities();
                break;
            case 'old_clientinfo_uid':
                $options = $this->getClientService()->getEntities();
                break;
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    //회선정보에 따른 IP생성
    public function createByLineInfo(LineEntity $entity, string $ip): IPEntity
    {
        $formDatas = [
            'lineinfo_uid' => $entity->getPK(),
            'ip'           => $ip,
            'status'       => IPEntity::DEFAULT_STATUS,
        ];
        return $this->create($formDatas);
    }
    private function getFormDatasByServer(ServerEntity $serverEntity, array $formDatas): array
    {
        $temps = [
            "serverinfo_uid" => $serverEntity->getPK(),
            "clientinfo_uid" => $serverEntity->getClientInfoUID(),
            "serviceinfo_uid" => $serverEntity->getServiceInfoUID(),
            "status" => IPEntity::STATUS_OCCUPIED
        ];
        return $temps;
    }
    //생성
    public function createByServer(ServerEntity $serverEntity, array $formDatas): IPEntity
    {
        $entity = $this->getEntity($formDatas("ipinfo_uid"));
        if (!$entity) {
            throw new \Exception("{$formDatas("ipinfo_uid")}에 대한 IP정보를 찾을수 없습니다.");
        }
        $entity = parent::modify(
            $entity,
            $this->getFormDatasByServer(
                $serverEntity,
                $formDatas
            )
        );
        return $entity;
    }
    //List 검색용
    //OrderBy 처리
    public function setOrderBy(mixed $field = null, mixed $value = null): void
    {
        $this->getModel()->orderBy('INET_ATON(ip)', 'ASC');
        parent::setOrderBy($field, $value);
    }
}
