<?php
/**
 * Flex @package Helix Framework
 * Template Name - Flex
 * @author Aplikko http://www.aplikko.com
 * @copyright Copyright (c) 2025 Aplikko
 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 or later
*/
// no direct access
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\Folder;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\Installer\Installer;
use Joomla\Database\DatabaseInterface;

class plgSystemTmp_helix3InstallerScript {

    function postflight($type, $parent)
    {
        $db = Factory::getContainer()->get(DatabaseInterface::class);
        $status = new stdClass;
        $status->plugins = array();

        $src = $parent->getParent()->getPath('source');
        $manifest = $parent->getParent()->manifest;
        $plugins = $manifest->xpath('plugins/plugin');

        foreach ($plugins as $key => $plugin)
        {
            $name = (string)$plugin->attributes()->plugin;
            $group = (string)$plugin->attributes()->group;
            $path = $src.'/plugins/'.$group;

            if (is_dir($src.'/plugins/'.$group.'/'.$name))
            {
                $path = $src.'/plugins/'.$group.'/'.$name;
            }

            $installer = new Installer();
            $installer->setDatabase(Factory::getContainer()->get(DatabaseInterface::class));
            $result = $installer->install($path);

            if ($result)
            {
                $db = Factory::getContainer()->get(DatabaseInterface::class);
                $query = $db->getQuery(true);
                $fields = array(
                    $db->quoteName('enabled') . ' = 1'
                );

                $conditions = array(
                    $db->quoteName('type') . ' = ' . $db->quote('plugin'),
                    $db->quoteName('element') . ' = ' . $db->quote($name),
                    $db->quoteName('folder') . ' = ' . $db->quote($group)
                );

                $query->update($db->quoteName('#__extensions'))->set($fields)->where($conditions);
                $db->setQuery($query);
                $db->execute();
            }
        }

        $template_path = $src.'/template';

        if (is_dir($template_path))
        {
            $installer = new Installer();
            $installer->setDatabase(Factory::getContainer()->get(DatabaseInterface::class));
            $installer->install($template_path);
        }

        $conf = Factory::getConfig();
        $conf->set('debug', false);
        
        echo $this->getInstallationHTML();
        
        return true;
    }
    
    /**
     * Generate the installation HTML message
     */
    private function getInstallationHTML()
    {
        ob_start();
        ?>
        <div class="container-fluid">
            <div class="row">
                <div class="col-12">
                    <div class="alert alert-success d-flex align-items-center">
                        <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" width="28" height="28" fill="none" class="me-3">
                            <style>
                                @keyframes check {
                                    to {
                                        stroke-dashoffset: 0;
                                    }
                                }
                                .flex-check-animation {
                                    animation: check 1.5s ease-in-out forwards;
                                }
                            </style>
                            <circle cx="12" cy="12" r="8" stroke="#28a745" stroke-width="1.5"/>
                            <path stroke="#28a745" stroke-linecap="round" stroke-width="1.5" d="M9.215 12.052l1.822 1.805 3.748-3.714" class="flex-check-animation" stroke-dashoffset="100" stroke-dasharray="100"/>
                        </svg>
                        <h4 class="m-0">Installation of Flex Template was successful!</h4>
                    </div>
                </div>
            </div>
        </div>

        <style type="text/css">
        .flex-installation-wrap{
            padding: 25px;
            background: #f8f9fa;
            border-radius: 8px;
            margin: 20px 0;
            border: 1px solid #dee2e6;
        }
        .flex-installation-wrap .flex-installation-left p {
            text-align:center;
            margin:12px auto;
            color:#6c757d;
            font-size: 1em;
            font-weight: 500;
        }
        .flex-installation-wrap .flex-installation-footer{
            margin-top: 25px;
            padding-top: 20px;
            border-top: 1px solid #dee2e6;
        }
        .flex-installation-wrap img {
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .flex-installation-wrap iframe {
            border-radius: 6px;
        }
        .flex-installation-wrap h2 {
            color: #2c3e50;
            margin-bottom: 15px;
            font-weight: 600;
            font-size: 1.5rem;
        }
        .flex-installation-wrap h3 {
            color: #34495e;
            margin-bottom: 12px;
            font-size: 1.3rem;
        }
        .flex-installation-wrap h4 {
            font-size: 1.1rem;
            margin-bottom: 10px;
        }
        .flex-description {
            font-size: 0.95rem;
            line-height: 1.5;
            color: #495057;
        }
        .btn-flex {
            padding: 10px 22px;
            font-weight: 500;
            border-radius: 6px;
            transition: all 0.2s ease;
            border: none;
            text-decoration: none;
            display: inline-block;
            text-align: center;
            margin: 5px;
            min-width: 160px;
            font-size: 0.9rem;
        }
		.btn-flex:before,
		.btn:before,
		.btn:after,
		.btn-action:before,
		.btn-action:after {
			content:'';
			display:none;
		}
        .btn-flex:hover {
            transform: translateY(-2px);
            text-decoration: none;
        }
        .btn-flex-primary {
            background: #f14833;
            color: white !important;
        }
        .btn-flex-secondary {
            background: #6c757d;
            color: white !important;
        }
        .btn-flex-tertiary {
            background: #0C8590;
            color: white !important;
        }
        .feature-badge {
            background: #52986B;
            color: white;
            padding: 7px 20px 7px 14px;
            border-radius: 20px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            margin-bottom: 15px;
            font-size: 0.9rem;
            gap: 8px;
        }
        .video-container {
            margin-bottom: 20px;
        }
        .feature-list {
            list-style: none;
            padding-left: 0;
            margin-bottom: 15px;
        }
        .feature-list li {
            margin-bottom: 6px;
            padding-left: 0;
            display: flex;
            align-items: center;
        }
        .feature-list svg {
            margin-right: 8px;
            flex-shrink: 0;
        }
        .pro-tip {
            font-size: 0.85rem;
            color: #6c757d;
        }
        .flex-check {
            width: 16px;
            height: 16px;
        }
        </style>

        <div class="container-fluid">
            <div class="flex-installation-wrap">
                <!-- Header Section -->
                <div class="row align-items-start mb-3">
                    <div class="col-12 col-md-5 flex-installation-left mb-4">
                        <img class="w-100" src="https://flex.aplikko.com/templates/flex/template_preview.png" alt="Flex Template" />
                        <p>
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24">
							 <style>
							  .sliders {animation: sliders 1.5s cubic-bezier(0.860, 0.000, 0.070, 1.000) infinite alternate both;}
							@keyframes sliders {
							  0% {
								transform: translateX(0px); 
							  }
							  100% {
								transform: translateX(10px); 
							  }
							}
							  .sliders-2 {animation: sliders-2 1.5s cubic-bezier(0.860, 0.000, 0.070, 1.000) infinite alternate both;}
							@keyframes sliders-2 {
							  0% {
								transform: translateX(0px); 
							  }
							  100% {
								transform: translateX(-10px); 
							  }
							}
							</style>
							  <path stroke="#595959" stroke-linecap="round" stroke-width="1.4" d="M6 8.746h12M6 15.317h12"/>
							  <circle class="sliders" cx="7.5" cy="8.746" r="1.5" fill="#f14833" stroke="#f14833" stroke-width="1.5"/>
							  <circle class="sliders-2" cx="16.5" cy="15.254" r="1.5" fill="#f14833" stroke="#f14833" stroke-width="1.5"/>
							</svg>
                            Version 4.3 - Joomla 6 Ready
                        </p>
                    </div>
                    <div class="col-12 col-md-7 flex-installation-texts">
                        <h2>FLEX - Most Flexible Joomla Template</h2>
                        <p class="flex-description">Flex is a bestseller, super flexible, multi-purpose Joomla template that can fit in any type of website you are building. It is clean, responsive, includes drag &amp; drop Layout and Page Builder and comes packed with powerful options!</p>
                        
                        <div class="feature-badge">                                    
                            <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="none">
                                <style>
                                    @keyframes flex-check {
                                        to {
                                            stroke-dashoffset: 0;
                                        }
                                    }
                                    .flex-badge-animation {
                                        animation: flex-check 3s infinite cubic-bezier(.99,-.1,.01,1.02) 3s;
                                    }
                                </style>
                                <circle cx="12" cy="12" r="8" stroke="white" stroke-width="1.5"/>
                                <path stroke="white" stroke-linecap="round" stroke-width="1.5" d="M9.215 12.052l1.822 1.805 3.748-3.714" class="flex-badge-animation" stroke-dashoffset="100" stroke-dasharray="100"/>
                            </svg>
                            Fully Joomla 6 Compatible
                        </div>
                        
                        <div class="row mt-3">
                            <div class="col-12 col-sm-6 mb-3">
                                <strong>Key Features</strong><hr />
                                <ul class="feature-list">
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Drag &amp; Drop Page Builder
                                    </li>
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Fully Responsive Design
                                    </li>
                                    <li>
                                       <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        60+ Pre-built Pages
                                    </li>
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Advanced Typography
                                    </li>
                                </ul>
                            </div>
                            <div class="col-12 col-sm-6 mb-3">
                                <strong>Includes</strong><hr />
                                <ul class="feature-list">
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Multiple Header Styles
                                    </li>
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Custom Color Schemes
                                    </li>
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Google Fonts Integration
                                    </li>
                                    <li>
                                        <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="currentColor" class="flex-check" viewBox="0 0 16 16"><path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0"/></svg>
                                        Lifetime Updates
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                
                <hr style="margin:0 0 30px 0; border-top:1px solid #C9CACB;" />
                
                <!-- Content Section -->
                <div class="row">
                    <div class="col-12 col-md-5 flex-installation-left mb-4">
                        <div class="video-container">
                            <h4>Getting Started Guide<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" fill="#999999" class="mx-2" viewBox="0 0 16 16"><path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/><path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286m1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94"/></svg></h4>
                            <div class="ratio ratio-16x9">
                                <iframe src="https://www.youtube.com/embed/agMGyThdulE" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                            </div>
                        </div>
                        <div class="video-container">
                            <h4>Advanced Features</h4>
                            <div class="ratio ratio-16x9">
                                <iframe src="https://www.youtube.com/embed/kBddBRQ-xic" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
                            </div>
                        </div>
                    </div>
                    
                    <div class="col-12 col-md-7 flex-installation-right">
                        <div class="flex-installation-texts mb-4">
                            <h3>About Joomla</h3>
                            <p class="flex-description">Joomla is an open source content management system (CMS) that helps you build powerful dynamic websites and applications. It has an intuitive interface that helps you use its features and functionality to the fullest.</p>
                            <p class="flex-description">With over 151+ million downloads since its inception in 2005, Joomla has successfully grown into one of the most widely used content management systems globally.</p>
                            <p>
                                <a href="https://6.joomla.org/" target="_blank" class="btn btn-sm btn-flex-outline-primary">
									<svg xmlns="http://www.w3.org/2000/svg" style="margin:-2px 5px 0 -3px;" width="14" height="14" fill="currentColor" class="bi bi-info-circle" viewBox="0 0 16 16"><path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/><path d="m8.93 6.588-2.29.287-.082.38.45.083c.294.07.352.176.288.469l-.738 3.468c-.194.897.105 1.319.808 1.319.545 0 1.178-.252 1.465-.598l.088-.416c-.2.176-.492.246-.686.246-.275 0-.375-.193-.304-.533zM9 4.5a1 1 0 1 1-2 0 1 1 0 0 1 2 0"/></svg>
                                    Learn More About Joomla 6
                                </a>
                            </p>
                        </div>
                        
                        <!-- Action Buttons -->
                        <div class="flex-installation-footer">
                            <h4 class="text-center mb-3">Ready to Get Started?</h4>
                            <div class="text-center">
                                <a href="index.php?option=com_templates&view=styles" class="btn-flex btn-flex-primary">
									<svg xmlns="http://www.w3.org/2000/svg" style="margin:-2px 4px 0 -3px;" width="16" height="16" fill="currentColor" class="bi bi-award" viewBox="0 0 16 16"><path d="M9.669.864 8 0 6.331.864l-1.858.282-.842 1.68-1.337 1.32L2.6 6l-.306 1.854 1.337 1.32.842 1.68 1.858.282L8 12l1.669-.864 1.858-.282.842-1.68 1.337-1.32L13.4 6l.306-1.854-1.337-1.32-.842-1.68zm1.196 1.193.684 1.365 1.086 1.072L12.387 6l.248 1.506-1.086 1.072-.684 1.365-1.51.229L8 10.874l-1.355-.702-1.51-.229-.684-1.365-1.086-1.072L3.614 6l-.25-1.506 1.087-1.072.684-1.365 1.51-.229L8 1.126l1.356.702z"/><path d="M4 11.794V16l4-1 4 1v-4.206l-2.018.306L8 13.126 6.018 12.1z"/></svg>
                                    Get Started with Flex
                                </a>
                                <a href="http://documentation.aplikko.com/flex/index.html" target="_blank" class="btn-flex btn-flex-secondary">
									<svg xmlns="http://www.w3.org/2000/svg" style="margin:-2px 8px 0 -2px;" width="16" height="16" fill="currentColor" class="me-2" viewBox="0 0 16 16">
                                        <path d="M1 2.828c.885-.37 2.154-.769 3.388-.893 1.33-.134 2.458.063 3.112.752v9.746c-.935-.53-2.12-.603-3.213-.493-1.18.12-2.37.461-3.287.811V2.828zm7.5-.141c.654-.689 1.782-.886 3.112-.752 1.234.124 2.503.523 3.388.893v9.923c-.918-.35-2.107-.692-3.287-.81-1.094-.111-2.278-.039-3.213.492V2.687zM8 1.783C7.015.936 5.587.81 4.287.94c-1.514.153-3.042.672-3.994 1.105A.5.5 0 0 0 0 2.5v11a.5.5 0 0 0 .707.455c.882-.4 2.303-.881 3.68-1.02 1.409-.142 2.59.087 3.223.877a.5.5 0 0 0 .78 0c.633-.79 1.814-1.019 3.222-.877 1.378.139 2.8.62 3.681 1.02A.5.5 0 0 0 16 13.5v-11a.5.5 0 0 0-.293-.455c-.952-.433-2.48-.952-3.994-1.105C10.413.809 8.985.936 8 1.783z"/>
                                    </svg>
                                    Documentation
                                </a>
                            </div>
                            
                            <div class="text-center mt-3">
                                <small class="pro-tip">
                                    <strong>Pro Tip:</strong> Start by configuring your template settings in <em>Extensions → Templates → Template Styles</em>
                                </small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
    
    public function abort($msg = null, $type = null)
    {
        if ($msg)
        {
            Factory::getApplication()->enqueueMessage($msg, 'warning');
        }

        foreach ($this->packages as $package)
        {
            $package['installer']->abort(null, $type);
        }
    }

}