<?php


namespace Magetop\Advancepreview\Controller\Order;

use Magento\Framework\App\Action;
class SaveNewDesign extends Action\Action
{
    protected $previewOrderItemsFactory;
    protected $resPreviewOrderItemsFactory;
    protected $colPreviewOrderItemsFactory;
    protected $jsonFactory;

    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magetop\Advancepreview\Model\PreviewOrderItemsFactory $previewOrderItemsFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderItemsFactory $resPreviewOrderItemsFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderItems\CollectionFactory $colPreviewOrderItemsFactory
    )
    {
        parent::__construct($context);
        $this->previewOrderItemsFactory = $previewOrderItemsFactory;
        $this->resPreviewOrderItemsFactory = $resPreviewOrderItemsFactory;
        $this->colPreviewOrderItemsFactory = $colPreviewOrderItemsFactory;
        $this->jsonFactory = $jsonFactory;
    }
    public function execute()
    {
        $params = $this->getRequest()->getParams();
        $productId = isset($params['product_id']) ? (int)$params['product_id'] : 0;
        $textureWidth = isset($params['texture_width']) ? (int)$params['texture_width'] : 0;
        $isReupProduct = isset($params['is_reup_product']) ? (int)$params['is_reup_product'] : 0;
        $textureHeight = isset($params['texture_height']) ? (int)$params['texture_height'] : 0;
        $zoom = isset($params['zoom']) ? (int)$params['zoom'] : 0;
        $light = isset($params['light']) ? (int)$params['light'] : 0;
        $texture = isset($params['texture']) ? trim($params['texture']) : '';
        $oldWwnFolder = isset($params['own_folder']) ? trim($params['own_folder']) : '';
        $namePreview = isset($params['name_preview']) ? trim($params['name_preview']) : '';
        $originalFile =  isset($params['original_file']) ? trim($params['original_file']) : '';
        $itemId =  isset($params['item_id']) ? (int)$params['item_id'] : 0;
        $previewItem = $this->getPreviewItemByItemId($itemId);
        $ownFolder = $this->getRequest()->getParam('new_own','');
        $dataUpdate = [
            'status' => 'new',
            'custom_item_id' => $previewItem->getId(),
            'updated_at' => date('Y-m-d H:i:s'),
            'preview_model'=>$ownFolder,
            'zoom'=>$zoom,
            'light'=>$light,
            'model_preview'=>$ownFolder,
            'name_preview'=>$namePreview,
            'product_id'=>$productId,
        ];
        $response = [
            'status'=>'error',
        ];
        $isSave = $this->savePreviewOrderItem($dataUpdate);
        if($isSave > 0) {
            $response['status'] = 'success';
        }
        return $this->jsonFactory->create()->setData($response);
    }
    public function getPreviewItemByItemId($itemId = 0) {
        $collection = $this->colPreviewOrderItemsFactory->create()
            ->addFieldToSelect(array('status'))
            ->addFieldToFilter('item_id',$itemId);
        $previewItem = $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
        return $previewItem;

    }
    protected function savePreviewOrderItem($previewData = []) {
        $model = $this->previewOrderItemsFactory->create();
        $model->setData($previewData);
        $id = 0;
        try {
            $this->resPreviewOrderItemsFactory->create()->save($model);
            $id =  $model->getId();
        }
        catch (\Exception $exception) {

        }
        return $id;

    }
}
