<?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)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}

use PrestaShop\PrestaShop\Core\Module\WidgetInterface;

require_once _PS_MODULE_DIR_ . 'anproductvideogallery/classes/anProductVideosGallery.php';

class anproductvideogallery extends Module implements WidgetInterface
{
    const PREFIX = 'anpvg_';
    
    protected $_tabs = [
 
        [
            'class_name' => 'AdminProductvideoGallery',
            'parent' => 'AdminParentModulesSf',
            'name' => 'Product video Gallery: Video',
            'active' => 0
        ], 
        [
            'class_name' => 'AdminProductvideogaleleryAjax',
            'parent' => 'AdminParentModulesSf',
            'name' => 'Product video Gallery: Ajax',
            'active' => 0
        ]     
    ];    

    public function __construct()
    {
        $this->name = 'anproductvideogallery';
        $this->tab = 'others';
        $this->version = '1.0.0';
        $this->author = 'Anvanto';
        $this->need_instance = 0;

        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Product video Gallery');
        $this->description = $this->l('For themes by Anvanto');

        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
    }

    /**
     * @return bool
     */

    public function install()
    {
        $sql = include _PS_MODULE_DIR_ . $this->name . '/sql/install.php';
        foreach ($sql as $_sql) {
            Db::getInstance()->Execute($_sql);
        } 
            
        foreach ($this->_tabs as $tab) {
            $this->tabAdd($tab, $this);
        }

        $this->installDemoContent();

        return parent::install()
            && $this->registerHook('displayHeader')
            && $this->registerHook('displayAnvantoProductThumbsList');
    }

    public function uninstall()
    {
        $sql = include _PS_MODULE_DIR_ . $this->name .  '/sql/uninstall.php';
        foreach ($sql as $_sql) {
            Db::getInstance()->Execute($_sql);
        }     
        
        foreach ($this->_tabs as $tab) {
            $this->tabDelete($tab);
        }     
        
        return parent::uninstall();
    }

    /**
     * @return bool
     */

    public function renderWidget($hookName, array $params)
    {
        if (!isset($params['product']['id_product'])){
            return;
        }

        $this->context->smarty->assign(['widgets' => anProductVideosGallery::getVideos($params['product']['id_product'])]);

        if (isset($params['type']) && $params['type'] == 'thumb'){
            return $this->fetch($this->getTemplatePath('views/templates/front/thumb.tpl'));
        }
        if (isset($params['type']) && $params['type'] == 'image'){
            return $this->fetch($this->getTemplatePath('views/templates/front/image.tpl'));
        }
    }

    public function getWidgetVariables($hookName, array $params)
    {            
        return;
    }
    
    public function getContent()
    {
       Tools::redirectAdmin($this->context->link->getAdminLink('AdminProductvideoGallery'));
    }

    public function hookDisplayHeader()
    { 
        $this->context->controller->registerStylesheet(
            $this->name . "_css",
            'modules/' . $this->name . '/views/css/front.css',
            ['server' => 'local', 'priority' => 150]
        );
           
        $this->context->controller->registerJavascript(
            $this->name . "_js",
            'modules/' . $this->name . '/views/js/front.js',
            ['server' => 'local', 'priority' => 150]
        );
    }

    public function installDemoContent()
    {
        $languages = Language::getLanguages(false);
        $video = new anProductVideosGallery();

        foreach ($languages as $lang) {  
            $video->title[$lang['id_lang']] = 'Social Feed Slider';
            $video->youtube[$lang['id_lang']] = 'https://www.youtube.com/watch?v=OQxlex-68ZA';
        }

        $video->save();
        $video->updateCategoriesProducts([], $video->id, 0);
    }

    public function tabAdd($tab, $module)
    {
        if (count($tab) == 0){
            return false;
        }

        $languages = Language::getLanguages();
        
        $_tab = new Tab();
        $_tab->active = $tab['active'];
        $_tab->class_name = $tab['class_name'];
        $_tab->id_parent = Tab::getIdFromClassName($tab['parent']);
        if (empty($_tab->id_parent)) {
            $_tab->id_parent = 0;
        }

        $_tab->module = $module->name;
        foreach ($languages as $language) {
            $_tab->name[$language['id_lang']] = $module->l($tab['name']);
        }

        $_tab->add();

        return true;
    }

    public function tabDelete($tab)
    {
        if (count($tab) == 0){
            return false;
        }        
        
        $_tab_id = Tab::getIdFromClassName($tab['class_name']);
        $_tab = new Tab($_tab_id);
        $_tab->delete();

        return true;
    }
    
    public function topPromo()
    {        
        $this->context->smarty->assign('theme', $this->getThemeInfo());
        return $this->display(__FILE__, 'views/templates/admin/top.tpl');
    }
    
    public function getThemeInfo()
    {
        $theme = [];
        $themeFileJson = _PS_THEME_DIR_.'/config/theme.json';
        if (Tools::file_exists_no_cache($themeFileJson)) {
            $theme = (array)json_decode(Tools::file_get_contents($themeFileJson), 1);            
        }

        if (!isset($theme['url_contact_us']) || $theme['url_contact_us'] == ''){
            
            $urlContactUs = 'https://addons.prestashop.com/contact-form.php';

            if (isset($theme['addons_id']) && $theme['addons_id'] != ''){
                $urlContactUs .= '?id_product=' .$theme['addons_id'];
            } elseif (isset($this->url_contact_us) && $this->url_contact_us != ''){
                $urlContactUs = $this->url_contact_us;
            } elseif (isset($this->addons_product_id) && $this->addons_product_id != ''){
                $urlContactUs .= '?id_product=' .$this->addons_product_id;
            }
            
            $theme['url_contact_us'] = $urlContactUs;
        }
        
        if (!isset($theme['url_rate']) || $theme['url_rate'] == ''){
            
            $urlRate = 'https://addons.prestashop.com/ratings.php';

            if (isset($theme['addons_id']) && $theme['addons_id'] != ''){
                $urlRate .= '?id_product=' .$theme['addons_id'];
            } elseif (isset($this->url_rate) && $this->url_rate != ''){
                $urlRate = $this->url_rate;
            } elseif (isset($this->addons_product_id) && $this->addons_product_id != ''){
                $urlRate .= '?id_product=' .$this->addons_product_id;
            }
            
            $theme['url_rate'] = $urlRate;
        }        
        return $theme;
    }
}
