<?php

namespace App\Controllers\Admin;

use App\Helpers\HomeHelper;
use App\Services\Customer\ServiceService;
use App\Services\Customer\PaymentService;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class Home extends AdminController
{
    private $_service = null;
    private ?PaymentService $_PaymentService = null;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->content_title = lang("{$this->getService()->getClassName()}.title");
        $this->class_path .= $this->getService()->getClassName();
        $this->uri_path .= strtolower($this->getService()->getClassName('/')) . '/';
        $this->view_path .= 'welcome' . DIRECTORY_SEPARATOR;
    }

    final public function getService(): ServiceService
    {
        if (!$this->_service) {
            $this->_service = new ServiceService();
        }
        return $this->_service;
    }
    public function getHelper(): mixed
    {
        if (!$this->_helper) {
            $this->_helper = new HomeHelper();
        }
        return $this->_helper;
    }
    final public function getPaymentService(): PaymentService
    {
        if (!$this->_PaymentService) {
            $this->_PaymentService = new PaymentService();
        }
        return $this->_PaymentService;
    }
    protected function getResultSuccess_process(string $message = MESSAGES["SUCCESS"], ?string $actionTemplate = null): RedirectResponse|string
    {
        switch ($this->getAction()) {
            case 'index':
                $this->control = $this->getControlDatas();
                $this->getHelper()->setViewDatas($this->getViewDatas());
                $actionTemplate = $this->request->getVar('ActionTemplate') ?? $actionTemplate;
                if ($actionTemplate) {
                    $view_file = $this->view_path . $actionTemplate . DIRECTORY_SEPARATOR . $this->getAction();
                } else {
                    $view_file = $this->view_path . $this->getAction();
                }
                $result = view($view_file, ['viewDatas' => $this->getViewDatas()]);
                break;
            default:
                $result = parent::getResultSuccess_process($message, $actionTemplate);
        }
        return $result;
    }


    //Index,FieldForm관련
    public function index(): RedirectResponse|string
    {
        //각 Field 초기화
        $this->setAction(__FUNCTION__);
        //각 Field 초기화
        $this->initAction($this->getService()->getIndexFields());
        //Total 서버 현황
        $this->totalCounts = $this->getService()->getTotalCountsByType();
        //interval을 기준으로 최근 신규 서비스정보 가져오기
        $this->interval = intval($this->request->getVar('interval') ?? SERVICE_NEW_INTERVAL);
        $this->newServiceEntities = $this->getService()->getEntitiesByNewService($this->interval);
        $this->newServiceCount    = count($this->newServiceEntities);
        //서비스별 미납 Count
        $this->unPaids = $this->getPaymentService()->getUnPaidCount();
        $this->unPaidCount     = count($this->unPaids);
        helper(['form']);
        return $this->getResultSuccess_process();
    }
}
