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

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

require_once _PS_MODULE_DIR_ . 'an_sizeguide/classes/anStatisticsSizeGuide.php';

class an_sizeguideajaxModuleFrontController extends ModuleFrontController
{    
    public function initContent()
    {
        $result = [];
        if (Tools::isSubmit('action')) {

            if (Tools::getValue('token') != Tools::getToken(false)){
                Tools::redirect('index.php?controller=404');
            }

            $actionName = Tools::getValue('action', '') . 'Action';
            if (method_exists($this, $actionName)) {
                $result = $this->$actionName();
            }
        }

        die(json_encode($result));
    }    
    
    public function getSizeGuideAction()
    {
        $return['html'] = AnSizeGuideWidgets::getSizeGuideById(Tools::getValue('id_widget')); 
         
        if (Configuration::get(an_sizeguide::PREFIX . 'recording_statistics')){
            anStatisticsSizeGuide::saveRecordingStatistics(Tools::getValue('id_widget'));
        }
        
        return $return;
    }    
}