<?php
/**
 * Magento 2 Google Analytics 4 (GA4) GTM
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Anowave license that is
 * available through the world-wide-web at this URL:
 * https://www.anowave.com/license-agreement/
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category 	Anowave
 * @package 	Anowave_Ec
 * @copyright 	Copyright (c) 2026 Anowave (https://www.anowave.com/)
 * @license  	https://www.anowave.com/license-agreement/
 */

namespace Anowave\Ec\Block\Adminhtml\Order\View\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Template;
use Magento\Backend\Block\Widget\Tab\TabInterface;
use Anowave\Ec\Model\TransactionRepository;
use Anowave\Ec\Model\Transaction;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\App\RequestInterface;

class State extends Template implements TabInterface
{
    private $order = null;

    /**
     * Template
     *
     * @var string
     */
    protected $_template = 'state.phtml';

    protected OrderRepositoryInterface $orderRepositoryInterface;

    protected TransactionRepository $transactionRepository;

    protected RequestInterface $requestInterface;

    public function __construct(
        Context $context,
        TransactionRepository $transactionRepository,
        OrderRepositoryInterface $orderRepositoryInterface,
        RequestInterface $requestInterface,
        array $data = []
    ) 
    {
        $this->transactionRepository = $transactionRepository;
        $this->orderRepositoryInterface = $orderRepositoryInterface;
        $this->requestInterface = $requestInterface;

        parent::__construct($context, $data);
    }

    /**
     * Retrieve order model instance
     *
     * @return \Magento\Sales\Model\Order
     */
    public function getOrder()
    {
        if (is_null($this->order))
        {
            $order_id = (int) $this->requestInterface->getParam('order_id');

            if ($order_id)
            {
                $this->order = $this->orderRepositoryInterface->get($order_id);
            }
        }

        return $this->order;
    }

    /**
     * {@inheritdoc}
     */
    public function getTabLabel()
    {
        return __('GA4');
    }

    /**
     * {@inheritdoc}
     */
    public function getTabTitle()
    {
        return __('GA4');
    }

    /**
     * {@inheritdoc}
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * {@inheritdoc}
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Get Tab Class
     *
     * @return string
     */
    public function getTabClass()
    {
        return 'ajax only';
    }

    /**
     * Get Class
     *
     * @return string
     */
    public function getClass()
    {
        return $this->getTabClass();
    }

    /**
     * Get Tab Url
     *
     * @return string
     */
    public function getTabUrl()
    {
        return $this->getUrl('track/*/state', ['_current' => true]);
    }

    /**
     * Get state 
     * 
     * @return Transaction
     */
    public function getState() : Transaction
    {
        $transaction = $this->transactionRepository->getByOrderId
        (
            $this->getOrder()->getId()
        );

        return $transaction;
    }
}