<?php

/**
 * @package        Joomla
 * @subpackage     Joom Donation
 * @author         Dang Thuc Dam
 * @copyright      Copyright (C) 2009 - 2025 Ossolution Team
 * @license        GNU/GPL, see LICENSE.php
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Language\Text;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\Path;
use Joomla\CMS\Plugin\PluginHelper;

class Pkg_EdocmanInstallerScript
{
	protected $installType;
	public static $languageFiles = array('en-GB');

	/**
	 * Minimum PHP version
	 */
	private const MIN_PHP_VERSION = '7.4.0';

	/**
	 * Minimum Joomla version
	 */
	private const MIN_JOOMLA_VERSION = '4.2.0';


	/**
	 * method to install the component
	 *
	 * @return void
	 */
	public function install($parent)
	{
		$this->installType = 'install';
	}

	public function update($parent)
	{
		$this->installType = 'update';
	}

	function preflight($type, $parent)
	{
		if (version_compare(JVERSION, self::MIN_JOOMLA_VERSION, '<'))
		{
			Factory::getApplication()->enqueueMessage(
				'Cannot install Edocman in a Joomla! release prior to ' . self::MIN_JOOMLA_VERSION,
				'error'
			);

			return false;
		}

		if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION, '<'))
		{
			Factory::getApplication()->enqueueMessage(
				'Edocman requires PHP ' . self::MIN_PHP_VERSION . '+ to work. Please contact your hosting provider, ask them to update PHP version for your hosting account.',
				'error'
			);

			return false;
		}

		if ($type === 'update')
		{
			$this->deleteOldUpdateSite();
		}


	}

	private function deleteOldUpdateSite(): void
	{
		/* @var \Joomla\Database\DatabaseDriver $db */

		$db    = Factory::getContainer()->get('db');
		$query = $db->getQuery(true)->delete('#__update_sites')->where($db->quoteName('location') . ' = ' . $db->quote('https://www.joomdonation.com/updates/edocman.xml'));
 
		$db->setQuery($query)->execute();
	}

	/**
	 * Ensure Task - Events Booking plugin is enabled
	 *
	 * @return void
	 */
	private function ensureAutoCleanPluginIsEnabled(): void
	{
		$plugin = PluginHelper::getPlugin('system', 'autocleanedocman');

		// Plugin is already enabled
		if ($plugin)
		{
			return;
		}

		/* @var \Joomla\Database\DatabaseDriver $db */
		$db    = Factory::getContainer()->get('db');
		$query = $db->getQuery(true)
			->update('#__extensions')
			->set('enabled = 1')
			->where('element = ' . $db->quote('autocleanedocman'))
			->where('folder = ' . $db->quote('system'));
		$db->setQuery($query)
			->execute();
	}

	public function postflight($type, $parent)
	{
		if ($type == 'install' || $type == 'update') {
			$app			= Factory::getApplication();
			$db				= Factory::getContainer()->get('db');

			$this->ensureAutoCleanPluginIsEnabled();

			$query			= $db->getQuery(true)
				->select($db->quoteName('template'))
				->from($db->quoteName('#__template_styles'))
				->where($db->quoteName('client_id') . ' = 0')
				->where($db->quoteName('home') . ' = 1');
			$db->setQuery($query);
			$template		= $db->loadResult();
			$overrides		= [];
			
			$views = array('categories', 'category', 'document', 'documents', 'common', 'search');
			
			foreach ($views as $view) {
				$overridePath = JPATH_ROOT . '/templates/' . $template . '/html/com_edocman/' . $view;
				if (is_dir(Path::clean($overridePath))) {
					$overrides[] = $view;
				}
			}
			
			if (!empty($overrides)) {
				$app->enqueueMessage("Please be informed that some layouts of Edocman are currently being overridden in the frontend template. You may need to update them to incorporate the new features of the component. The following layouts of Edocman component have been overridden in frontend's template: <strong>" . implode(', ', $overrides) . "</strong>", 'notice');
			}
        }
		?>
		<style>
		.alert-info
		{
			background:none !important;
			padding:0px !important;
		}
		.success-container {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
            overflow: hidden;
        }
        
        .success-container .header {
            display: flex;
            align-items: center;
            padding: 0;
        }
        
        .success-container .logo-container {
            flex: 0 0 220px;
            background-color: white;
            padding: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-right: 1px solid #EDE7F6;
        }
        
        .success-container .logo {
            max-width: 180px;
            height: auto;
        }

		.success-container .header .logo
		{
			background-color: white;
		}
        
        .success-container .header-text {
            flex: 1;
            text-align: center;
            background-color: #82267C; /* Edocman purple color */
            background: linear-gradient(135deg, #7E57C2, #82267C);
            color: white;
            padding: 15px 20px;
            height: 100%;
        }
        
        .success-container .header h1 {
            font-size: 22px;
            margin-bottom: 3px;
            text-shadow: 1px 1px 2px rgba(0,0,0,0.2);
			color:#fff;
        }
        
        .success-container .content {
            padding: 20px;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
        }
        
        .product-image-container {
            flex: 0 0 300px;
            padding: 10px;
        }
        
        .product-image {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border: 1px solid #EDE7F6; /* Light purple border */
        }
        
        .success-content {
            flex: 1;
            padding: 10px 20px;
            min-width: 300px;
        }
        
        .success-message {
            background-color: #EDE7F6; /* Light purple background */
            border-left: 4px solid #82267C; /* Edocman purple */
            padding: 12px;
            margin-bottom: 15px;
            border-radius: 4px;
        }
        
        .success-icon {
            color: #82267C; /* Edocman purple */
            font-size: 20px;
            margin-right: 8px;
            vertical-align: middle;
        }
        
        .next-steps {
            margin-top: 15px;
        }
        
        .next-steps h3 {
            margin-bottom: 10px;
            color: #512DA8; /* Darker purple for headings */
            font-size: 16px;
        }
        
        .next-steps ul {
            padding-left: 20px;
            margin-bottom: 15px;
        }
        
        .next-steps li {
            margin-bottom: 5px;
            font-size: 14px;
        }
        
        .next-steps li::marker {
            color: #82267C; /* Purple bullets */
        }
        
        .success-content .btn {
            display: inline-block;
            background-color: #82267C; /* Edocman purple */
            padding: 8px 16px;
            border-radius: 4px;
            text-decoration: none;
            font-weight: bold;
            transition: background-color 0.3s;
            box-shadow: 0 2px 4px rgba(103, 58, 183, 0.3);
			color:#fff !important;
        }
        
        .btn:hover {
            background-color: #5E35B1; /* Darker purple on hover */
            box-shadow: 0 4px 8px rgba(103, 58, 183, 0.4);
        }
        
        .footer {
            background-color: #EDE7F6; /* Light purple background */
            padding: 10px;
            text-align: center;
            font-size: 12px;
            color: #673AB7; /* Edocman purple */
            border-top: 1px solid #D1C4E9; /* Very light purple border */
        }
        
        .purple-accent {
            height: 4px;
            background: linear-gradient(90deg, #7E57C2, #5E35B1);
        }

        @media (max-width: 768px) {
            .content {
                flex-direction: column;
            }
            
            .product-image-container, .success-content {
                flex: 0 0 100%;
            }
            
            .header {
                flex-direction: column;
            }
            
            .logo-container {
                border-right: none;
                border-bottom: 1px solid #EDE7F6;
                width: 100%;
                padding: 15px 0;
            }
            
            .header-text {
                width: 100%;
            }
        }
		</style>
		<?php
		//echo $returnLayouts;	
		?>
		<div class="success-container">
			<div class="purple-accent"></div>
			<div class="header">
				<div class="logo-container">
					<img src="https://joomdonation.com/images/logo.png" alt="Joom Donation" class="logo" />
				</div>
				<div class="header-text">
					<h1>Edocman for Joomla</h1>
					<p>Joomla Download Manager - Documents Management</p>
				</div>
			</div>
			
			<div class="content">
				<div class="product-image-container">
					<img src="https://joomdonation.com/images/com_dms/documents/edocman.png" alt="Edocman Extension" class="product-image">
				</div>
				
				<div class="success-content">
					<div class="success-message">
						<span class="success-icon">✓</span>
						<strong>Installation Successful!</strong> 
						<p>Congratulations! Edocman has been successfully installed on your Joomla website.</p>
					</div>
					
					<div class="next-steps">
						<h3>Next Steps:</h3>
						<ul>
							<li>Configure Edocman settings in the admin panel</li>
							<li>Create document categories</li>
							<li>Upload your documents</li>
						</ul>
						<a href="index.php?option=com_edocman" class="btn">Go to Edocman Dashboard</a>
					</div>
				</div>
			</div>
			
			<div class="footer">
				&copy; 2025 Edocman - Professional Document Management for Joomla
			</div>
		</div>
		<?php
	}
}