<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\ProductPreview;


use Magento\Backend\App\Action;

/**
 * Class UploadMedia
 * @package Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
 */
class UploadMedia 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;

    }

    /**
     * @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\Result\Json|\Magento\Framework\Controller\ResultInterface|void
     * @throws \Magento\Framework\Exception\FileSystemException
     */
    function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>'Something wrong when adding 3d model',
            'html'=>''
        ];
        $params = $this->getRequest()->getParams();
        $productId = isset($params['product_id']) ? $params['product_id'] : 0;
        $previewId = isset($params['threed_id']) ? $params['threed_id'] : 0;
        $title = isset($params['title']) ? trim($params['title']) : '';
        if($title == '') {
            $response['message'] = 'Title is empty';
            return $this->getAjaxResponse($response);
        }

        $customFolder = self::CUSTOM_PREVIEWS.'/product_id_'.$productId;
        $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['file_name'] = 'model-1.zip';
        $options['allow_type'] = ['zip'];
        $customPreview = $uploadModel->uploadFileAndGetName('preview_media',$customFolderUpload,$media.$customFolder,$params, $options);
        if($customPreview == '' && $previewId <= 0) {
            $response['message'] = 'Something wrong when upload 3d model';
            return $this->getAjaxResponse($response);
        }
        $dataSave = array(
            'product_id'=>$productId,
            'title'=>$title,
            'filename'=>$customPreview,
            'created_at'=>date('Y-m-d H:i:s')
        );
        if($previewId > 0) {
            $dataSave['threed_id'] = $previewId;
            unset($dataSave['created_at']);
        }
        if($previewId > 0 && $customPreview == '') {
            unset($dataSave['filename']);
        }
        $isSave = $this->saveThreeModel($dataSave);
        if($isSave) {
            $response['status'] = 'success';
            $response['message'] = 'You have added 3d model success';
            $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\Form')->setTemplate('Magetop_Advancepreview::product/items.phtml')->toHtml();

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

    /**
     * @param array $dataSave
     * @return bool
     */
    protected function saveThreeModel($dataSave = []) {
        $model = $this->previewThreeFactory->create();
        $model->setData($dataSave);
        try {
            $this->resPreviewThreeFactory->create()->save($model);
            return true;
        }
        catch (\Exception $exception) {

        }
        return  true;
    }
}
