<?php

namespace Sibs\Sibs\Model\Config\Source;

use Magento\Config\Model\Config\CommentInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\UrlInterface;
use Sibs\Sibs\DB\SibsDB;
use Sibs\Sibs\Helper\DataHelper;
use Sibs\Sibs\Model\Config\Backend\AppleFileType;

class AppleFileComment implements CommentInterface
{
    /**
     * @var \Sibs\Sibs\Helper\DataHelper
     */
    protected $_paymentHelper;

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
     */
    protected $rootDirectory;

    /**
     * @var \Magento\Framework\Filesystem\Directory\WriteInterface
     */
    protected $pubDirectory;

    /**
     * @var string
     */
    protected $appleFileEndpoint;

    public function __construct(
        DataHelper $paymentHelper,
        \Magento\Framework\Filesystem $filesystem
    ) {
        $this->_paymentHelper    = $paymentHelper;
        $this->rootDirectory     = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
        $this->pubDirectory      = $filesystem->getDirectoryWrite(DirectoryList::PUB);
        $this->appleFileEndpoint = AppleFileType::getAppleFileEndpoint();
    }

    /**
     * Get comment Text
     *
     * @param mixed $elementValue
     * @return string
     */
    public function getCommentText($elementValue)
    {
        $rootMerchantAppleUrl  = $this->getRootMerchantAppleURL();
        $merchantAppleUrl      = $this->getMerchantAppleURL();
        $appleFileUrl          = 'https://developer.apple.com/documentation/applepaywebmerchantregistrationapi/preparing_merchant_domains_for_verification';
        $documentationUrl      = 'https://www.docs.pay.sibs.com/payment-methods/apple-pay/';

        $appleComment = '';

        $appleComment .= '<p>' .
                __("For apple payment properly work, you need to register and download apple file in this") .
                ' <a href="' . $appleFileUrl . '">' . __('link') . '</a> ' .
                __('and upload here') . '.' .
            '</p>';

        $appleComment .= '<p>' .
                __('This file needs to be acessible in this') .
                ' <a href="' . $rootMerchantAppleUrl .'">' . __('link') . '</a> ' .
                $this->getRootValidated() . '<br/> ' . __('or this') .
                ' <a href="' . $merchantAppleUrl .'">' . __('link') . '</a> ' .
                $this->getPubValidated() . '.' .
            '</p>';

        $appleComment .= '<p>' .
                __('See more in our') .
                ' <a href="' . $documentationUrl . '">' . __("documentation") . '</a>' . '.' .
            '</p>';

        return $appleComment;
    }

    /**
     * Get root merchant apple URL
     *
     * @return string
     */
    private function getRootMerchantAppleURL()
    {
        $storeManager      = SibsDB::get('\Magento\Store\Model\StoreManagerInterface');
        $domain            = $storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB);

        return $domain . $this->appleFileEndpoint;
    }

    /**
     * Get pub merchant apple URL
     *
     * @return string
     */
    private function getMerchantAppleURL()
    {
        $storeManager      = SibsDB::get('\Magento\Store\Model\StoreManagerInterface');
        $domain            = $storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB);

        return $domain . 'pub/' . $this->appleFileEndpoint;
    }

    /**
     * Get validation root file comment
     *
     * @return string
     */
    private function getRootValidated()
    {
        if ($this->validateIfFileExistsInRoot()) {
            return '(Root: <span style="color:green">' .
                __("File Validated") .
            '</span>)';
        }
        
        return '(Root: <span style="color:red">' .
            __("File Needed") .
        '</span>)';
    }

    /**
     * Get validation pub file comment
     *
     * @return string
     */
    private function getPubValidated()
    {
        if ($this->validateIfFileExistsInPub()) {
            return '(Pub: <span style="color:green">' .
                __("File Validated") .
            '</span>)';
        }
        
        return '(Pub: <span style="color:red">' .
            __("File Needed") .
        '</span>)';
    }

    /**
     * Validate if root file exists
     *
     * @return bool
     */
    private function validateIfFileExistsInRoot()
    {
        return $this->rootDirectory->isFile('/' . $this->appleFileEndpoint);
    }

    /**
     * Validate if pub file exists
     *
     * @return bool
     */
    private function validateIfFileExistsInPub()
    {
        return $this->pubDirectory->isFile('/' . $this->appleFileEndpoint);
    }
}
