<?php

use Bitrix\Main\Config\Option;
use Bitrix\Main\Localization\Loc;
use Bitrix\Main\EventManager;
use Bitrix\Main\ModuleManager;
use Bitrix\Main\Application;
use Bitrix\Main\Request;
use Bitrix\Main\Server;

Loc::loadMessages(__FILE__);

if (class_exists('assist_pay')) {
    return;
}

class assist_pay extends CModule
{
    public $MODULE_ID = 'assist.pay';
    public $MODULE_VERSION;
    public $MODULE_VERSION_DATE;
    public $MODULE_NAME;
    public $MODULE_DESCRIPTION;
    public $MODULE_GROUP_RIGHTS = 'Y';

    /** @var Server */
    protected $server;

    /** @var Request */
    protected $request;

    public function __construct()
    {
        $arModuleVersion = [];

        require __DIR__ . '/version.php';

        $this->MODULE_VERSION = $arModuleVersion['VERSION'];
        $this->MODULE_VERSION_DATE = $arModuleVersion['VERSION_DATE'];

        $this->MODULE_NAME = Loc::getMessage('ASSIST_PAY_MODULE_NAME');
        $this->MODULE_DESCRIPTION = Loc::getMessage('ASSIST_PAY_MODULE_DESCRIPTION');

        $this->PARTNER_NAME = Loc::getMessage('ASSIST_PAY_PARTNER_NAME');
        $this->PARTNER_URI = Loc::getMessage('ASSIST_PAY_PARTNER_URI');

        $context = Application::getInstance()->getContext();
        $this->server = $context->getServer();
        $this->request = $context->getRequest();
    }

    public function DoInstall()
    {
        global $APPLICATION;

        if (!$this->isVersionD7()) {
            $APPLICATION->ThrowException(Loc::getMessage('ASSIST_PAY_INSTALL_ERROR_VERSION'));
        }

        $this->InstallDB();
        $this->InstallEvents();
        $this->InstallFiles();

        ModuleManager::registerModule($this->MODULE_ID);
    }

    /**
     * @throws Exception
     */
    public function DoUninstall()
    {
        global $APPLICATION, $step, $obModule;

        if ($step < 2) {
            $APPLICATION->IncludeAdminFile(GetMessage('ASSIST_PAY_INSTALL_TITLE'), __DIR__ . '/unstep1.php');
        } elseif ($step == 2) {
            $GLOBALS['CACHE_MANAGER']->CleanAll();
            ModuleManager::unRegisterModule($this->MODULE_ID);

            $this->UnInstallDB(['savedata' => $_REQUEST['savedata'] ?? false]);
            $this->UnInstallEvents();
            $this->UnInstallFiles();

            $obModule = $this;
            $APPLICATION->IncludeAdminFile(GetMessage('ASSIST_PAY_INSTALL_TITLE'), __DIR__ . '/unstep2.php');
        }
    }

    public function InstallDB($arParams = []): bool
    {
        return true;
    }

    /**
     * @throws Exception
     */
    public function UnInstallDB($arParams = []): bool
    {
        Option::delete($this->MODULE_ID);
        return true;
    }

    public function InstallEvents(): bool
    {
        // Include module
        EventManager::getInstance()->registerEventHandler('main', 'OnPageStart', $this->MODULE_ID);
        return true;
    }

    public function UnInstallEvents(): bool
    {
        // Include module
        EventManager::getInstance()->unRegisterEventHandler('main', 'OnPageStart', $this->MODULE_ID);
        return true;
    }

    public function InstallFiles($arParams = []): bool
    {
        CopyDirFiles(__DIR__ . '/sale_payment/assist/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/sale_payment/assist', true,true);
        CopyDirFiles(__DIR__ . '/images/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/images/sale/sale_payments');
        CopyDirFiles(__DIR__ . '/tools/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/tools');
        return true;
    }

    public function UnInstallFiles(): bool
    {
        DeleteDirFiles(__DIR__ . '/sale_payment/assist/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/sale_payment/assist');
        DeleteDirFiles(__DIR__ . '/sale_payment/assist/template/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/php_interface/include/sale_payment/assist/template');
        DeleteDirFiles(__DIR__ . '/images/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/images/sale/sale_payments');
        DeleteDirFiles(__DIR__ . '/tools/', $_SERVER['DOCUMENT_ROOT'] . '/bitrix/tools');
        return true;
    }

    private function isVersionD7(): bool
    {
        return CheckVersion(ModuleManager::getVersion('main'), '14.00.00');
    }
}
