<?php
/**
* 2023 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2023 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

class anThemeFiles 
{
    public $themeDir;
    public $mainThemeDir;
    public $themeName;
    public $mainThemeName;
    public $contentDir;
    public $moduleDir;
    public $MOD_FILES_THEME;
    public $pathModuleFiles;
    public $pathCss;
    public $pathJs;
    public $pathPresets;
    public $pathConfig;
    public $pathImg;
    public $uriImg;
    public $oldPathCss;
    public $oldPathJs;
    public $cssGenerator;

    public function __construct()
    {
        $this->themeDir = Context::getContext()->shop->theme->getDirectory();
        $this->themeName = Context::getContext()->shop->theme->getName();
        $parentThemeFolder = Context::getContext()->shop->theme->get('parent');

        $this->mainThemeDir = $this->themeDir;
        $this->mainThemeName = $this->themeName;
        if ($parentThemeFolder){
            $this->mainThemeDir = _PS_ALL_THEMES_DIR_ . $parentThemeFolder . '/';
            $this->mainThemeName = $parentThemeFolder;
        }

        $this->contentDir = $this->mainThemeDir.'assets/antheme/';




        if ($parentThemeFolder && !Tools::file_exists_no_cache($this->contentDir)){
            $this->contentDir = $this->mainThemeDir.'assets/antheme/';
        } 
        
        
        $this->pathModuleFiles = $this->mainThemeDir.'assets/antheme/';
        $this->pathCss = $this->mainThemeDir.'assets/antheme/css/';
        $this->pathJs = $this->mainThemeDir.'assets/antheme/js/';
        $this->pathConfig = $this->mainThemeDir.'assets/antheme/config/';

        //  Presets
        $this->pathPresets = $this->mainThemeDir.'assets/antheme/presets/';
    
        //  Img
        $this->MOD_FILES_THEME = 'themes/'.$this->mainThemeName.'/assets/antheme/';
        $this->pathImg = $this->mainThemeDir.'assets/antheme/img/';
        $this->uriImg = __PS_BASE_URI__ . $this->MOD_FILES_THEME . 'img/';

        $this->moduleDir = _PS_MODULE_DIR_ . 'an_theme/';
        
    
        $this->oldPathCss = _PS_MODULE_DIR_ . 'an_theme/views/css/';
        $this->oldPathJs = _PS_MODULE_DIR_ . 'an_theme/views/js/';

        $this->cssGenerator = '';
    }

    public function getCssDir()
    { 
        $this->createFoldersJsCss();
        if (Tools::file_exists_no_cache($this->pathCss)){
            return $this->pathCss;
        }
        return $this->oldPathCss;
    }

    public function getJsDir()
    { 
        $this->createFoldersJsCss();
        if (Tools::file_exists_no_cache($this->pathJs)){
            return $this->pathJs;
        }
        return $this->oldPathJs;
    }

    public function getFontPath($folderFile)
    {
        if ($folderFile == ''){
            return false;
        }
        
        if (Tools::file_exists_no_cache($this->pathModuleFiles . 'fonts/' . $folderFile . '.css')){       
            $path = $this->MOD_FILES_THEME . 'fonts/' . $folderFile . '.css';   
        } elseif (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'an_theme/views/fonts/' . $folderFile . '.css')) {   
            $path = 'modules/an_theme/views/fonts/' . $folderFile . '.css';        
        } else {
            return false;
        }

        return $path;
    }

    public function getCssJsPath($fileName, $type = 'css')
    {
        if ($fileName == ''){
            return false;
        }

        if (Tools::file_exists_no_cache($this->mainThemeDir.'assets/antheme/'.$type.'/' . $fileName)){         
            $path = 'themes/'.$this->mainThemeName.'/assets/antheme/'.$type.'/' . $fileName;   
        } elseif (Tools::file_exists_no_cache(_PS_MODULE_DIR_ . 'an_theme/views/'.$type.'/' . $fileName)) {   
            $path = 'modules/an_theme/views/'.$type.'/' . $fileName;        
        } else {
            return false;
        }

        return $path;
    }    

    public function generateFileName($format = 'css', $prefix = 'antheme-')
    {
        return $prefix . md5(uniqid() . Context::getContext()->shop->id)  . '.' . $format;
    }

    public function killFile($file)
    {
       @unlink(_PS_MODULE_DIR_ . 'an_theme/views/' .  Tools::substr($file, strpos($file, '.') + 1) .'/'.$file);
       @unlink($this->pathModuleFiles .  Tools::substr($file, strpos($file, '.') + 1) .'/'.$file);
        return $file;
    }
    
    public function findFile($name, $path, $oldPath)
    {
        $fileName = '';

        $globalResult = glob($path . $name);
        if (!is_array($globalResult) || count($globalResult) < 1){
            $globalResult = glob($oldPath . $name);
        }
        if (is_array($globalResult) && count($globalResult) > 0){
            $fileName = basename(array_shift($globalResult));
        }
        
        if ($fileName != ''){
            return $fileName;
        }

        return false;
    }

    public function createFoldersJsCss()
    {
        anThemeFiles::checkCreateFolder($this->pathCss);
        anThemeFiles::checkCreateFolder($this->pathJs);
        anThemeFiles::checkCreateFolder($this->pathPresets);
        anThemeFiles::checkCreateFolder($this->pathConfig);
        anThemeFiles::checkCreateFolder($this->pathImg);

        Tools::copy(_PS_MODULE_DIR_ . 'an_theme/index.php', $this->pathModuleFiles . 'index.php');
    }    

    public function checkCreateFolder($path)
    {
        if (!Tools::file_exists_no_cache($path)){
            mkdir($path, 0777, true);
            Tools::copy(_PS_MODULE_DIR_ . 'an_theme/index.php', $path . 'index.php');
        }
    }

    public function getNewFileNameExt($name)
    {
        $ext = substr($name, strrpos($name, '.') + 1);
        return md5(uniqid()) . '.' . $ext;
    }
}
?>