<?php
/**
 * @copyright (C) 2013 iJoomla, Inc. - All rights reserved.
 * @license GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html)
 * @author iJoomla.com <webmaster@ijoomla.com>
 * @url https://www.jomsocial.com/license-agreement
 * The PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript *are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0
 * More info at https://www.jomsocial.com/license-agreement
 */

use Joomla\CMS\Factory;
use Joomla\CMS\Filesystem\File;
use Joomla\CMS\Helper\UserGroupsHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Table\Table;

// Disallow direct access to this file
defined('_JEXEC') or die('Restricted access');

class CommunityThemeHelper
{

    private $matches;

    /**
     * Handles storage of SCSS related POST data into SCSS overrides file and JSON encoded database entries
     * @param $postScss - array of SCSS POST data
     * @param $saveKey - which primary storage key within JSON structure is used
     */
    public static function parseScss($postScss, $saveKey)
    {
        $mainframe = Factory::getApplication();
        $saveKeyUp = strtoupper($saveKey);

        // String for SCSS override file
        $scssString = '';

        // Array for JSON storage
        $scssArray = array();

        $safeHtmlFilter = CFactory::getInputFilter();

        foreach ($postScss as $key => $value) {

            if (!strlen($key)) continue;
            unset($color);

            // Build result array
            if (strlen($value)) {
                $key = $safeHtmlFilter->clean($key);
                $value = $safeHtmlFilter->clean($value);
                $scssArray[$key] = $value;
            }

            if ($value != '') {
                $scssString .= "\n" . '$' . $key . ": ";
                if ($saveKey == 'colors') $scssString .= "#";
                $scssString .= "$value;";
            }
        }

        // Store SCSS override JSON encoded in the database
        $themeTable = Table::getInstance('Theme', 'CommunityTable');
        $themeTable->load('scss');
        $themeTable->key = 'scss'; // needed for new record

        if (strlen($themeTable->value)) {
            $oldScss = json_decode($themeTable->value, true);
        } else {
            // Fallback if the JSON settings don't exist yet
            $oldScss = array(
                'colors' => array(),
                'general' => array()
            );
        }

        $oldScss[$saveKey] = $scssArray;

        $themeTable->value = json_encode($oldScss);

        $themeTable->store();

        // Find the _variables.scss
        $variablesFile = CFactory::getPath('template://scss/_variables.scss');
        jimport('joomla.file');

        // FAIL if it doesn't exist
        if (!strlen($variablesFile) || !File::exists($variablesFile)) {
            $message = Text::_('COM_COMMUNITY_THEME_' . $saveKeyUp . '_COULD_NOT_COMPILE_SCSS');
            $mainframe->enqueueMessage($message, 'message');
            $mainframe->redirect('index.php?option=com_community&view=theme' . $saveKey);
        }

        // Read the contents of the file
        $variablesContent = file_get_contents($variablesFile);

        // Find the beginninng and end of the overrides area
        $startTag = '//OVERRIDES_' . $saveKeyUp . '_START';
        $endTag = '//OVERRIDES_' . $saveKeyUp . '_END';

        $overrideStart = strpos($variablesContent, $startTag);
        $overrideEnd = strpos($variablesContent, $endTag);

        // FAIL if can't find both
        if (!is_int($overrideStart) || !is_int($overrideEnd) || !$overrideStart > 0 || !$overrideEnd > 0) {
            $message = Text::_('COM_COMMUNITY_THEME_' . $saveKeyUp . 'S_COULD_NOT_COMPILE_SCSS');
            $mainframe->enqueueMessage($message, 'message');
            $mainframe->redirect('index.php?option=com_community&view=theme' . $saveKey);
        }

        // Exctract the file contents before existing overrides
        $variablesHead = substr($variablesContent, 0, $overrideStart);

        // Extract the file contents after existing overrides
        $variablesTail = substr($variablesContent, $overrideEnd + strlen($endTag), strlen($variablesContent) - $overrideEnd);

        // Stitch the Head, new SCSS, and Tail together, effectively getting rid of old SCSS overrides (if any)
        $variablesNew =
            trim( $variablesHead ) . "\n" .
            trim( $startTag ) . "\n" .
            trim( $scssString ) . "\n" .
            trim( $endTag ) . "\n" .
            trim( $variablesTail );

        // Write to file
        File::write($variablesFile, $variablesNew);

        // Run the compiler
        $compiler = new CCompiler();
        $compiler = $compiler->buildSCSS();
        $compiler->saveCSSFile(Factory::getApplication()->input->get('theme_profile', 'style'));

        /************** SCSS DIRECTION OVERRIDES HAPPEN HERE ***************/
        $variablesContent = $variablesNew;
        // Find the beginninng and end of the RTL overrides area
        $startTag = '//OVERRIDES_RTL_START';
        $endTag = '//OVERRIDES_RTL_END';

        $overrideStart = strpos($variablesContent, $startTag);
        $overrideEnd = strpos($variablesContent, $endTag);

        // FAIL if can't find both
        if (!is_int($overrideStart) || !is_int($overrideEnd) || !$overrideStart > 0 || !$overrideEnd > 0) {
            $message = Text::_('COM_COMMUNITY_THEME_RTL_COULD_NOT_COMPILE_SCSS');
            $mainframe->enqueueMessage($message, 'message');
            $mainframe->redirect('index.php?option=com_community&view=theme' . $saveKey);
        }

        // Exctract the file contents before existing overrides
        $variablesHead = substr($variablesContent, 0, $overrideStart);

        // Extract the file contents after existing overrides
        $variablesTail = substr($variablesContent, $overrideEnd + strlen($endTag), strlen($variablesContent) - $overrideEnd);

        /********************** LTR ********************/
        // Stitch the Head, Direction, and Tail together, adding LTR
        $variablesNewLTR =
            trim( $variablesHead ) . "\n" .
            trim( $startTag ) . "\n" .
            '$scss-direction: ltr;'. "\n" .
            trim( $endTag ) . "\n" .
            trim( $variablesTail );

        // Write to file
        File::write($variablesFile, $variablesNewLTR);
        // Run the compiler
        $compiler = new CCompiler();
        $compiler = $compiler->buildSCSS();
        $compiler->saveCSSFile(Factory::getApplication()->input->get('theme_profile', 'style'));

        /********************** RTL ********************/
        // Stitch the Head, Direction, and Tail together, adding RTL
        $variablesNewRTL =
            trim( $variablesHead ) . "\n" .
            trim( $startTag ) . "\n" .
            '$scss-direction: rtl;'. "\n" .
            trim( $endTag ) . "\n" .
            trim( $variablesTail );

        // Write to file
        File::write($variablesFile, $variablesNewRTL);
        // Run the compiler
        $compiler = new CCompiler();
        $compiler = $compiler->buildSCSS();
        $compiler->saveCSSFile(Factory::getApplication()->input->get('theme_profile', 'style.rtl'));
    }

    public static function parseSettings($settings, $saveKey)
    {
        $settingsArray = array();
        $safeHtmlFilter = CFactory::getInputFilter();

        foreach ($settings as $key => $value) {

            if (!strlen($key)) continue;

            if(is_array($value) && isset($value['tagline'])){
                $key = $safeHtmlFilter->clean($key);
                foreach($value as $v){
                    $v = $safeHtmlFilter->clean($v);
                }
                $settingsArray [$key] = $value;
            }elseif (strlen($value)) {
                $key = $safeHtmlFilter->clean($key);
                $value = $safeHtmlFilter->clean($value);
                $settingsArray [$key] = $value;
            }
        }
        
        // Store SCSS override JSON encoded in the database
        $themeTable = Table::getInstance('Theme', 'CommunityTable');
        $themeTable->load('settings');
        $themeTable->key = 'settings'; // needed for new record

        if (strlen($themeTable->value)) {
            $oldSettings = json_decode($themeTable->value, true);
        } else {
            $oldSettings = array(
                'profile' => array(),
                'general' => array(),
                'group' => array(),
                'event' => array()
            );
        }

        if ($saveKey == 'profile') {
            //print_r($oldSettings[$saveKey]);
            //print_r($settingsArray);

            // name badge array update
            $groups = UserGroupsHelper::getInstance()->getAll();
            foreach ($groups as $group) {
                $badgeIndex = 'profile-group-badge-' . $group->id;
                if (isset($oldSettings[$saveKey][$badgeIndex]) && isset($settingsArray[$badgeIndex])) {
                    $oldSettings[$saveKey][$badgeIndex] = $settingsArray[$badgeIndex];
                }
            }

            foreach ($oldSettings[$saveKey] as $key => $value) {
                if ($key == 'tagline' || is_array($oldSettings[$saveKey][$key])) {
                    unset($oldSettings[$saveKey][$key]);
                }
            }

            //only override the old settings if exist without using original index key for profile field
            $oldSettings[$saveKey] = (is_array($oldSettings[$saveKey])) ? ($oldSettings[$saveKey] + $settingsArray) : $settingsArray;
        } else {
            //only override the old settings if exist
            $oldSettings[$saveKey] = (is_array($oldSettings[$saveKey])) ? array_merge($oldSettings[$saveKey],$settingsArray) : $settingsArray;
        }
        
        $themeTable->value = json_encode($oldSettings);
        $themeTable->store();
    }


    // Cover info parser
    public function prepareCoverInfo(&$settings)
    {
        $taglines = array();
        for ($key = 0; $key < 10; $key++) {
            if (!isset($settings['profileSpaceBefore' . $key])) {
                break;
            }
            $spacebefore = $settings['profileSpaceBefore' . $key];
            unset($settings['profileSpaceBefore' . $key]);

            $before = $settings['profileBefore' . $key];
            unset($settings['profileBefore' . $key]);

            $field = $settings['profileField' . $key];
            unset($settings['profileField' . $key]);

            $after = $settings['profileAfter' . $key];
            unset($settings['profileAfter' . $key]);

            $spaceafter = $settings['profileSpaceAfter' . $key];
            unset($settings['profileSpaceAfter' . $key]);

            $profileFields[] = array(
                'spacebefore' => $spacebefore,
                'before' => $before,
                'field' => $field,
                'after' => $after,
                'spaceafter' => $spaceafter,
            );
        }

        $taglines['tagline'] = json_encode($profileFields); // this is for default profile
        $taglines['name_badge_position'] = $settings['name_badge_position'];
        //there might be additional settings for multiprofiles, lets find it out
        $multiProfilesModel = new CommunityModelMultiProfile();
        $multiProfiles = $multiProfilesModel->getMultiProfiles();
        foreach($multiProfiles as $profile) {
            $profileFields = array();
            for ($key = 0; $key < 10; $key++) {
                if (!isset($settings['profileSpaceBefore'.$key.'_'.$profile->id])) {
                    break;
                }

                $spacebefore = $settings['profileSpaceBefore'.$key.'_'.$profile->id];
                unset($settings['profileSpaceBefore'.$key.'_'.$profile->id]);

                $before = $settings['profileBefore'.$key.'_'.$profile->id];
                unset($settings['profileBefore'.$key.'_'.$profile->id]);

                $field = $settings['profileField'.$key.'_'.$profile->id];
                unset($settings['profileField'.$key.'_'.$profile->id]);

                $after = $settings['profileAfter'.$key.'_'.$profile->id];
                unset($settings['profileAfter'.$key.'_'.$profile->id]);

                $spaceafter = $settings['profileSpaceAfter'.$key.'_'.$profile->id];
                unset($settings['profileSpaceAfter'.$key.'_'.$profile->id]);

                $profileFields[] = array(
                    'spacebefore' => $spacebefore,
                    'before' => $before,
                    'field' => $field,
                    'after' => $after,
                    'spaceafter' => $spaceafter,
                );
            }
            $taglines[$profile->id]['tagline'] = json_encode($profileFields);

        }

        return $taglines;
    }

    public static function getDefault($key)
    {
        /*
         * This is where we store our own defaults
         * in case the template file is broken or otherwise not available
         */
        $defaultSettings = JPATH_ROOT.'/components/com_community/templates/jomsocial/assets/default.' . $key . '.json';

        /*
         * Attempt to read and parse template settings
         */
        $templateSettings = CFactory::getPath('template://assets/default.' . $key . '.json');
        jimport('joomla.file');

        // If the file exists, and contains parseable JSON data
        if (strlen($templateSettings) &&  File::exists($templateSettings)) {

            $json = file_get_contents($templateSettings);

            if(strlen($json)) {
                $settings = json_decode($json, true);
            }
        }

        if(!isset($settings) || !is_array($settings)) {
            return json_decode(file_get_contents($defaultSettings), true);
        }

        return $settings;
    }

    public static function licenseViewDisable()
    {
        if (COMMUNITY_PRO_VERSION) {
            return '<div class="feature--disabled">
                <h3>'.Text::_('COM_COMMUNITY_FEATURE_DISABLED_TITLE').'</h3>
                <p>'.Text::_('COM_COMMUNITY_FEATURE_DISABLED_DESC').'</p>
                <div class="space-16"></div>
                <a href="http://tiny.cc/kwk0px" class="btn btn-primary">'.Text::_('COM_COMMUNITY_BUY').'</a>
                <a href="http://tiny.cc/cyk0px" class="btn btn-success">'.Text::_('COM_COMMUNITY_UPGRADE').'</a>
            </div>';
        }
    }

}