<?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 anHomeProductFiles
{
    public function __construct()
    {
        $this->themeDir = Context::getContext()->shop->theme->getDirectory();
        $this->themeName = Context::getContext()->shop->theme->getName();
        
        $this->moduleDir = _PS_MODULE_DIR_ . 'an_homeslider/';

        $this->fileJsonDir = $this->themeDir.'assets/antheme/an_homeproducts/';
        $this->fileJsonBanners = $this->themeDir.'assets/antheme/an_homeproducts/banners.json';
        $this->fileJsonblocks = $this->themeDir.'assets/antheme/an_homeproducts/blocks.json';
    }




    public function getJsonDirblocks()
    { 
        return $this->getJsonDir() . 'blocks.json';
    }

    public function getJsonDirBanners()
    { 
        return $this->getJsonDir() . 'banners.json';
    }

    public function getJsonDir()
    { 
        $this->createFolders();

        if (Tools::file_exists_no_cache($this->fileJsonDir)){
            return $this->fileJsonDir;
        }

        return $this->moduleDir;
    }

    public function getJsonBannersPathFile()
    { 
        if (Tools::file_exists_no_cache($this->fileJsonDir . 'banners.json')){
            return Tools::file_get_contents($this->fileJsonDir . 'banners.json');
        } else if (Tools::file_exists_no_cache($this->moduleDir . 'banners.json')){
            return Tools::file_get_contents($this->moduleDir . 'banners.json');
        }

        return '';
    }

    public function getJsonBlocksPathFile()
    { 
        if (Tools::file_exists_no_cache($this->fileJsonDir . 'blocks.json')){
            return Tools::file_get_contents($this->fileJsonDir . 'blocks.json');
        } else if (Tools::file_exists_no_cache($this->moduleDir . 'blocks.json')){
            return Tools::file_get_contents($this->moduleDir . 'blocks.json');
        }

        return '';
    }

    public function createFolders()
    {
        $this->checkCreateFolder($this->fileJsonDir);
    }    

    public function checkCreateFolder($path)
    {
        if (!Tools::file_exists_no_cache($path)){
            mkdir($path, 0777, true);
            Tools::copy($this->moduleDir . 'index.php', $path . 'index.php');
        }
    }   












    public function checkAndGetDir()
    {
        $this->createFoldersImg();
        
        if (Tools::file_exists_no_cache($this->imgDir)){
            return $this->imgDir;
        } else if (Tools::file_exists_no_cache($this->imgDirOld)){
            return $this->imgDirOld;
        }
        
        return '';
    }

    public function getUrlImg($imgName = '')
    {
        if ($imgName == ''){
            return '';
        }

        if (Tools::file_exists_no_cache($this->imgDir.$imgName)){
            return $this->imgUrl . $imgName;
        } else if (Tools::file_exists_no_cache($this->imgDirOld.$imgName)){
            return $this->imgUrlOld . $imgName;
        }
        
        return '';
    }

    public function getPathImg($imgName = '')
    {
        if ($imgName == ''){
            return '';
        }

        if (Tools::file_exists_no_cache($this->imgDir.$imgName)){
            return $this->imgDir . $imgName;
        } else if (Tools::file_exists_no_cache($this->imgDirOld.$imgName)){
            return $this->imgDirOld . $imgName;
        }
        
        return '';
    }    

    public function deleteImg($imgName)
    {
        @unlink($this->imgDir . $imgName);
        @unlink($this->imgDirOld . $imgName);
    }







    
 
}