<?php
/**
 * @package         ReReplacer
 * @version         12.4.0PRO
 * 
 * @author          Peter van Westen <info@regularlabs.com>
 * @link            http://regularlabs.com
 * @copyright       Copyright © 2022 Regular Labs All Rights Reserved
 * @license         http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

use RegularLabs\Component\Conditions\Administrator\Helper\ConvertAssignments;

defined('_JEXEC') or die;

require_once __DIR__ . '/script.install.helper.php';

class Com_ReReplacerInstallerScript extends Com_ReReplacerInstallerScriptHelper
{
    public $alias          = 'rereplacer';
    public $extension_type = 'component';
    public $name           = 'REREPLACER';

    public function onAfterInstall($route)
    {
        $this->createTable();
        $this->fixColumns();
        $this->deleteJoomla3Files();

        if ($this->convertAssignmentsToConditions())
        {
            return false;
        }

        return parent::onAfterInstall($route);
    }

    public function uninstall($adapter)
    {
        $this->uninstallPlugin($this->extname, 'system');
        $this->uninstallPlugin($this->extname, 'actionlog');
    }

    private function convertAssignmentsToConditions()
    {
        $loader = include JPATH_LIBRARIES . '/vendor/autoload.php';
        $loader->setPsr4('RegularLabs\\Library\\', JPATH_LIBRARIES . '/regularlabs/src');
        $loader->setPsr4('RegularLabs\\Component\\Conditions\\Administrator\\', JPATH_ADMINISTRATOR . '/components/com_conditions/src');

        return ConvertAssignments::convert('com_rereplacer', 'rereplacer');
    }

    private function createTable()
    {
        $query = "CREATE TABLE IF NOT EXISTS `#__rereplacer` (
            `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
            `name` VARCHAR(100) NOT NULL,
            `description` TEXT NOT NULL,
            `category` VARCHAR(50) NOT NULL,
            `color` VARCHAR(8) NULL DEFAULT NULL,
            `search` TEXT NOT NULL,
            `replace` TEXT NOT NULL,
            `area` TEXT NOT NULL,
            `params` TEXT NOT NULL,
            `published` TINYINT(1) NOT NULL DEFAULT '0',
            `ordering` INT NOT NULL DEFAULT '0',
            `checked_out` INT UNSIGNED DEFAULT NULL,
            `checked_out_time` datetime NULL DEFAULT NULL,
            PRIMARY KEY  (`id`),
            KEY `id` (`id`,`published`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
        $this->db->setQuery($query);
        $this->db->execute();

        $this->db->setQuery('ALTER TABLE `#__rereplacer` MODIFY `checked_out` INT UNSIGNED DEFAULT NULL;');
        $this->db->execute();

        $this->db->setQuery('ALTER TABLE `#__rereplacer` MODIFY `checked_out_time` datetime NULL DEFAULT NULL;');
        $this->db->execute();

        $this->db->setQuery('UPDATE `#__rereplacer` SET `checked_out` = NULL WHERE `checked_out` = 0;');
        $this->db->execute();

        $this->db->setQuery('UPDATE `#__rereplacer` SET `checked_out_time` = NULL WHERE CAST(`checked_out_time` AS CHAR(20)) = \'0000-00-00 00:00:00\';');
        $this->db->execute();
    }

    private function deleteJoomla3Files()
    {
        $this->delete(
            [
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/controllers',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/helpers',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/models',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/views',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/tables',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/controller.php',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/item_params.xml',
                JPATH_ADMINISTRATOR . '/components/com_' . $this->extname . '/' . $this->extname . '.php',
            ]
        );
    }

    private function fixColumns()
    {
        $query = 'SHOW COLUMNS FROM `#__rereplacer`';
        $this->db->setQuery($query);

        $columns = $this->db->loadColumn();

        if ( ! in_array('color', $columns))
        {
            $query = "ALTER TABLE `#__rereplacer` ADD `color` VARCHAR(8) NULL DEFAULT NULL AFTER `category`;";
            $this->db->setQuery($query);
            $this->db->execute();
        }
    }
}
