<?php

namespace App\Libraries\Mangboard;

use App\Libraries\CommonLibrary;
use App\Libraries\MyStorage\MangboardStorage;
use App\Models\Mangboard\FileModel;
use App\Entities\Mangboard\UserEntity;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\BoardsEntity;
use App\Entities\Mangboard\BoardEntity;

class FileLibrary extends CommonLibrary
{
    private $_model = null;
    private $_boards_entity = null;
    private $_user_entity = null;
    public function __construct(BoardsEntity $boards_entity, UserEntity $user_entity)
    {
        parent::__construct();
        $this->_boards_entity = $boards_entity;
        $this->_user_entity = $user_entity;
    }
    public function getModel(): FileModel
    {
        if ($this->_model === null) {
            return $this->_model = new FileModel();
        }
        return $this->_model;
    }
    public function getBoardsEntity(): BoardsEntity
    {
        return $this->_boards_entity;
    }
    public function getUserEntity(): UserEntity
    {
        return $this->_user_entity;
    }
    public function create(BoardEntity $board_entity, MangboardStorage $storage): FileEntity
    {
        //파일관리 table에 등록
        $formDatas = [];
        //Board PID 넣기
        $formDatas['board_pid'] = $board_entity->getPk();
        $formDatas['user_pid'] = $this->getUserEntity()->getPK();
        $formDatas['user_name'] = $this->getUserEntity()->getTitle();
        $formDatas['board_name'] = $this->getBoardsEntity()->getTitle();
        $formDatas['table_name'] = $this->getModel()->getTable();
        $formDatas['file_path'] = $storage->getBasePath() . DIRECTORY_SEPARATOR . $storage->getPath() . DIRECTORY_SEPARATOR . $storage->getOriginName();
        $formDatas['file_name'] = $storage->getOriginName();
        $formDatas['file_type'] = $storage->getMimeType();
        $formDatas['file_caption'] = $storage->getOriginName();
        $formDatas['file_alt'] = $storage->getOriginName();
        $formDatas['file_description'] = "Filedata";
        $formDatas['file_size'] = $storage->getFileSize();
        $formDatas['file_sequence'] = $storage->getOriginSequence();
        $formDatas['reg_date'] = date("Y-m-d H:i:s");
        return $this->getModel()->create($formDatas);
    }
    public function createByCrawler(BoardEntity $board_entity, array $storages): void
    {
        try {
            foreach ($storages as $storage) {
                $entity = $this->create($board_entity, $storage);
                log_message("notice", sprintf(
                    "%s -> %s 게시물의 %s번째:%s 파일 등록 완료",
                    __FUNCTION__,
                    $board_entity->getTitle(),
                    $storage->getOriginSequence(),
                    $entity->getTitle()
                ));
            }
        } catch (\Exception $e) {
            log_message("notice", sprintf(
                "\n---%s -> %s 게시물의 %s번째:%s 파일 등록 오류---\n%s\n--------------------------------\n",
                __FUNCTION__,
                $board_entity->getTitle(),
                $storage->getOriginSequence(),
                $storage->getOriginName(),
                $e->getMessage()
            ));
        }
    }
}
