<?php

namespace App\Controllers\Admin\Cloudflare\API;

use App\Libraries\Log\Log;
use App\Models\Cloudflare\API\RecordModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\Cloudflare\API\Record;

class RecordController extends APIController
{
    private $_zone_uids = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_className .= '/Record';
        $this->_model = new RecordModel();
        $this->_defines = [
            'insert' => [
                'fields' => ['zone_uid', 'type', 'content', 'proxied', 'hosts'],
                'fieldFilters'  => [],
                'fieldRules' => [
                    'zone_uid'    => 'required|min_length[10]|max_length[200]',
                    'type'        => 'required|in_list[A,CNAME,MX,SPF,TXT,NS,INFO]',
                    'content'     => 'required|trim|min_length[4]',
                    'proxied'     => 'required|string',
                    'hosts'       => 'required|trim|string',
                ]
            ],
            'index' => [
                'fields' => ['zone_uid', 'host', 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'],
                'fieldFilters'  => ['zone_uid', 'type', 'fixed', 'proxied', 'locked'],
                'batchjobFilters' => ['content', 'proxied'],
            ],
            'excel' => [
                'fields' => ['zone_uid', 'host', 'type', 'content', 'ttl', 'proxied', 'locked', 'updated_at', 'created_at'],
                'fieldFilters'  => ['zone_uid', 'type', 'proxied', 'locked'],
            ],
        ];
        helper($this->_className);
        $this->_viewPath = strtolower($this->_className);
        $this->_viewDatas['title'] = lang($this->_className . '.title');
        $this->_viewDatas['className'] = $this->_className;
    }

    //Field별 Form Option용
    protected function getFieldFormOption(string $field): array
    {
        switch ($field) {
            case 'zone_uid':
                if (is_null($this->_zone_uids)) {
                    //모든 필요한 FormOption등 조기화작업 필요
                    $this->_zone_uids = [DEFAULT_EMPTY => lang($this->_className . '.label.' . $field) . ' 선택'];
                    foreach ($this->getZoneModel()->orderBy('domain', 'asc')->findAll() as $zone) {
                        $this->_zone_uids[$zone['uid']] = $zone['domain'];
                    }
                }
                return $this->_zone_uids;
                break;
            default:
                return parent::getFieldFormOption($field);
                break;
        }
    }

    //Insert관련
    protected function insert_validate()
    {
        parent::insert_validate();
        //Content 검증 Type이 A 인경우 IP형식 검사
        if ($this->_viewDatas['fieldDatas']['type'] === 'A') {
            if (!isIPAddress_CommonHelper($this->_viewDatas['fieldDatas']['content'], $this->_viewDatas['fieldDatas']['type'])) {
                throw new \Exception("{$this->_viewDatas['title']}의 {$this->_viewDatas['fieldDatas']['type']}, {$this->_viewDatas['fieldDatas']['content']} 형식 오류[사설IP 않됨]");
            }
        }
        //Host 검증
        $this->_viewDatas['fieldDatas']['hosts'] = explode("\n", $this->_viewDatas['fieldDatas']['hosts']);
        $cnt = 1;
        foreach ($this->_viewDatas['fieldDatas']['hosts'] as $host) {
            if (!isHost_CommonHelper($host)) {
                throw new \Exception("{$this->_viewDatas['title']}의 {$cnt}번째 {$host} 호스트명 형식 오류");
            }
            if (!$this->_model->isUniqueHost($this->_viewDatas['fieldDatas'][$this->_model::PARENT_FIELD], $host, $this->_viewDatas['fieldDatas']['content'])) {
                throw new \Exception("{$this->_viewDatas['title']}의 {$cnt}번째 {$host}:{$this->_viewDatas['fieldDatas']['content']}은 이미 등록된 호스트입니다.");
            }
            $cnt++;
        }
    }
    protected function insert_process()
    {
        foreach ($this->_viewDatas['fieldDatas']['hosts'] as $host) {
            $zone = $this->getZoneModel()->gentEntity($this->_viewDatas['fieldDatas'][$this->_model::PARENT_FIELD]);
            $this->insert_Host(
                $zone,
                $host,
                $this->_viewDatas['fieldDatas']['type'],
                $this->_viewDatas['fieldDatas']['content'],
                $this->_viewDatas['fieldDatas']['proxied']
            );
        }
    }

    //Update관련
    protected function update_process($entity)
    {
        $api = new Record($this->getZoneModel()->gentEntity($entity->getParentFieldData()));
        $entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
        return parent::update_process($entity);
    }
    //Toggle관련
    protected function toggle_process($entity)
    {
        $api = new Record($this->getZoneModel()->gentEntity($entity->getParentFieldData()));
        $entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
        return parent::toggle_process($entity);
    }
    //Batchjob관련
    protected function batchjob_process($entity)
    {
        $api = new Record($this->getZoneModel()->gentEntity($entity->getParentFieldData()));
        $entity = $api->update($entity, $this->_viewDatas['fieldDatas']);
        return parent::batchjob_process($entity);
    }
    //Delete 관련
    protected function delete_process($entity)
    {
        $api = new Record($this->getZoneModel()->gentEntity($entity->getParentFieldData()));
        $api->delete($entity);
        return parent::delete_process($entity);
    }
    //Sync관련
    protected function sync_process($entity)
    {
        $api = new Record($this->getZoneModel()->gentEntity($entity->getParentFieldData()));
        $entity = $api->sync($entity);
        return parent::sync_process($entity);
    }
    //CDN고정관련
    final public function cdnToggle(string $uid)
    {
        try {
            $entity = $this->_model->getEntity($uid);
            $fixedRecordModel = new \App\Models\Cloudflare\API\FixedRecordModel();
            if ($entity->fixed == 'on') {
                $entity->fixed = "off";
                $this->_model->save($entity);
                $fixedRecordModel->where('host', $entity->host)->delete();
                Log::add("info", "{$entity->getTitle()}의 fixed : on=>off");
            } else {
                $entity->fixed = "on";
                $this->_model->save($entity);
                //throw new \Exception($entity);
                $fixedRecordModel->insert(['host' => $entity->host]);
                Log::add("info", "{$entity->getTitle()}의 fixed : off=>on");
            }
            $message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
            Log::save("{$this->_viewDatas['title']} {$message}");
            return alert_CommonHelper($message, session()->get(RETURN_URL));
        } catch (\Exception $e) {
            $message = "{$entity->getTitle()} " . __FUNCTION__ . " 실패하였습니다.";
            Log::add("warning", $message . "<br>\n{$e->getMessage()}");
            Log::save("{$this->_viewDatas['title']} {$message}", false);
            return alert_CommonHelper($message, 'back');
        }
    }
}
