<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Controller\Adminhtml\ProductPreview;

use Magento\Backend\App\Action;
class UploadCustomDesignPdf  extends \Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
{
    /**
     *
     */
    const CUSTOM_PREVIEWS = 'custom_previews';
    /**
     * @var \Magento\Framework\Filesystem\Driver\File
     */
    protected $file;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewFileFactory
     */
    protected $advancePreviewFileFactory;
    /**
     * @var \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory
     */
    protected $advancePreviewUploadFactory;
    /**
     * @var \Magetop\Advancepreview\Model\PreviewThreeFactory
     */
    protected $previewThreeFactory;
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory
     */
    protected $resPreviewThreeFactory;
    /**
     * @var \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory
     */
    protected $colPreviewThreeFactory;

    /**
     * UploadMedia constructor.
     * @param Action\Context $context
     * @param \Magento\Framework\Controller\Result\JsonFactory $jsonFactory
     * @param \Magento\Framework\Filesystem\Driver\File $file
     * @param \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory
     * @param \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory
     * @param \Magetop\Advancepreview\Model\PreviewThreeFactory $previewThreeFactory
     * @param \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory $resPreviewThreeFactory
     * @param \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory
     */
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Model\AdvancePreviewFileFactory $advancePreviewFileFactory,
        \Magetop\Advancepreview\Model\AdvancePreviewUploadFactory $advancePreviewUploadFactory,
        \Magetop\Advancepreview\Model\PreviewThreeFactory $previewThreeFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThreeFactory $resPreviewThreeFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->file = $file;
        $this->advancePreviewFileFactory = $advancePreviewFileFactory;
        $this->advancePreviewUploadFactory = $advancePreviewUploadFactory;
        $this->previewThreeFactory = $previewThreeFactory;
        $this->resPreviewThreeFactory = $resPreviewThreeFactory;
        $this->colPreviewThreeFactory = $colPreviewThreeFactory;

    }
    function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>'Something wrong when upload file',
        ];
        $params = $this->getRequest()->getParams();
        $productId = isset($params['product_id']) ? (int)$params['product_id'] : 0;
        $designType = isset($params['design_type']) ? trim($params['design_type']) : 'pdf';

        $customFolder = self::CUSTOM_PREVIEWS.'/product_id_'.$productId.'/own_pdf';
        $options['file_name'] = 'own_design.pdf';
        if($designType == 'ai') {
            $options['file_name'] = 'own_design.ai';
            $customFolder = self::CUSTOM_PREVIEWS.'/product_id_'.$productId.'/own_ai';
        }
        $advancePreviewFile = $this->advancePreviewFileFactory->create();
        $customFolderUpload = $advancePreviewFile->getBaseDir($customFolder);
        $uploadModel = $this->advancePreviewUploadFactory->create();
        $media = $advancePreviewFile->getAdvanceMediaFolder();
        if(!$this->file->isExists($customFolderUpload)) {
            $this->file->createDirectory($customFolderUpload,0775);
        }

//        $options['allow_type'] = ['pdf','ai'];
        $name = $designType == 'ai' ? 'txt_design_preview_ai' : 'txt_design_preview_pdf';
        $customPreview = $uploadModel->uploadFileAndGetName($name,$customFolderUpload,$media.$customFolder,$params, $options);
        if($customPreview != '') {
            $response['status'] = 'success';
            $response['message'] = 'message '. $customPreview;
        }

        return $this->getAjaxResponse($response);
    }
}
