<?php
/**
 * Webkul Software.
 * 
 * PHP version 7.0+
 *
 * @category  Webkul
 * @package   Webkul_MobikulCore
 * @author    Webkul <support@webkul.com>
 * @copyright 2010-2019 Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html ASL Licence
 * @link      https://store.webkul.com/license.html
 */
namespace Webkul\MobikulCore\Observer;

use Magento\Store\Model\App\Emulation;
use Magento\Framework\Event\ObserverInterface;

/**
 * OrderStatusNotification Class
 */
class OrderStatusNotification implements ObserverInterface
{
    protected $helper;
    protected $jsonHelper;
    protected $deviceToken;
    protected $emulate;

    /**
     * Construct Function for class OrderStatusNotification
     *
     * @param Emulation                           $emulate     emulate
     * @param \Webkul\MobikulCore\Helper\Data         $helper      helper
     * @param \Webkul\MobikulCore\Model\DeviceToken   $deviceToken deviceToken
     * @param \Magento\Framework\Json\Helper\Data $jsonHelper  jsonHelper
     * 
     * @return void
     */
    public function __construct(
        Emulation $emulate,
        \Webkul\MobikulCore\Helper\Data $helper,
        \Webkul\MobikulCore\Model\DeviceToken $deviceToken,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->helper      = $helper;
        $this->emulate     = $emulate;
        $this->jsonHelper  = $jsonHelper;
        $this->deviceToken = $deviceToken;
    }

    /**
     * Execute Function for class OrderStatusNotification
     *
     * @param \Magento\Framework\Event\Observer $observer $observer
     *
     * @return void|json
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $order = $observer->getOrder();
        if ($order->getState() != "") {
            $canReorder = 0;
            if ($this->helper->canReorder($order) == 1) {
                $canReorder = $this->helper->canReorder($order);
            }
            $environment = $this->emulate->startEnvironmentEmulation($order->getStoreId(), \Magento\Framework\App\Area::AREA_ADMINHTML, true);
            $message = [
                "id"               => $order->getId(),
                "body"             => __("Your order status changed to ").$order->getStatusLabel(),
                "title"            => __("Order Status Changed!!"),
                "sound"            => "default",
                "message"          => __("Your order status changed to ").$order->getStatusLabel(),
                "canReorder"       => $canReorder,
                "incrementId"      => $order->getIncrementId(),
                "notificationType" => "order"
            ];
            if ($order->getState() == "new") {
                $message["title"]   = __("Order Placed Successfully!!");
                $message["message"] = __("Your order status is ").$order->getStatusLabel();
            }
            $url     = "https://fcm.googleapis.com/fcm/send";
            $authKey = $this->helper->getConfigData("mobikul/notification/apikey");
            $headers = [
                "Authorization: key=".$authKey,
                "Content-Type: application/json",
            ];
            if ($authKey != "") {
                $customerId          = 0;
                if (!$order->getCustomerIsGuest()) {
                    $tokenCollection = $this->deviceToken->getCollection()->addFieldToFilter("customer_id", $order->getCustomerId());
                } else {
                    $tokenCollection = $this->deviceToken->getCollection()->addFieldToFilter("customer_id", 0)->addFieldToFilter("email", $order->getCustomerEmail());
                }
                foreach ($tokenCollection as $eachToken) {
                    $fields = [
                        "to"                => $eachToken->getToken(),
                        "data"              => $message,
                        "priority"          => "high",
                        "time_to_live"      => 30,
                        "delay_while_idle"  => true,
                        "content_available" => true
                    ];
                    if ($eachToken->getOs() == "ios") {
                        $fields["notification"] = $message;
                    }
                    $ch = curl_init();
                    curl_setopt($ch, CURLOPT_URL, $url);
                    curl_setopt($ch, CURLOPT_POST, true);
                    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->jsonHelper->jsonEncode($fields));
                    $result = curl_exec($ch);
                    curl_close($ch);
                    if ($this->isJson($result)) {
                        $result = $this->jsonHelper->jsonDecode($result);
                        if ($result["success"] == 0 && $result["failure"] == 1) {
                            $eachToken->delete();
                        }
                    }
                }
            }
            $this->emulate->stopEnvironmentEmulation($environment);
        }
    }

    /**
     * Function isJson to check if a string is jSon
     *
     * @param string $string string
     *
     * @return bool
     */
    public function isJson($string)
    {
        json_decode($string);
        return (json_last_error() == JSON_ERROR_NONE);
    }
}
