<?php


namespace Magetop\Advancepreview\Controller\Adminhtml\PreviewDiscount;


use Magento\Backend\App\Action;
use Magetop\Advancepreview\Model\PreviewDiscountFactory;
use Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount;

class Items extends \Magetop\Advancepreview\Controller\Adminhtml\ProductPreview
{
    protected $previewDiscountFactory;
    protected $resPreviewDiscountFactory;
    protected $colPreviewDiscountFactory;
    public function __construct(
        Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
        \Magetop\Advancepreview\Model\PreviewDiscountFactory $previewDiscountFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscountFactory $resPreviewDiscountFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDiscount\CollectionFactory $colPreviewDiscountFactory
    )
    {
        parent::__construct($context, $jsonFactory);
        $this->previewDiscountFactory = $previewDiscountFactory;
        $this->resPreviewDiscountFactory =  $resPreviewDiscountFactory;
        $this->colPreviewDiscountFactory = $colPreviewDiscountFactory;
    }

    public function execute()
    {
        $response = [
            'status'=>'error',
            'html'=>''
        ];
        try {
            $productId = (int)$this->getRequest()->getParam('product_id',0);
            if($productId > 0 && !$this->checkDiscountAvailable($productId)) {
                $this->generateDefaultData($productId);
            }
            $response['html'] = $this->_view->getLayout()->createBlock('Magetop\Advancepreview\Block\Adminhtml\Product\Edit\PreviewDiscount')->setTemplate('Magetop_Advancepreview::product/discount/items.phtml')->toHtml();
            $response['status'] = 'success';
        }
        catch (\Exception $exception) {

        }
        return $this->getAjaxResponse($response);
    }
    protected function checkDiscountAvailable($productId) {
        $collection = $this->colPreviewDiscountFactory->create()
            ->addFieldToSelect('discount_id')
            ->addFieldToFilter('product_id',$productId)
            ->setPageSize(1)
            ->setOrder('discount_id','ASC');
        return $collection && $collection->getSize() > 0 ? true : false;
    }
    protected function generateDefaultData($productId) {
        $createData = date('Y-m-d H:i:s');
        $sampleItems  = [
            [
              'product_id'=>$productId,
              'discount_title'=>'Our Price',
              'discount_price'=>0,
              'discount_amount'=>0,
              'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'discount_title'=>'Additional options',
                'discount_price'=>0,
                'discount_amount'=>0,
                'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'discount_title'=>'Processing & Finishing',
                'discount_price'=>0,
                'discount_amount'=>0,
                'created_at'=>$createData,
            ],
            [
                'product_id'=>$productId,
                'discount_title'=>'Shipping',
                'discount_price'=>0,
                'discount_amount'=>0,
                'created_at'=>$createData,
            ]
        ];
        foreach ($sampleItems as $sampleItem) {
            $this->savePreviewDiscount($sampleItem);
        }
    }
    protected function savePreviewDiscount($sampleItem = []) {
        $model = $this->previewDiscountFactory->create();
        $model->setData($sampleItem);
        $id = 0;
        try {
            $this->resPreviewDiscountFactory->create()->save($model);
            $id = $model->getId();
        }
        catch (\Exception $exception) {

        }
        return $id;
    }

}
