<?php
/******************************************************************************
 *   ______                                                                   *
 *  /       \                                                                 *
 * /$$$$$$  | ________  __    __   ______    ______                           *
 * $$ |__$$ |/        |/  |  /  | /      \  /      \                          *
 * $$    $$ |$$$$$$$$/ $$ |  $$ |/$$$$$$  |/$$$$$$  |                         *
 * $$$$$$$$ |  /  $$/  $$ |  $$ |$$ |  $$/ $$    $$ |                         *
 * $$ |  $$ | /$$$$/__ $$ \__$$ |$$ |      $$$$$$$$/                          *
 * $$ |  $$ |/$$      |$$    $$/ $$ |      $$       |                         *
 * $$/   $$/ $$$$$$$$/  $$$$$$/  $$/        $$$$$$$/                          *
 *                                                                            *
 * @PROJECT    : 3D Preview Extension [Magetop.com]                           *
 * @AUTHOR     : Azure - Developer                                            *
 * @COPYRIGHT  : © 2019  Magetop.com                                          *
 * @LINK       : https://Magetop.com                                          *
 * @CREATED    :  10/01/2022                                                  *
 ******************************************************************************/

namespace Magetop\Advancepreview\Block\Adminhtml\ReWrites\Order;

use Magento\Sales\Model\Order\Address;
class CustomInfo extends \Magento\Sales\Block\Adminhtml\Order\View\Info
{
    protected $orderHelper;
    protected $dailyShippingFactory;
    protected $colPreviewOrderItemsFactory;
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Sales\Helper\Admin $adminHelper,
        \Magento\Customer\Api\GroupRepositoryInterface $groupRepository,
        \Magento\Customer\Api\CustomerMetadataInterface $metadata,
        \Magento\Customer\Model\Metadata\ElementFactory $elementFactory,
        Address\Renderer $addressRenderer,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewDailyShipping\CollectionFactory $dailyShippingFactory,
        \Magetop\Advancepreview\Model\ResourceModel\PreviewOrderItems\CollectionFactory $colPreviewOrderItemsFactory,
        \Magetop\Advancepreview\Helper\OrderHelper $orderHelper,
        array $data = [])
    {
        parent::__construct($context, $registry, $adminHelper, $groupRepository, $metadata, $elementFactory, $addressRenderer, $data);
        $this->dailyShippingFactory = $dailyShippingFactory;
        $this->colPreviewOrderItemsFactory = $colPreviewOrderItemsFactory;
        $this->orderHelper = $orderHelper;
    }

    public function getCustomProjectNameInOrder(
        \Magento\Sales\Model\Order $order
    ) {
        $items = $order->getAllItems();
        $status = [];
        $invoiceId = $this->getRequest()->getParam('invoice_id');
        $statusLabelsstandard = [
            'new'=>'New',
            'process'=>'In progress',
            'shipping'=>'Ready for shipping/Production done',
            'shipped'=>'Shipped'
        ];
        $statusLabel = [
            'new' => 'New – design uploaded',
            'downloading' => 'Checking design by Myfitmix',
            'design_failed' => 'Design Failed, customer must upload a new design',
            'design_approved' => 'Design approved',
            'customer_reject' => 'Proof Rejected by Customer',
            'design_full_approved' => 'Design fully approved',
            'process' => 'In progress',
            'shipping' => 'Ready for shipping/Production done',
            'shipped' => 'Shipped',
            'delivered' => 'Delivered',
        ];
        $statusCustomNoPrintLabels = [
            'new'=>'New – No design',
            'downloading' => 'Checking design by Myfitmix',
            'design_failed' => 'Design Failed, customer must upload a new design',
            'design_approved' => 'Design approved',
            'customer_reject' => 'Proof Rejected by Customer',
            'design_full_approved' => 'Design fully approved',
            'process' => 'In progress',
            'shipping' => 'Ready for shipping/Production done',
            'shipped' => 'Shipped',
            'delivered' => 'Delivered',
        ];
        foreach ($items as $item) {
            // status
            if($invoiceId > 0 && !$this->orderHelper->checkIncoiceAndItemId($invoiceId, $item->getId())) {
                continue;
            }

            $infoRequest = $item->getProductOptionByCode('info_buyRequest');
            $designType = $infoRequest['txt_custom_preview_design_type'];
            $isNewProof = 0;

            $previewItem = $this->getCustomPreviewOrderItem($item->getId());
            $isNewProof = $previewItem->getData('is_new_proof');
            $adminUploadFile = $previewItem->getData('admin_upload_design');

            $itemStatus = 'new';
            if($previewItem && $previewItem->getId()) {
                $itemStatus = $previewItem->getData('status');
            }
            //$txtStatus = isset($statusLabel[$itemStatus]) ? $statusLabel[$itemStatus] : $itemStatus;
            if ($designType == 0) {
                $txtStatus = isset($statusLabelsstandard[$itemStatus]) ? $statusLabelsstandard[$itemStatus] : $itemStatus;
            }else if ($previewItem->getCustomDesign() == '') {
                $txtStatus = isset($statusCustomNoPrintLabels[$itemStatus]) ? $statusCustomNoPrintLabels[$itemStatus] : $itemStatus;
            }else{
                if ($isNewProof == 2 && ($itemStatus == 'customer_reject' || $itemStatus == 'design_approved')) {
                    $txtStatus = 'Proof is uploaded, waiting for customer approval';
                }else{
                    $txtStatus = isset($statusLabel[$itemStatus]) ? $statusLabel[$itemStatus] : $itemStatus;
                }
            }
            if ($adminUploadFile != null && $itemStatus == 'new') {
                $txtStatus = 'New';
            }
            $getName = $item->getName();
//            $txtStatus = $item->getName() . ' - ' . $txtStatus;
            $status[] = [
                'status'=>$txtStatus,
                // 'name'=>$item->getProduct()->getName()
                'name'=>$getName
            ];
        }
        return $status;
    }
    protected function getDailyShippingById($dailyId = 0) {
        if($dailyId > 0) {
            $collection = $this->dailyShippingFactory->create()
                ->addFieldToFilter('daily_shipping_id',$dailyId);
            return $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
        }
        return  null;
    }
    protected function getCustomPreviewOrderItem($itemId) {
        $collection = $this->colPreviewOrderItemsFactory->create()
            ->addFieldToFilter('item_id',$itemId);
        // echo (string)$collection->getSelect(). '--';
        return $collection && $collection->getSize() > 0 ? $collection->getFirstItem() : null;
    }
}
