<?php
declare(strict_types=1);

namespace Webkul\MobikulGraphQL\Model\Resolver;
if (!defined('DS')) {
    define('DS', DIRECTORY_SEPARATOR);
}

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
 * ProductPageData resolver
 */
class RemoveFromWishlist  extends \Webkul\MobikulGraphQL\Controller\Customer\RemoveFromWishlist
{

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        try {
            $this->wholeData = $args;
            return $this->removeWishlist();
        } catch (\Exception $e) {
            echo $e->getMessage(); die;
        }
    }
    public function removeWishlist()
    {
        try  {
            $this->verifyRequest();
            if (!$this->customerId && $this->customerToken != "") {
                $this->returnArray["otherError"] = "customerNotExist";
                throw new \Magento\Framework\Exception\LocalizedException(
                    __("As customer you are requesting does not exist, so you need to logout.")
                );
            }
            $environment = $this->emulate->startEnvironmentEmulation($this->storeId);
            $item = $this->wishlistItem->load($this->itemId);
            if (!$item->getId()) {
                $this->returnArray["alreadyDeleted"] = true;
                return $this->getJsonResponse($this->returnArray);
            }
            $error = false;
            $wishlist = $this->wishlistProvider->loadByCustomerId($this->customerId, true);
            if (!$wishlist) {
                $error = true;
            }
            $item->delete();
            $wishlist->save();
            if ($error) {
                $this->returnArray["message"] = __("An error occurred while deleting the item from wishlist.");
                return $this->getJsonResponse($this->returnArray);
            }
            $this->returnArray["success"] = true;
            $this->returnArray["message"] = __("Item successfully deleted from wishlist.");
            $this->returnArray["status"] = true;
            $this->emulate->stopEnvironmentEmulation($environment);
            return $this->returnArray; 
            return $this->getJsonResponse($this->returnArray);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->returnArray["message"] = __($e->getMessage());
            $this->helper->printLog($this->returnArray);
            return $this->getJsonResponse($this->returnArray);
        } catch (\Exception $e) {
            $this->returnArray["message"] = __($e->getMessage());
            $this->helper->printLog($this->returnArray);
            return $this->getJsonResponse($this->returnArray);
        }
    }

    /**
     * Function verify Request to authenticate the request
     * Authenticates the request and logs the result for invalid requests
     *
     * @return Json
     */
    public function verifyRequest($wholeData = [])
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        //Load product by product id
        $oauthToken = $objectManager->create('Magento\Integration\Model\Oauth\Token');
        if ($this->getRequest()->getMethod() == "POST" && $this->wholeData) {
            $this->itemId = $this->wholeData["itemId"] ?? 0;
            $this->storeId = $this->wholeData["storeId"] ?? 1;
            $this->customerToken = $this->wholeData["customerToken"] ?? "";
            $this->customerId = $objectManager->create('Magento\Integration\Model\Oauth\Token')->loadByToken($this->customerToken)->getData('customer_id'); 
            if (!$this->customerId && $this->customerToken != "") {
                $this->returnArray["otherError"] = "customerNotExist";
                throw new \Magento\Framework\Exception\LocalizedException(
                    __("Customer you are requesting does not exist.")
                );
            }
            
        } else {
            throw new \Exception(__("Invalid Request"));
        }
    }
}