<?php
class ControllerCommonConfigbar extends Controller {

	private $configbar = array(
		'begin'   => false ,
		'setup'   => false ,
		'email'   => false ,
		'product' => false ,
		'payment' => false ,
		'shipping'=> false ,
		'domain'  => false ,
		'end'			=> false
	) ;

	public function index() {

		$this->data['token'] = $this->session->data['token'] ;

		$quick_config = $this->config->get('quick_config');
    $userName = $this->user->getUserName();
    $userID = $this->user->getId();

		if (isset($quick_config) && $userName != 'futura') {
      $this->data['exibe_quick_configure'] = (!isset($quick_config[$userID]) || (isset($quick_config[$userID]) && ($quick_config[$userID]['status'] == 1 && strtotime($quick_config[$userID]['date']) < strtotime(date('Y-m-d'))))) ? 1 : 0;
    } else {
      $this->data['exibe_quick_configure'] = 0;
    }

		$this->data['configbars'] = $this->getDadosConfigBar() ;
		$this->response->setOutput($this->render('common/configbar'));
	}

	protected function getDadosConfigBar(){

		$this->getLinks();
		$this->getLanguage();
		$configbars= $this->getConfigBar();

		//valida se a barra devera ou n aparecer na home
		$arrayBar = array(
			'display'=> $this->getDisplay($configbars)
		);

		foreach ($configbars as $key => $value) {
			$arrayBar['configbar'][$key]['status']    =  $value ;
			$arrayBar['configbar'][$key]['title']     = (isset($this->data[$key.'_title']))     ? $this->data[$key.'_title']     : ''    ;
			$arrayBar['configbar'][$key]['text']      = (isset($this->data[$key.'_text']))      ? $this->data[$key.'_text']      : ''    ;
			$arrayBar['configbar'][$key]['link']      = (isset($this->data[$key.'_link']))      ? $this->data[$key.'_link']      : false ;
			$arrayBar['configbar'][$key]['text_link'] = (isset($this->data[$key.'_text_link'])) ? $this->data[$key.'_text_link'] : false ;
		}

		return $arrayBar ;
	}

	protected function getDisplay($array){
		$return = true ;
		if(count(array_filter($array)) == (count($array)-1) ){
			$return = true ;
		}

		if($array['domain']){
			$return = false;
		}

		return $return;
	}

	protected function getLinks(){
		$this->load->type('tipocomum');
		$this->data['email_link']    =  $this->url->link('setting/setting',  'token='. $this->session->data['token'].'&tabs=email', 'SSL');
		// $this->data['product_link']  =  $this->url->link('catalog/product',  'token='. $this->session->data['token'], 'SSL');
		$this->data['product_link']  =   'http://manual.futurasistemas.com.br/manual-do-sistema/e-commerce/futura-e-commerce/';
		$this->data['payment_link']  =  $this->url->link('extension/module', 'token='. $this->session->data['token'].'&module='.Type_TitleModules::Tp_Pagamento, 'SSL');
		$this->data['shipping_link'] =  $this->url->link('extension/module', 'token='. $this->session->data['token'].'&module='.Type_TitleModules::Tp_Envio, 'SSL');
		$this->data['domain_link']   =  $this->url->link('setting/setting',  'token='. $this->session->data['token'].'&tabs=server', 'SSL');
	}

	protected function getLanguage(){
		$language = $this->load->language('common/configbar');
    $this->data = array_merge($this->data, $language);
	}

	protected function getConfigBar() {

		$this->configbar = array(
			'begin'   => true									,
			'setup'   => $this->getSetup()		,
			'email'   => $this->getEmail()    ,
			'product' => $this->getProduct()  ,
			'payment' => $this->getPayment()  ,
			'shipping'=> $this->getShipping() ,
			'domain'  => $this->getDomain()   ,
			'end'     => false
		) ;

		arsort ($this->configbar);
		return $this->configbar;
	}

	protected function getSetup() {
			$setup = array (
				'config_title' 				 => $this->config->get('config_title'),
				'config_logo' 				 => $this->config->get('config_logo'),
				'primary_color' 			 => $this->config->get('primary_color'),
				'secondary_color' 		 => $this->config->get('secondary_color'),
				'config_telephone' 		 => $this->config->get('config_telephone'),
				'config_email' 				 => $this->config->get('config_email'),
				'config_owner' 				 => $this->config->get('config_owner'),
				'config_name' 				 => $this->config->get('config_name'),
				'config_cnpj' 				 => $this->config->get('config_cnpj'),
				'config_ie' 					 => $this->config->get('config_ie'),
				'config_address' 			 => $this->config->get('config_address'),
				'config_email'				 => $this->getEmail()
		);
		return $this->getReturn($setup);
	}

	protected function getProduct() {
		$return = false ;
		$sql = "select "
				  ."(select category_id from ".DB_PREFIX."category limit 0,1) as category "
					."from ".DB_PREFIX."product p where p.image <> '' limit 0,1";
		$query = $this->db->query($sql);

		if($query->rows){
			$return = true ;
		}
		return $return;
	}

	protected function getEmail()	{
		$this->load->helper('ftr_geral');
		$email = array(
			'config_smtp_host' 		 => $this->config->get('config_smtp_host'),
			'config_smtp_username' => $this->config->get('config_smtp_username'),
			'config_smtp_password' => $this->config->get('config_smtp_password'),
			'config_smtp_port' 		 => $this->config->get('config_smtp_port'),
			'config_alert_mail' 	 => ftr_geral::isIsset($this->config->get('config_alert_mail')) ? true : false,
			'config_account_mail'  => ftr_geral::isIsset($this->config->get('config_account_mail')) ? true : false
		);
		return $this->getReturn($email);
	}

	protected function getPayment()	{
		$return = false ;
		$this->load->model('setting/extension');
		$results = $this->model_setting_extension->getExtensions('payment');
		foreach ($results as $result) {
			if ($this->config->get($result['code'] . '_status')) {
							$return = true ;
			}
		}
		return $return ;
	}

	protected function getShipping()	{
		$return = false ;
		$this->load->model('setting/extension');
		$results = $this->model_setting_extension->getExtensions('shipping');
		foreach ($results as $result) {
			if ($this->config->get($result['code'] . '_status')) {
							$return = true ;
			}
		}
		return $return ;
	}

	protected function getDomain()	{
		$domain = array (
			'config_habilita_linkproducao'  => $this->config->get('config_habilita_linkproducao')
		);
		return $this->getReturn($domain);
	}

	protected function getReturn($array){
		$return = false ;
		if(count($array) == count(array_filter($array))){
			$return = true;
		}
		return $return;
	}


}
