<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Setup\Operations;

use Aitoc\PreOrders\Api\Data\Source\AttributesInterface;
use Aitoc\PreOrders\Api\Data\Source\LegacyBackordersValueInterface;
use Aitoc\PreOrders\Api\Data\Source\PaymentTypeInterface;
use Aitoc\PreOrders\Api\Data\Source\PreOrderNoteScope;
use Aitoc\PreOrders\Helper\Settings as SettingsHelper;
use Aitoc\PreOrders\Model\AttributesManagement;
use Magento\Catalog\Model\ProductRepository;
use Magento\CatalogInventory\Api\StockItemCriteriaInterface;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\State as AppState;

class TransferLegacySettings
{
    /**
     * @var SettingsHelper
     */
    private $settingsHelper;

    /**
     * @var StockItemRepository
     */
    private $stockItemRepository;

    /**
     * @var StockItemCriteriaInterface
     */
    private $stockItemCriteria;

    /**
     * @var AttributesManagement
     */
    private $attributesManagement;

    /**
     * @var ProductRepository
     */
    private $productRepository;

    /**
     * @param SettingsHelper $settingsHelper
     * @param StockItemRepository $stockItemRepository
     * @param StockItemCriteriaInterface $stockItemCriteria
     * @param AttributesManagement $attributesManagement
     * @param ProductRepository $productRepository
     * @param \Magento\Framework\App\State $appState
     * @throws LocalizedException
     */
    public function __construct(
        SettingsHelper $settingsHelper,
        StockItemRepository $stockItemRepository,
        StockItemCriteriaInterface $stockItemCriteria,
        AttributesManagement $attributesManagement,
        ProductRepository $productRepository,
        AppState $appState
    ) {
        $this->settingsHelper = $settingsHelper;
        $this->stockItemRepository = $stockItemRepository;
        $this->stockItemCriteria = $stockItemCriteria;
        $this->attributesManagement = $attributesManagement;
        $this->productRepository = $productRepository;
        $appState->setAreaCode('frontend');
    }

    /**
     * Transfer settings from old extension version
     * @throws LocalizedException
     */
    public function execute()
    {
        $this->settingsHelper->setPaymentTypeSetting(PaymentTypeInterface::TYPE_INSTANT_PAYMENT);
        $preOrderNoteValue = [
            PreOrderNoteScope::CATEGORY_PAGE_SCOPE,
            PreOrderNoteScope::PRODUCT_PAGE_SCOPE,
            PreOrderNoteScope::MINI_CART_SCOPE,
            PreOrderNoteScope::CART_SCOPE,
            PreOrderNoteScope::CHECKOUT_SCOPE,
            PreOrderNoteScope::SEARCH_RESULT_SCOPE,
            PreOrderNoteScope::WISH_LIST_SCOPE,
            PreOrderNoteScope::COMPARE_LIST_SCOPE
        ];
        $this->settingsHelper->setPreOrderNoteScopeSettings($preOrderNoteValue);

        $this->migrateLegacyProductAttributes();
    }

    /**
     * @throws LocalizedException
     */
    private function migrateLegacyProductAttributes()
    {
        $preOrderBackordersArray = [
            LegacyBackordersValueInterface::PRE_ORDERS,
            LegacyBackordersValueInterface::PRE_ORDER_FOR_OUT_OF_STOCK
        ];
        $searchCriteria = $this->stockItemCriteria;
        foreach ($preOrderBackordersArray as $k => $item) {
            $searchCriteria->addFilter('backorders_filter' . $k, 'backorders', $item, 'or');
        }
        $stockItems = $this->stockItemRepository->getList($searchCriteria)->getItems();
        foreach ($stockItems as $stockItem) {
            $productId = $stockItem->getProductId();
            $product = $this->productRepository->getById($productId);
            $preOrderNote = $product->getPreorderdescript();
            $currentQty = $stockItem->getQty();
            $this->attributesManagement
                ->saveAttributeValue($productId, AttributesInterface::PRE_ORDER_STATUS, 1);
            $this->attributesManagement
                ->saveAttributeValue($productId, AttributesInterface::PRE_ORDER_NOTE, $preOrderNote);
            switch ($stockItem->getBackorders()) {
                case LegacyBackordersValueInterface::PRE_ORDERS:
                    $this->attributesManagement
                        ->saveAttributeValue($productId, AttributesInterface::PRE_ORDER_QTY, $currentQty);
                    $this->attributesManagement
                        ->saveAttributeValue($productId, AttributesInterface::OUT_OF_STOCK_QTY, 0);
                    break;
                case LegacyBackordersValueInterface::PRE_ORDER_FOR_OUT_OF_STOCK:
                    $this->attributesManagement
                        ->saveAttributeValue($productId, AttributesInterface::PRE_ORDER_QTY, 0);
                    $this->attributesManagement
                        ->saveAttributeValue($productId, AttributesInterface::OUT_OF_STOCK_QTY, '');
                    break;
            }
        }
    }
}