<?php
class ControllerExtensionModuleNetreviewsApi extends Controller {
    
    public function index () {


        $this->load->model('extension/module/netreviews');

        
        $this->language->load('extension/module/netreviews');
        $this->document->setTitle($this->language->get('heading_title'));
        
        $data['post_data'] = $this->request->post;

        // Check if there is any post data
       if (!isset($data['post_data']) OR empty($data['post_data'])) {
            
            $data['reponse']['debug'] = "Aucunes variables POST reçues";
            $data['reponse']['return'] = 2; //A definir
            $data['afficherapi']= $this->model_extension_module_netreviews->AV_encode_base64(json_encode($data['reponse']));
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/module/netreviews/api.twig')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/extension/module/netreviews/api', $data));
            } else {
            $this->response->setOutput($this->load->view('extension/module/netreviews/api', $data));
            }
            // Do not continue, exit stage left
            return;
       }
        
        if (isset($data['post_data']['message'])) {
            $data['message'] = json_decode($this->model_extension_module_netreviews->AV_decode_base64($data['post_data']['message']), true);
        }
    

        // Check if the module is active
        $data['isActiveReply'] = $this->model_extension_module_netreviews->isActiveModule($data['post_data'],$data['message']['idWebsite']);
        if($data['isActiveReply']['return'] != 1) {
            $data['afficherapi']= $this->model_extension_module_netreviews->AV_encode_base64(json_encode($data['isActiveReply']));
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/module/netreviews/api.twig')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/extension/module/netreviews/api', $data));
            } else {
            $this->response->setOutput($this->load->view('extension/module/netreviews/api', $data));
            }
            // Do not continue, exit stage left
            return;
        }
        //Check if the user has provided good credentials
        $data['checkReply'] = $this->model_extension_module_netreviews->checkData($data['message'],$data['post_data']['query']);


        if ($data['checkReply']['return'] != 1) {
            $data['afficherapi']= $this->model_extension_module_netreviews->AV_encode_base64(json_encode($data['checkReply']));
            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/module/netreviews/api.twig')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/extension/module/netreviews/api', $data));
            } else {
            $this->response->setOutput($this->load->view('extension/module/netreviews/api', $data));
            }
            // Do not continue, exit stage left
            return;
        }



//        if (!isset($data['post_data']['query']))
//            $data['post_data']['query'] = ' pas de query';
         $data['reply'] = '';
         if (isset($data['post_data']['query'])){
        
            switch ($data['post_data']['query']) {
                // Already checked that the idWebsite and Secret Key match for each store that has been configured
                case 'isActiveModule':
                    $data['reply'] = array(
                        'debug' => 'Module Installé et activé',
                        'return' => 1,//Module OK
                        'query' => $data['post_data']['query']
                    );
                    break;
                case 'setModuleConfiguration':
                    $data['reply'] = $this->model_extension_module_netreviews->setModuleConfiguration($data['message']);
                    break;
                case 'getModuleAndSiteConfiguration':
                    $data['reply'] = $this->model_extension_module_netreviews->getModuleAndSiteConfiguration($data['message']);
                    break;
                case 'getOrders':
                    $data['reply'] = $this->model_extension_module_netreviews->getOrders($data['message'],$data['post_data']['query']);
                    break;
                /*case 'getUrlProducts'; // pas encore actif.
                    $data['reply'] = "Opencart 3 - Tagjs pour l'affichage les avis produits"
                    break;*/
                case 'setProductsReviews':
                    $data['reply'] = "Opencart 3 - Tagjs pour l'affichage les avis produits";
                    break;
                case 'truncateTables':
                    $data['reply'] = "Opencart 3 - Tagjs pour l'affichage les avis produits";
                    break;
                default:
                      break;
            }
        }
        
        $data['afficherapi'] =  $this->model_extension_module_netreviews->AV_encode_base64(json_encode($data['reply']));       
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/extension/module/netreviews/api.tpl')) {
            $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/extension/module/netreviews/api', $data));
        } else {
            $this->response->setOutput($this->load->view('extension/module/netreviews/api', $data));
        }


    }
}
?>