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

		$this->load->helper('ftr_geral');

		$this->language->load('extension/total');

		$this->document->setTitle($this->language->get('heading_title'));

   		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_news'] = $this->language->get('text_news');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_confirm'] = $this->language->get('text_confirm');

		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		if (isset($this->session->data['error'])) {
			$this->data['error'] = $this->session->data['error'];

			unset($this->session->data['error']);
		} else {
			$this->data['error'] = '';
		}

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

		$extensions = $this->model_setting_extension->getInstalled('total');

		foreach ($extensions as $key => $value) {
			if (!file_exists(DIR_APPLICATION . 'controller/total/' . $value . '.php')) {
				$this->model_setting_extension->uninstall('total', $value);

				unset($extensions[$key]);
			}
		}

		$this->data['extensions'] = array();
		$files = glob(DIR_APPLICATION . 'controller/total/*.php');
		if ($files) {
			foreach ($files as $file) {
				$extension = basename($file, '.php');

				$this->language->load('total/' . $extension);

				$action = array();

				if (!in_array($extension, $extensions)) {
					$action[] = array(
						'text' => $this->language->get('text_install'),
						'href' => $this->url->link('extension/total/install', 'token=' . $this->session->data['token'] . '&extension=' . $extension, 'SSL')
					);
				} else {
					$action[] = array(
						'text' => $this->language->get('text_edit'),
						'href' => $this->url->link('total/' . $extension . '', 'token=' . $this->session->data['token'], 'SSL')
					);

					$action[] = array(
						'text' => $this->language->get('text_uninstall'),
						'href' => $this->url->link('extension/total/uninstall', 'token=' . $this->session->data['token'] . '&extension=' . $extension, 'SSL')
					);
				}

				$this->data['extensions'][] = array(
					'name'       => $this->language->get('heading_title'),
					'status'     => $this->config->get($extension . '_status') ? $this->language->get('text_enabled') : $this->language->get('text_disabled'),
					'sort_order' => $this->config->get($extension . '_sort_order'),
					'action'     => $action,
					'news'   => ftr_geral::getNovidadeModulo($this->language->get($extension.'_data_new'))
				);
			}
		}

		usort(
			$this->data['extensions'],
			 function( $a, $b ) {
				 if( $a['news'] == $b['news'] ) return 0;
				 return ( ( $a['news'] > $b['news'] ) ? -1 : 1 );
			 }
		);

		$this->template = 'extension/total.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);

		$this->response->setOutput($this->render());
	}

	public function install() {
		$this->language->load('extension/total');

		if (!$this->user->hasPermission('modify', 'extension/total')) {
			$this->session->data['error'] = $this->language->get('error_permission');

			$this->redirect($this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL'));
		} else {
			$this->load->model('setting/extension');

            switch ($this->request->get['extension']) {
                case 'sub_total';
                case 'total' : $subtype = Type_ExtensionSubType::Tp_Totalizador; break;
                case 'shipping' : $subtype = Type_ExtensionSubType::Tp_TotalizadorFrete; break;
                case in_array($this->request->get['extension'],
                                ['coupon','descontoporpagamento','descontoprimeiracompra',
                                'descontofaixavalorpedido','low_order_fee','descontoacrescimokit','yapay','wirecard',
                                'descontoautomatico'] ) :
                                 $subtype = Type_ExtensionSubType::Tp_TotalizadorAcrescimoDesconto; break;
                default :
                    $subtype = Type_ExtensionSubType::Tp_Default; break;

            }

			$this->model_setting_extension->install('total', $this->request->get['extension'],$subtype);

			$this->load->model('user/user_group');

			$this->model_user_user_group->addPermission($this->user->getId(), 'access', 'total/' . $this->request->get['extension']);
			$this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'total/' . $this->request->get['extension']);

			require_once(DIR_APPLICATION . 'controller/total/' . $this->request->get['extension'] . '.php');

			$class = 'ControllerTotal' . str_replace('_', '', $this->request->get['extension']);
			$class = new $class($this->registry);

			if (method_exists($class, 'install')) {
				$class->install();
			}

			$this->redirect($this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}

	public function uninstall() {
		$this->language->load('extension/total');
		$this->load->type('tipocomum');
		$modulos = Type_ModulosObrigatorios::getEnumList();

		if (!$this->user->hasPermission('modify', 'extension/total') || (in_array($this->request->get['extension'], $modulos))  ) {

			if(in_array($this->request->get['extension'], $modulos)){
					$this->session->data['error'] = $this->language->get('error_permission_module');
			}else{
					$this->session->data['error'] = $this->language->get('error_permission');
			}

			$this->redirect($this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL'));
		} else {
			$this->load->model('setting/extension');
			$this->load->model('setting/setting');

			$this->model_setting_extension->uninstall('total', $this->request->get['extension']);

			$this->model_setting_setting->deleteSetting($this->request->get['extension']);

			require_once(DIR_APPLICATION . 'controller/total/' . $this->request->get['extension'] . '.php');

			$class = 'ControllerTotal' . str_replace('_', '', $this->request->get['extension']);
			$class = new $class($this->registry);

			if (method_exists($class, 'uninstall')) {
				$class->uninstall();
			}

			$this->redirect($this->url->link('extension/total', 'token=' . $this->session->data['token'], 'SSL'));
		}
	}
}
