<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDailyShipping;


use Magento\Backend\App\Action;

class Items extends \Magetop\Advancepreview\Controller\Adminhtml\PreviewDailyShipping
{
    protected $previewDailyShippingFactory;
    protected $resPreviewDailyShippingFactory;
    protected $colPreviewDailyShippingFactory;
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magetop\Advancepreview\Model\PreviewDailyShippingFactory $previewDailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShippingFactory $resPreviewDailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShipping\CollectionFactory $colPreviewDailyShippingFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->previewDailyShippingFactory = $previewDailyShippingFactory;
        $this->resPreviewDailyShippingFactory = $resPreviewDailyShippingFactory;
        $this->colPreviewDailyShippingFactory = $colPreviewDailyShippingFactory;
    }

    public function execute()
    {
        $response = [
            'status'=>'error',
            'message'=>'error',
            'html'=>''
        ];
        try {
            $productId = (int)$this->getRequest()->getParam('product_id',0);
            if($productId > 0 && !$this->checkIsDataEdit($productId)) {
                $this->generateSampleData($productId);
            }
            $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\PreviewDailyShipping')->setTemplate('Magetop_Advancepreview::product/daily_shipping/items.phtml')->toHtml();
            $response['status'] = 'success';
            $response['message'] = 'success';
        }
        catch (\Exception $exception) {
            $response['message'] = $exception->getMessage();
        }
        return $this->getAjaxResponse($response);
    }
    protected function checkIsDataEdit($productId = 0) {
        $collection = $this->colPreviewDailyShippingFactory->create()
            ->addFieldToSelect(array('daily_shipping_id'))
            ->addFieldToFilter('product_id',$productId)
            ->setPageSize(1)
            ->setOrder('daily_shipping_id','desc');
        return $collection && $collection->getSize() > 0 ? true : false;
    }
    protected function generateSampleData($productId) {
        $createData = date('Y-m-d H:i:s');
        $sampleItems  = [
            [
                'product_id'=>$productId,
                'delivery_type'=>'Standard',
                'delivery_days'=>0,
                'status'=>1,
                'amount'=>0,
                'description'=>'',
                'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'delivery_type'=>'Priority',
                'delivery_days'=>0,
                'status'=>1,
                'amount'=>0,
                'description'=>'',
                'created_at'=>$createData,
            ], [
                'product_id'=>$productId,
                'delivery_type'=>'Express',
                'delivery_days'=>0,
                'status'=>1,
                'amount'=>0,
                'description'=>'',
                'created_at'=>$createData,
            ],
        ];
        foreach ($sampleItems as $sampleItem) {
            $this->saveSampleDataItem($sampleItem);
        }
    }
    protected function saveSampleDataItem($dataSave) {
        $model = $this->previewDailyShippingFactory->create();
        $model->setData($dataSave);
        try {
            $this->resPreviewDailyShippingFactory->create()->save($model);
        }
        catch (\Exception $exception) {

        }
    }

}
