<?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;


use Magento\Framework\View\Element\Template;

/**
 * Class AccountOrders
 * @package Magetop\Advancepreview\Block
 */
class AccountOrders extends \Magetop\Advancepreview\Block\AdvancePreviewAbstract
{
    /**
     * @var \Magento\Sales\Model\ResourceModel\Order\CollectionFactory
     */
    protected $orderCollectionFactory;
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $httpContext;
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;
    public function __construct(
        Template\Context $context,
        \Magento\Framework\Pricing\Helper\Data $priceHelper,
        \Magetop\Advancepreview\Helper\Data $advancePreviewHelper,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    )
    {
        parent::__construct($context, $priceHelper, $advancePreviewHelper, $data);
        $this->httpContext = $httpContext;
        $this->orderCollectionFactory = $orderCollectionFactory;
        $this->customerSession = $customerSession;

    }

    /**
     * @return \Magento\Sales\Model\ResourceModel\Order\Collection|null
     */
    public function getAllCustomerOrders() {
        $customerId = $this->getCustomerId();
        $orders = null;
        if($customerId > 0) {
            $collection =  $this->orderCollectionFactory->create($customerId)
                ->addFieldToSelect('*')
              /*  ->addFieldToFilter('status',
                    ['in' => $this->_orderConfig->getVisibleOnFrontStatuses()]
                )*/
                ->setOrder(
                    'created_at',
                    'desc'
                );
            $orders = $collection;
        }
        return $orders;
    }
    public function getItemDetail($order) {
        $items = $order->getAllItems();
        $storeId = $order->getStore()->getId();
        $orderId = $order->getId();
        $priceItem = 0;
        $projectName = '';
        foreach ($items as $item)
        {
            $infoRequest = $item->getProductOptionByCode('info_buyRequest');

            if(isset($infoRequest['txt_project_final_price'])) {
                $priceItem += $infoRequest['txt_project_final_price'];
            }
            $projectName = isset($infoRequest['custom_project_name']) && $projectName == '' ? $infoRequest['custom_project_name'] : '';
            $gernerateProjoectId = isset($infoRequest['text_project_generate']) ? (int)$infoRequest['text_project_generate'] : '';
            if($projectName != '' && $gernerateProjoectId > 0) {
                if($gernerateProjoectId > 1000) {
//                    $gernerateProjoectId = $gernerateProjoectId;
                }
                else if($gernerateProjoectId > 100) {
                    $gernerateProjoectId = '0'.$gernerateProjoectId;
                }
                else if($gernerateProjoectId > 10) {
                    $gernerateProjoectId = '00'.$gernerateProjoectId;
                }
                else {
                    $gernerateProjoectId = '000'.$gernerateProjoectId;
                }
                $projectName = $gernerateProjoectId. ' '. $projectName;
            }
        }
        return [
          'total_price'=>$priceItem,
          'project_name'=>$projectName,
        ];
    }

    /**
     * @return int|null
     */
    protected function getCustomerId() {
        $customerId = 0;
        if($this->checkIsLogin()) {
            $customerId = $this->customerSession->getCustomerId();
        }
        return $customerId;
    }

    /**
     * @return bool
     */
    protected function checkIsLogin() {
        $isLogin = false;
        if($this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH)) {
            $isLogin = true;
        }
        return $isLogin;
    }
}
