<?php

namespace App\Controllers;

use CodeIgniter\HTTP\DownloadResponse;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Html;
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
use Psr\Log\LoggerInterface;

abstract class MVController extends CommonController
{
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->class_path = "";
    }
    abstract protected function getModel(): mixed;
    //Field별 Form Rule용
    protected function setFormFieldRule($field, Validation $validation, string $action): Validation
    {
        switch ($field) {
            default:
                $validation->setRule($field, $field, $this->getModel()->getFieldRule($action, $field));
                break;
        }
        return $validation;
    }
    final protected function setFormFieldRules(array $fields, Validation $validation, string $action): Validation
    {
        foreach ($fields as $field) {
            $validation = $this->setFormFieldRule($field, $validation, $action);
        }
        return $validation;
    }
    //Field별 Form Option용
    protected function getFormFieldOption(string $field, array $options): array
    {
        switch ($field) {
            default:
                // dd(lang($this->class_path . '.' . strtoupper($field)));
                $options[$field] = lang($this->class_path . '.' . strtoupper($field));
                break;
        }
        return $options;
    }
    final protected function getFormFieldOptions(array $fields, array $options = []): array
    {
        foreach ($fields as $field) {
            if (is_array($field)) {
                throw new \Exception(__FUNCTION__ . "에서 field array 입니다.\n" . var_export($field, true));
            }
            $options = $this->getFormFieldOption($field, $options);
        }
        // dd($options);
        return $options;
    }
    //전송된 데이터
    protected function getFormData(string $field, array $formDatas): array
    {
        switch ($field) {
            default:
                $formDatas[$field] = $this->request->getVar($field);
                break;
        }
        return $formDatas;
    }
    final protected function getFormDatas(array $formDatas = []): array
    {
        foreach ($this->fields as $field) {
            $formDatas = $this->getFormData($field, $formDatas);
        }
        return $formDatas;
    }
    //  생성
    protected function create_validate(string $action, array $fields): void
    {
        //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요
        $this->validation = $this->setFormFieldRules($fields, service('validation'), $action);
        if (!$this->validation->withRequest($this->request)->run()) {
            throw new \Exception("{$this->class_name} 작업 데이터 검증 오류발생\n" . implode(
                "\n",
                $this->validation->getErrors()
            ));
        }
    }
    protected function create_form_process(): void {}
    final protected function create_form_procedure(): RedirectResponse|string
    {
        try {
            helper(['form']);
            $this->create_form_process();
            $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
            return view(
                $this->view_path . "create",
                data: ['viewDatas' => $this->getViewDatas()]
            );
        } catch (\Exception $e) {
            log_message("error", $e->getMessage());
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
    protected function create_process(): void
    {
        $this->create_validate($this->action, $this->fields);
        $this->formDatas = $this->getFormDatas();
        $this->entity = $this->getModel()->create(formDatas: $this->formDatas);
    }
    final protected function create_procedure(): RedirectResponse|string
    {
        //Transaction Start
        $this->getModel()->transStart();
        try {
            helper(['form']);
            $this->create_process();
            $this->getModel()->transCommit();
            $this->message = "{$this->class_name} : 생성작업이 완료되었습니다.";
            return view(
                $this->view_path . "view",
                data: ['viewDatas' => $this->getViewDatas()]
            );
        } catch (\Exception $e) {
            //Transaction Rollback
            $this->getModel()->transRollback();
            log_message("error", $e->getMessage());
            return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
        }
    }
    //  수정
    protected function modify_validate(string $action, array $fields): void
    {
        //변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요
        $this->validation = $this->setFormFieldRules($fields, service('validation'), $action);
        if (!$this->validation->withRequest($this->request)->run()) {
            throw new \Exception("{$this->class_name} 작업 데이터 검증 오류발생\n" . implode(
                "\n",
                $this->validation->getErrors()
            ));
        }
    }
    protected function modify_form_process(string $uid): void
    {
        $this->entity = $this->getModel()->getEntityByPK(intval($uid));
        if ($this->entity === null) {
            throw new \Exception("해당 정보를 찾을수 없습니다.");
        }
    }
    final protected function modify_form_procedure(string $uid): RedirectResponse|string
    {
        try {
            helper(['form']);
            $this->modify_form_process($uid);
            $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
            return view(
                $this->view_path . "modify",
                data: ['viewDatas' => $this->getViewDatas()]
            );
        } catch (\Exception $e) {
            log_message("error", $e->getMessage());
            return redirect()->back()->with('error', $e->getMessage());
        }
    }
    protected function modify_process(string $uid): void
    {
        $this->modify_validate($this->action, $this->fields);
        $this->formDatas = $this->getFormDatas();
        //자신정보정의
        $this->entity = $this->getModel()->getEntityByPK($uid);
        if ($this->entity === null) {
            throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다.");
        }
        $this->entity = $this->getModel()->modify($this->entity, $this->formDatas);
    }
    final protected function modify_procedure(string $uid): RedirectResponse|string
    {
        //Transaction Start
        $this->getModel()->transStart();
        try {
            helper(['form']);
            $this->modify_process($uid);
            $this->getModel()->transCommit();
            $this->message = "{$this->class_name} : 수정작업이 완료되었습니다.";
            return view(
                $this->view_path . "view",
                data: ['viewDatas' => $this->getViewDatas()]
            );
        } catch (\Exception $e) {
            //Transaction Rollback
            $this->getModel()->transRollback();
            log_message("error", $e->getMessage());
            return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
        }
    }
    //일괄처리작업
    final protected function batcjob_procedure(): RedirectResponse
    {
        //Transaction Start
        $this->getModel()->transStart();
        try {
            //변경할 UIDS
            $uids = $this->request->getVar('batchjob_uids');
            if (!$uids) {
                throw new \Exception("적용할 리스트를 선택하셔야합니다.");
            }
            foreach (explode(",", $uids) as $uid) {
                $this->modify_process($uid);
            }
            $this->message = "{$this->class_name} : 일괄처리작업이 완료되었습니다.";
            log_message("notice", __FUNCTION__ . $this->message);
            // 이전 URL로 리다이렉트
            return redirect()->to($this->popPreviousUrl())->with('error', $this->message);
        } catch (\Exception $e) {
            //Transaction Rollback
            $this->getModel()->transRollback();
            log_message("error", $e->getMessage());
            return redirect()->back()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
        }
    }
    //단일필드작업
    final public function toggle(string $uid, string $field): RedirectResponse
    {
        //Transaction Start
        $this->getModel()->transStart();
        try {
            $this->action = __FUNCTION__;
            $this->fields = [$field];
            $this->modify_process($uid);
            $this->message = "{$this->class_name} : Toggle 수정작업이 완료되었습니다.";
            $this->getModel()->transCommit();
            log_message("notice", __FUNCTION__ . $this->message);
            // 이전 URL로 리다이렉트
            return redirect()->to($this->popPreviousUrl())->with('error', $this->message);
        } catch (\Exception $e) {
            //Transaction Rollback
            $this->getModel()->transRollback();
            log_message("error", $e->getMessage());
            return redirect()->back()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
        }
    }
    //삭제
    protected function delete_process(string $uid): void
    {
        //자신정보정의
        $this->entity = $this->getModel()->getEntityByPK(intval($uid));
        if ($this->entity === null) {
            throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
        }
        $this->entity = $this->getModel()->delete($this->entity->getPK());
    }
    final public function delete(string $uid): RedirectResponse
    {
        //Transaction Start
        $this->getModel()->transStart();
        try {
            $this->delete_process($uid);
            $this->message = "{$this->class_name} : 삭제작업이 완료되었습니다.";
            $this->getModel()->transCommit();
            log_message("notice", __FUNCTION__ . $this->message);
            // 이전 URL로 리다이렉트
            return redirect()->to($this->popPreviousUrl())->with('error', $this->message);
        } catch (\Exception $e) {
            //Transaction Rollback
            $this->getModel()->transRollback();
            log_message("error", $e->getMessage());
            return redirect()->back()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
        }
    }
    //  리스트
    private function list_condition_process(): void
    {
        //조건절 처리
        foreach ($this->filter_fields as $field) {
            $this->$field = $this->request->getVar($field) ?: DEFAULTS['EMPTY'];
            if ($this->$field !== DEFAULTS['EMPTY']) {
                $this->getModel()->setList_FieldFilter($field, $this->$field);
            }
        }
        //검색어 처리
        $this->word = $this->request->getVar('word') ?: DEFAULTS['EMPTY'];
        if ($this->word !== DEFAULTS['EMPTY']) {
            $this->getModel()->setList_WordFilter($this->word);
        }
        //검색일 처리
        $this->start = $this->request->getVar('start') ?: DEFAULTS['EMPTY'];
        $this->end = $this->request->getVar('end') ?: DEFAULTS['EMPTY'];
        $this->getModel()->setList_DateFilter($this->start, $this->end);
    }
    //Totalcount 처리
    private function list_total_process(): int
    {
        $this->list_condition_process();
        $total_count = $this->getModel()->countAllResults();
        // echo $this->getModel()->getLastQuery();
        return $total_count;
    }
    //PageNation 처리
    private function list_pagination_process($pager_group = 'default', int $segment = 0, $template = 'default_full'): string
    {
        //Page, Per_page필요부분
        $this->page = (int) $this->request->getVar('page') ?: 1;
        $this->per_page = (int) $this->request->getVar('per_page') ?: intval(getenv("mvc.default.list.per_page"));
        //줄수 처리용
        $page_options = array("" => "줄수선택");
        for ($i = $this->per_page; $i <= $this->total_count; $i += $this->per_page) {
            $page_options[$i] = $i;
        }
        $this->page_options = $page_options;
        // 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성
        // 2.app/Config/Pager.php/$templates에 'bootstrap_full => 'Pagers\bootstrap_full',
        // 'bootstrap_simple' => 'Pagers\bootstrap_simple', 추가
        $pager = service("pager");
        // $this->getModel()->paginate($this->per_page, $pager_group, $this->page, $segment);
        $pager->makeLinks(
            $this->page,
            $this->per_page,
            $this->total_count,
            $template,
            $segment,
            $pager_group
        );
        $this->page = $pager->getCurrentPage($pager_group);
        $this->total_page = $pager->getPageCount($pager_group);
        return $pager->links($pager_group, $template);
    }
    private function list_entitys_process(): array
    {
        $this->list_condition_process();
        //Sorting 처리
        $this->order_field = $this->request->getVar('order_field') ?: DEFAULTS['EMPTY'];
        $this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
        $this->getModel()->setList_OrderBy(
            $this->order_field !== DEFAULTS['EMPTY'] &&
                $this->order_value !== DEFAULTS['EMPTY'] ? "{$this->order_field} {$this->order_value}" : ""
        );
        if ($this->page) {
            $this->getModel()->limit(
                $this->per_page,
                $this->page * $this->per_page - $this->per_page
            );
        }
        $entitys = $this->getModel()->select($this->getModel()->getTable() . '.*')->findAll();
        log_message("debug", $this->getModel()->getLastQuery());
        return $entitys;
    }
    final protected function list_procedure(): string
    {
        try {
            helper(['form']);
            //URL처리
            $this->uri = $this->request->getUri();
            //total 처리
            $this->total_count = $this->list_total_process();
            //pagenation 처리
            $this->pagination = $this->list_pagination_process();
            //모델 처리
            $this->entitys = $this->list_entitys_process();
            // 현재 URL을 스택에 저장
            $this->pushCurrentUrl();
            return view(
                $this->view_path . "index",
                ['viewDatas' => $this->getViewDatas()]
            );
        } catch (\Exception $e) {
            log_message("error", $e->getMessage());
            return $this->helper->alert($e->getMessage());
        }
    }

    //OUPUT Document 관련
    private function output_save_process(string $document_type, mixed $loaded_data): array
    {
        $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel";
        switch ($document_type) {
            case 'excel':
                $file_name = sprintf("%s_%s.xlsx", $this->class_name, date('Y-m-d_Hm'));
                $writer = IOFactory::createWriter($loaded_data, 'Xlsx');
                $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
                break;
            case 'pdf':
                $file_name = sprintf("%s_%s.pdf", $this->class_name, date('Y-m-d_Hm'));
                $writer = new Mpdf($loaded_data);
                $writer->save($full_path . DIRECTORY_SEPARATOR . $file_name);
                break;
            default:
                if (!is_file($full_path)) {
                    throw new \Exception("첨부파일이 확인되지 않습니다.\n");
                }
                // //oupuput directly
                // header("Content-Type: application/vnd.ms-excel");
                // header(sprintf("Content-Disposition: attachment; filename=%s", urlencode($output_name)));
                // header("Expires: 0");
                // header("Cache-Control: must-revalidate");
                // header("Pragma: public");
                // header("Content-Length:" . filesize($full_path));
                // return $writer->save('php://output');
                break;
        }
        return array($full_path, $file_name);
    }
    //File Download관련
    final protected function download_procedure(string $output_type, $uid = false): DownloadResponse|RedirectResponse
    {
        try {
            helper(['form']);
            //URL처리
            $this->uri = $this->request->getUri();
            switch ($output_type) {
                case 'excel':
                case 'pdf':
                    // string buffer에서 읽어오는 경우
                    $this->entitys = $this->list_entitys_process();
                    $html = view(
                        'templates' . DIRECTORY_SEPARATOR . $this->action,
                        ['viewDatas' => $this->getViewDatas()]
                    );
                    //data loading
                    $reader = new Html();
                    $loaded_data = $reader->loadFromString($html);
                    list($full_path, $file_name) = $this->output_save_process($output_type, $loaded_data);
                    $full_path .= DIRECTORY_SEPARATOR . $file_name;
                    break;
                default:
                    if (!$uid) {
                        throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다.");
                    }
                    $this->entity = $this->getModel()->getEntityByPK(intval($uid));
                    if ($this->entity === null) {
                        throw new \Exception("{$uid} 정보를 찾을수 없습니다.");
                    }
                    list($file_name, $uploaded_filename) = $this->entity->getDownlaodFile();
                    $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename;
                    break;
            }
            return $this->response->download($full_path, null)->setFileName($file_name);
        } catch (\Exception $e) {
            log_message("error", $e->getMessage());
            return $this->helper->alert($e->getMessage());
        }
    }
}
