<?php

namespace App\Controllers\Admin;

use App\Helpers\MapurlHelper;
use App\Models\MapurlModel;

use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class MapurlController extends AdminController
{
    private $_model = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->class_name .= "Mapurl";
        $this->class_path .= $this->class_name;
        $this->title = lang("{$this->class_path}.title");
        $this->helper = new MapurlHelper();
    }
    protected function getModel(): MapurlModel
    {
        if ($this->_model === null) {
            $this->_model = new MapurlModel();
        }
        return $this->_model;
    }
    protected function getFormFieldOption(string $field, array $options = []): array
    {
        switch ($field) {
            default:
                $options = parent::getFormFieldOption($field, $options);
                break;
        }
        return $options;
    }
    protected function getFormData(string $field, array $formDatas): array
    {
        switch ($field) {
            default:
                $formDatas = parent::getFormData($field, $formDatas);
                break;
        }
        return $formDatas;
    }
    private function init(string $action): void
    {
        $this->action = $action;
        $this->fields = [$this->getModel()::TITLE, 'newurl', 'status'];
        $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
        $this->filter_fields = ['status'];
        $this->field_options = $this->getFormFieldOptions($this->filter_fields);
    }
    private function remaping_process(): void
    {
        //모든 필요한 FormOption등 조기화작업 필요
        $this->getModel()->where('status', DEFAULTS['STATUS']);
        $this->getModel()->orderBy('oldurl', 'ASC');
        $this->entitys = $this->getModel()->getEntitys();
        $remap_page = view($this->view_path . DIRECTORY_SEPARATOR . strtolower($this->class_path) . DIRECTORY_SEPARATOR . 'remap_template', ["viewDatas" => $this->getViewDatas()]);
        $remap_path = FCPATH . DIRECTORY_SEPARATOR . "mapurl";
        //디렉토리 생성 여부 확인
        if (!is_dir($remap_path)) {
            mkdir($remap_path, 0755, true);
        }
        //htmlBuild용
        if (!file_put_contents($remap_path . '/index.html', $remap_page)) {
            throw new \Exception(__FUNCTION__ . "에서 " . $remap_path . "/index.html Write 실패");
        }
    }
    //생성
    public function create_form(): RedirectResponse|string
    {
        $this->init('create');
        return $this->create_form_procedure();
    }
    protected function create_process(): void
    {
        parent::create_process();
        $this->remaping_process();
    }
    public function create(): RedirectResponse|string
    {
        $this->init(__FUNCTION__);
        return $this->create_procedure();
    }
    //수정
    public function modify_form(string $uid): RedirectResponse|string
    {
        $this->init('modify');
        return $this->modify_form_procedure($uid);
    }
    //(modify,toggle,batchjob사용)
    protected function modify_process(string $uid): void
    {
        parent::modify_process($uid);
        $this->remaping_process();
    }
    public function modify(string $uid): RedirectResponse|string
    {
        $this->init(__FUNCTION__);
        return $this->modify_procedure($uid);
    }
    //일괄처리작업
    public function batcjob(): RedirectResponse
    {
        $this->action = __FUNCTION__;
        $this->fields = ['status'];
        $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
        return $this->batcjob_procedure();
    }
    //삭제
    protected function delete_process(string $uid): void
    {
        parent::delete_process($uid);
        $this->remaping_process();
    }
    //  리스트
    public function index(): string
    {
        $this->action = __FUNCTION__;
        $this->fields = [$this->getModel()::TITLE, 'newurl', 'status'];
        $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
        $this->filter_fields = ['status'];
        $this->field_options = $this->getFormFieldOptions($this->filter_fields);
        $this->batchjob_fields = ['status'];
        return $this->list_procedure();
    }

    // Download
    public function download(string $output_type, $uid = false): DownloadResponse|string
    {
        $this->action = __FUNCTION__;
        $this->fields = [$this->getModel()::TITLE, 'newurl', 'status'];
        $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
        $this->filter_fields = ['status'];
        $this->field_options = $this->getFormFieldOptions($this->filter_fields);
        $this->batchjob_fields = ['status'];
        return $this->download_procedure($output_type, $uid);
    }
}
