<?php


namespace Magetop\Advancepreview\Block\Adminhtml\Product\Edit;

use Magento\Backend\Block\Template;
class Form extends Template
{
    protected $file;
    protected $previewHelper;
    protected $colPreviewThreeFactory;
    public function __construct(
        Template\Context $context,
        \Magento\Framework\Filesystem\Driver\File $file,
        \Magetop\Advancepreview\Helper\Data $previewHelper,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewThree\CollectionFactory $colPreviewThreeFactory,
        array $data = []
    )
    {
        parent::__construct($context, $data);
        $this->file = $file;
        $this->previewHelper = $previewHelper;
        $this->colPreviewThreeFactory = $colPreviewThreeFactory;
    }
    public function getPreviewById($previewId, $productId) {
        $collection = $this->colPreviewThreeFactory->create()
            ->addFieldToSelect(array('*'))
            ->addFieldToFilter('threed_id',$previewId);
        $folderPreview = time();
        $previewData = [
            'threed_id'=>0,
            'product_id'=>$productId,
            'title'=>'',
            'is_default'=>2,
            'filename'=>'',
            'folder_preview'=>$folderPreview,
            'file_preview'=>'',
            'position'=>0,
            'preview_light'=>0,
            'preview_zoom'=>0,
            'created_at'=>date('Y-m-d H:i:s')
        ];
        if($collection && $collection->getSize() > 0) {
            $previewData = $collection->getFirstItem()->getData();
        }
        return $previewData;
    }
    public function getPreviewByProductId($productId = 0) {
        $collection = $this->colPreviewThreeFactory->create()
            ->addFieldToSelect('*')
            ->addFieldToFilter('product_id',$productId)
            ->setOrder('is_default','ASC')
            ->setOrder('position','ASC');
//        echo (string)$collection->getSelect();
        return $collection;
    }
    public function getAjaxUrl() {
        $uploadUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/uploadMedia');
        $itemsUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/items');
        $editUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/edit');
        $dellUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/dellItem');
        $ownDesignUrl = $this->previewHelper->getOpdAdminUrl('advancepreview/productPreview/uploadCustomDesignPdf');
        return [
            'upload_url'=>$uploadUrl,
            'item_url'=>$itemsUrl,
            'edit_url'=>$editUrl,
            'dell_url'=>$dellUrl,
            'own_design_url'=>$ownDesignUrl,
        ];
    }
    public function getSamplePdfFile($poductId) {
        $mediaPath = $this->getMediaDirectory()->getAbsolutePath();
//        if(is_f)
        $image = '';
        if($this->file->isExists($mediaPath.'advancepreview/custom_previews/product_id_'.$poductId.'/own_pdf/own_design.pdf')) {
            $image = $this->getMediaUrl().'advancepreview/custom_previews/product_id_'.$poductId.'/own_pdf/own_design.pdf';
        }
        return $image;
    }
    public function getSampleAiFile($poductId) {
        $mediaPath = $this->getMediaDirectory()->getAbsolutePath();
//        if(is_f)
        $image = '';
        if($this->file->isExists($mediaPath.'advancepreview/custom_previews/product_id_'.$poductId.'/own_ai/own_design.ai')) {
            $image = $this->getMediaUrl().'advancepreview/custom_previews/product_id_'.$poductId.'/own_ai/own_design.ai';
        }
        return $image;
    }
    protected function getMediaUrl() {
        $mediaUrl = '';
        try {
            $mediaUrl = $this ->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA );
        }
        catch (\Exception $exception) {

        }
        return $mediaUrl;
    }
}
