<?php
namespace App\Controllers;

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 *
 * @package CodeIgniter
 */

use CodeIgniter\Controller;

class FrontController extends Controller
{

	/**
	 * An array of helpers to be loaded automatically upon
	 * class instantiation. These helpers will be available
	 * to all other controllers that extend BaseController.
	 *
	 * @var array
	 */
	protected $helpers = ['project','form','url'];

	/* Project Custom settings */
	protected $project; 
	
	/* Admin Model */
	protected $amObj;
	
	/* Bussiness logic model */
	public $cmObj;
	
	/* Current module menu_id */
	protected $menu_id;
	
	/* Store user roles */
	protected $user_roles;
	
	// Router
	protected $router;
	
	// Request
	public $request;
	
	// Validation
	public $validation;
	
	// current Language Code
	public $language_code;
	/**
	 * Constructor.
	 */
	public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
	{
		// Do Not Edit This Line
		parent::initController($request, $response, $logger);

		//--------------------------------------------------------------------
		// Preload any models, libraries, etc, here.
		//--------------------------------------------------------------------
		// E.g.:
		$this->session = \Config\Services::session();		 
		$this->project = config( 'Config\\Project' );
		$this->router = \Config\Services::router();
		$this->validation = \Config\Services::validation();
		// Admin Model
		//$this->amObj = model('App\Models\Admin\AdminModel');
		// Common Model
		$this->cmObj = new \App\Models\Front\CommonModel();
		$this->session->set('show_enq_modal',false);
	}

	// custom methods
	protected function view_inner_template($content_file="",$data=array()){
		$this->set_default_data($data);
		$this->set_required_class_obj($data);
		$data['prod_cat_menu'] = $this->cmObj->get_prod_cat_menu($data['language_code']);
		
		//$data['menu_html'] = $this->get_header_menu_html($data['menu_tree']);
		$html =  view('front/template/inner/html_start',$data);
		$html .= view('front/template/inner/header',$data);
		$html .= '<!--begin::Content-->';
		if(is_array($content_file)){
			foreach($content_file as $file_name){
				$html .= view('front/'.$file_name,$data);
			}
		}else{
			$html .= view('front/'.$content_file,$data);
		}
		$html .= '<!--end::Content-->';
		$html .= view('front/template/inner/footer',$data);
		$html .= view('front/template/inner/html_end',$data);
		return $html;
	}
	
	protected function view_email_template($content_file="",$data=array()){
		$this->set_default_data($data);
		$this->set_required_class_obj($data);
		$html =  view('email/header',$data);
		$html .= '<!--begin::Content-->';
		if(is_array($content_file)){
			foreach($content_file as $file_name){
				$html .= view('email/'.$file_name,$data);
			}
		}else{
			$html .= view('email/'.$content_file,$data);
		}
		$html .= '<!--end::Content-->';
		$html .= view('email/footer',$data);
		return $html;
	}
	
	protected function view_partial($content_file="",$data=array()){
		$this->set_default_data($data);
		$this->set_required_class_obj($data);
		$html = '';
		if(is_array($content_file)){
			foreach($content_file as $file_name){
				$html .= view('front/'.$data['language_code'].'/'.$file_name,$data);
			}
		}else{
			$html .= view('front/'.$data['language_code'].'/'.$content_file,$data);
		}
		return $html;
	}
	
	private function set_default_data(&$data=array()){
		// Set Meta content 
		$data['meta_data']['title'] = $data['meta_data']['title'] ?? $this->project->default_meta_title;
		$data['meta_data']['description'] = $data['meta_data']['description'] ?? $this->project->default_meta_description;
		$data['meta_data']['keywords'] = $data['meta_data']['keywords'] ?? $this->project->default_meta_keywords;
		// Set Plugins
		$data['plugins'] = $data['plugins'] ?? [];

		$data['page'] = $data['page'] ?? '';
		$data['controller_name'] = $data['controller_name'] ?? '';		
		// Set Language Code
		$data['language_code'] = $data['language_code'] ?? $this->project->default_lang_code;
		$this->language_code = $data['language_code'];
		return $data;
	}
	
	private function set_required_class_obj(&$data){
		$data['project'] = $this->project;
		$data['cmObj'] = $this->cmObj;
		$data['session'] = $this->session;
		$data['router'] = $this->router;
		$data['fcObj'] = $this;
		$data['validation'] = \Config\Services::validation();
	}
	
	private function get_header_menu_html($menu_tree,$html=''){
		foreach($menu_tree as $menu_dtl){
			if(count($menu_dtl['children']) > 0){
				$html .= '<li class="nav-item">'."\n";
				$html .= '<a class="nav-link" href="javascript:;">'.$menu_dtl['title'].' <i class="fa fa-caret-down"></i></a>'."\n";
				$html .= '<ul class="has-sub">'."\n";
				$html .= $this->get_header_sub_menu_html($menu_dtl['children']);
				$html .= '</ul></li>'."\n";
			}else{
				$url = trim($menu_dtl['url'])!="" ? $menu_dtl['url'] : 'javascript:;'; 
				$target = $menu_dtl['open_in_new_tab']=="Yes" ? 'target="_blank"' : '';
				$html .= '<li class="nav-item"><a class="nav-link" href="'.$url.'" '.$target.'>'.$menu_dtl['title'].'</a></li>'."\n";
			}
		}
		return $html;
	}
	
	private function get_header_sub_menu_html($menu_tree, $html=''){
		foreach($menu_tree as $menu_dtl){
			if(count($menu_dtl['children']) > 0){
				$html .= '<li>'."\n";
				$html .= '<a href="javascript:;">'.$menu_dtl['title'].' <i class="fa fa-caret-down"></i></a>'."\n";
				$html .= '<ul class="has-sub">'."\n";
				$html .= $this->get_header_sub_menu_html($menu_dtl['children']);
				$html .= '</ul></li>'."\n";
			}else{
				$url = trim($menu_dtl['url'])!="" ? $menu_dtl['url'] : 'javascript:;';
				$target = $menu_dtl['open_in_new_tab']=="Yes" ? 'target="_blank"' : '';
				$html .= '<li><a href="'.$url.'" '.$target.'>'.$menu_dtl['title'].'</a></li>'."\n";
			}
		}
		return $html;
	}
	
	protected function validate_user(){
		if(is_null($this->session->get($this->project->session_front_user_id))){			
			return false;
		}
		return true;
	}
	
	protected function get_customer_id(){
		return $this->session->get($this->project->session_front_user_id);
	}
	
	protected function show_error(){
        echo '<h4>Invalid Request!</h4>';
    }
	
	protected function get_requested_lang_code(){
		$req_lang_code = $this->request->uri->getSegment(1);
		if($this->cmObj->is_language_supported($req_lang_code)){
			return strtolower($req_lang_code);
		}
		return $this->project->default_lang_code;
	}
	
	public function anchor($link,$title_arr,$extra_attr_str=""){
		$link_lang_code = $this->project->language_cnt > 1 ? $this->language_code : '';
		if($link!=""){
			$url = base_url();
			$url .= $link_lang_code!='' ? $link_lang_code.'/' : '';
			$url .= $link;
		}else{
			$url = 'javascript:;';
		}
		$html = '<a href="'.$url.'" '.$extra_attr_str.'>';
		$html .= $title_arr[$this->language_code];
		$html .= '</a>';
		return $html;
	}
	
	public function lang_url($link){
		$link_lang_code = $this->project->language_cnt > 1 ? $this->language_code : '';
		$url = base_url();
		$url .= $link_lang_code!='' ? $link_lang_code.'/' : '';
		$url .= $link;
		return $url;
	}
	
	public function lang_lbl($lbl_arr){
		return $lbl_arr[$this->language_code];
	}
}