<?php

namespace App\Controllers\Admin;

use App\Controllers\Trait\UpDownloadTrait;
use App\Entities\CategoryEntity;
use App\Models\CategoryModel;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class CategoryController extends AdminController
{
    use UpDownloadTrait;
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->_model = new CategoryModel();
        $this->_viewDatas['className'] = 'Category';
        $this->_viewPath .= strtolower($this->_viewDatas['className']);
        $this->_viewDatas['title'] = lang($this->_viewDatas['className'] . '.title');
        $this->_viewDatas['class_icon'] = CLASS_ICONS[strtoupper($this->_viewDatas['className'])];
        helper($this->_viewDatas['className']);
    }

    public function getFields(string $action = ""): array
    {
        $fields = [
            'name', "linkurl", "isaccess", "isread", "iswrite", "isreply", "isupload", "isdownload",
            "status", "head", "tail",
        ];
        switch ($action) {
            case "index":
            case "excel":
                return [
                    'name', "linkurl", "isaccess", "isread", "iswrite", "isreply", "isupload", "isdownload",
                    "status", "created_at"
                ];
                break;
            case "view":
                return [...$fields, "updated_at", "created_at"];
                break;
            default:
                return $fields;
                break;
        }
    }
    public function getFieldFilters(): array
    {
        return ["isaccess", "isread", "iswrite", "isreply", "isupload", "isdownload", "status"];
    }
    public function getFieldBatchFilters(): array
    {
        return parent::getFieldBatchFilters();
    }

    private function write_leftmenu($old_category, array $categorys)
    {
        //신규 대분류인경우 기존 카테고리 메뉴 만들기
        foreach ($categorys as $category) {
            $viewDatas = [
                'className' => $this->_viewDatas['className'],
                'category' => $category,
                'parent_category' => $old_category,
                'sibling_categorys' => $categorys
            ];
            $leftmenu = view($this->_viewPath . '/leftmenu_template', ['viewDatas' => $viewDatas]);
            file_put_contents(APPPATH . 'Views' . "/layouts/common/left_menu/leftmenu_{$category->getPrimaryKey()}.php", $leftmenu);
        }
    }
    private function build_leftmenu()
    {
        $old_category = null;
        $categorys = array();
        foreach ($this->_model->getEntitys(['status' => DEFAULTS['STATUS']]) as $entity) {
            if ($entity->getHierarchy_Depth() == 1) {
                if (is_null($old_category)) {
                    $old_category = $entity;
                } else if ($old_category->getPrimaryKey() != $entity->getPrimaryKey()) {
                    $this->write_leftmenu($old_category, $categorys);
                    $old_category = $entity;
                    $categorys = array();
                }
            } else {
                array_push($categorys, $entity);
            }
        }
        $this->write_leftmenu($old_category, $categorys);
    }

    //Insert관련
    protected function insert_process()
    {
        $entity = parent::insert_process();
        $this->build_leftmenu();
        return $entity;
    }
    //Update관련
    protected function update_process($entity)
    {
        $entity = parent::update_process($entity);
        $this->build_leftmenu();
        return $entity;
    }
    //Toggle관련
    protected function toggle_process($entity)
    {
        $entity = parent::toggle_process($entity);
        $this->build_leftmenu();
        return $entity;
    }
    //Reply관련
    protected function reply_process($entity)
    {
        $entity = parent::reply_process($entity);
        $this->build_leftmenu();
        return $entity;
    }
    //Delete 관련
    protected function delete_process($entity)
    {
        $entity = parent::delete_process($entity);
        $this->build_leftmenu();
        return $entity;
    }
}
