<?php

namespace App\Libraries\MyStorage\Mangboard;

use App\Entities\Mangboard\BoardEntity;
use App\Entities\Mangboard\FileEntity;
use App\Entities\Mangboard\UserEntity;
use App\Libraries\MyStorage\FileLibrary as MyStorageLibrary;
use App\Libraries\MyStorage\Mangboard\SmallImageLibrary;
use App\Models\Mangboard\FileModel;

class FileLibrary extends MyStorageLibrary
{
    private $_file_model = null;
    private $_user_entity = null;
    private $_board_name = null;
    private $_board_table = null;
    private $_board_entity = null;
    private $_file_entity = null;
    private $_imageLibrary = null;
    public function __construct(string $path)
    {
        parent::__construct($path);
        $this->_imageLibrary = new SmallImageLibrary();
        $fullPath = WRITEPATH . $this->getUploadPath() . DIRECTORY_SEPARATOR . $this->getPath();
        $this->_imageLibrary->setDebug($this->getDebug());
        $this->_imageLibrary->setSourcePath($fullPath);
        $this->_imageLibrary->setDestinationPath($fullPath);
    }
    public function getFileModel(): FileModel
    {
        if ($this->_file_model === null) {
            return $this->_file_model = new FileModel();
        }
        return $this->_file_model;
    }
    public function getFileEntity(): FileEntity
    {
        return $this->_file_entity;
    }
    private function setFileEntity(FileEntity $file_entity): void
    {
        $this->_file_entity = $file_entity;
    }
    public function getBoardName(): string
    {
        if ($this->_board_name === null) {
            throw new \Exception("Board Name이 없습니다.");
        }
        return $this->_board_name;
    }
    public function setBoardName(string $board_name): void
    {
        $this->_board_name = $board_name;
    }
    public function getBoardTable(): string
    {
        if ($this->_board_table === null) {
            throw new \Exception("Board Table이 없습니다.");
        }
        return $this->_board_table;
    }
    public function setBoardTable(string $board_table): void
    {
        $this->_board_table = $board_table;
    }
    public function getUserEntity(): UserEntity
    {
        if ($this->_user_entity === null) {
            throw new \Exception("사용자정보가 없습니다.");
        }
        return $this->_user_entity;
    }
    public function setUserEntity(UserEntity $user_entity): void
    {
        $this->_user_entity = $user_entity;
    }
    public function getBoardEntity(): BoardEntity
    {
        return $this->_board_entity;
    }
    public function setBoardEntity(BoardEntity $board_entity): void
    {
        $this->_board_entity = $board_entity;
    }

    public function save(): static
    {
        parent::save();
        //파일관리 table에 등록
        $formDatas = [];
        //Board PID 넣기
        $formDatas['board_pid'] = $this->getBoardEntity()->getPk();
        //작은이미지생성후 Path/파일명 넣기
        $fileInfos = pathinfo($this->_imageLibrary->getDestinationPath() . DIRECTORY_SEPARATOR . $this->getOriginName(), PATHINFO_ALL);
        $dstFile = $fileInfos['filename'] . "_small." . $fileInfos['extension'];
        $this->_imageLibrary->setDestinationFile($dstFile);
        $formDatas['file_path'] = $this->_imageLibrary->create($this->getOriginName());

        $formDatas['user_pid'] = $this->getUserEntity()->getPK();
        $formDatas['user_name'] = $this->getUserEntity()->getTitle();
        $formDatas['board_name'] = $this->getBoardName();
        $formDatas['table_name'] = $this->getBoardTable();
        $formDatas['file_name'] = $this->getOriginName();
        $formDatas['file_type'] = $this->getMimeType();
        $formDatas['file_caption'] = $this->getOriginName();
        $formDatas['file_alt'] = $this->getOriginName();
        $formDatas['file_description'] = "Filedata";
        $formDatas['file_size'] = $this->getFileSize();
        $formDatas['file_sequence'] = $this->getOriginSequence();
        $formDatas['reg_date'] = date("Y-m-d H:i:s");
        $entity = $this->getFileModel()->create($formDatas);
        log_message("notice", sprintf(
            "%s %s번째 작업 완료",
            __FUNCTION__,
            $this->getOriginSequence()
        ));
        $this->setFileEntity($entity);
        return $this;
    }
}
