<?php
/**
* 2022 Anvanto
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
*
*  @author    Anvanto <anvantoco@gmail.com>
*  @copyright 2022 Anvanto
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*/

if (!defined('_PS_VERSION_')) {
    exit;
}
//
class an_theme_dashboard extends Module
{
    const PREFIX = "an_td_";

    public $dataDir;
    public $modules;
    public $recommended;
	
    public function __construct()
    {
        $this->name = 'an_theme_dashboard';
        $this->tab = 'front_office_features';
        $this->version = '1.0.8';
        $this->author = 'anvanto';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
        $this->bootstrap = true;
        $this->module_key = '';

        parent::__construct();

        $this->displayName = $this->l('Anvanto Theme Dashboard');
        $this->description = $this->l('Anvanto Theme Dashboard');

        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
		
		$this->dataDir = _PS_MODULE_DIR_.'an_theme_dashboard/data/';
		$this->modules = $this->loadConfigFileIfExists('modules');
		$this->recommended = $this->loadConfigFileIfExists('recommended');
    }
	
    protected function loadConfigFileIfExists($sourceName)
    {
        if (Tools::file_exists_no_cache(_PS_MODULE_DIR_.'an_theme_dashboard/'.$sourceName.'.php')) {
            return include(_PS_MODULE_DIR_.'an_theme_dashboard/'.$sourceName.'.php');
        }

        return array();
    }	

    public function install()
    {
        $this->tabAdd([
            'class_name' => 'AdminAnThemedashboard',
            'parent' => 'IMPROVE',
            'name' => 'Anvanto Theme',
            'displayTab' => true,
            'icon' => 'dashboard',
            'active' => 1
        ], $this);

        return parent::install();
    }

    public function uninstall()
    {
        $this->tabDelete(['class_name'=>'AdminAnThemedashboard']);
        return parent::uninstall();
    }

    //////////////
    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 ($tab['icon']){
            $_tab->icon = $tab['icon'];
        }
        
        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;
    }    
}