<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

/**
 * Module Name: Mosaic
 */
class TB_Mosaic_Module extends Themify_Builder_Component_Module {


	public static function get_module_name():string{
		add_filter( 'themify_builder_active_vars', [ __CLASS__, 'builder_active_enqueue' ] );
		return __('Mosaic', 'builder-mosaic');
	}
    
	public static function get_json_file():array{
		return ['f'=>Builder_Mosaic::$url . 'json/style.json','v'=>Builder_Mosaic::get_version()];
	}

	public static function get_module_icon():string{
		themify_get_icon('layout-column3','ti');
		themify_get_icon('angle-down','ti');
	    return 'view-grid';
	}

	public static function get_js_css():array {
		$url=Builder_Mosaic::$url . 'assets/';
		return array(
			'async'=>true,
			'css' => $url . 'styles',
			'js' => $url . 'scripts',
			'ver'=>Builder_Mosaic::get_version()
		);
	}

	public static function pagination_links( $limit, $total ) {
		$paged = ! empty( $_GET['builder-mosaic'] ) ? (int) $_GET['builder-mosaic'] : 1;
		$max_page = ceil( $total / $limit );
		return $max_page>0 && method_exists(__CLASS__,'get_pagination')?self::get_pagination('','','builder-mosaic',0,$max_page,$paged):'';
	}

	public static function builder_active_enqueue(array $vars ):array {
		if(!isset($vars['addons'])){//backward
			themify_enque_script( 'tb_builder-mosaic', Builder_Mosaic::$url . 'assets/active.js', Builder_Mosaic::get_version(), [ 'themify-builder-app-js' ] );
		}
		else{
			$vars['addons'][Builder_Mosaic::$url . 'assets/active.js']=Builder_Mosaic::get_version();
		}

		$i18n = include dirname( __DIR__ )  . '/includes/i18n.php';
		$vars['i18n']['label']+= $i18n;
		$providers=Builder_Mosaic_Provider::get_providers();
		$vars['mosaic_vars'] = [
			'url' => Builder_Mosaic::$url,
			'jqui' => includes_url( 'js/jquery/ui/' ),
			'custom_presets' => Builder_Mosaic::get_custom_presets(),
			'v' =>  Builder_Mosaic::get_version(),
			'abspath' => trailingslashit( ABSPATH ),
			'active' => []
		];
		if(!empty($providers['ptb'])){
			$vars['mosaic_vars']['ptb_settings']=$providers['ptb']::get_options();
			$vars['mosaic_vars']['active']['ptb']=1;
		}
		if(!empty($providers['tep'])){
			$vars['mosaic_vars']['active']['tep']=1;
		}
		if(!empty($providers['portfolio'])){
			$vars['mosaic_vars']['active']['portfolio']=1;
		}
		if(!empty($providers['nextgen'])){
			$vars['mosaic_vars']['active']['nextgen']=1;
		}
		if(!empty($providers['wc'])){
			$vars['mosaic_vars']['active']['wc']=1;
		}
		return $vars;
	}

	public static function get_static_content(array $module):string {
		if (isset($module['mod_settings']['tiled_posts_display'])) {
			
			if($module['mod_settings']['tiled_posts_display']==='text'){
				return parent::get_static_content($module);
			}
			if($module['mod_settings']['tiled_posts_display']==='gallery'){
				$text =!empty($module['mod_settings']['mod_title']) ? sprintf('<h3>%s</h3>', $module['mod_settings']['mod_title']) : '';
				if (isset($module['mod_settings']['shortcode_gallery'])) {
					$text.=$module['mod_settings']['shortcode_gallery'];
				}
				return $text;
			}
		}
		return ''; // no static content for dynamic content
	}

	public function __construct() {
            if(method_exists('Themify_Builder_Model', 'add_module')){
                parent::__construct('mosaic');
            }
            else{//backward
                 parent::__construct(array(
                    'name' =>$this->get_name(),
                    'slug' => 'mosaic',
                    'category' =>$this->get_group()
                ));
            }
	}

    public function get_name(){
		return self::get_module_name();
    }

    public function get_icon(){
		return self::get_module_icon();
    }

    function get_assets() {
		return self::get_js_css();
    }

	public function get_styling() {
		$general = array(
			// Background
			self::get_expand('bg', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_color('', 'background_color', 'bg_c', 'background-color')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_color('', 'bg_c', 'bg_c', 'background-color', 'h')
				    )
				)
			    ))
			)),
			self::get_expand('f', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_font_family(),
					self::get_color(array('', ' .tbm_title', ' .tbm_caption', ' .tbm_caption p'),'font_color'),
					self::get_font_size(),
					self::get_font_style( '', 'f_fs_g', 'f_fw_g' ),
					self::get_line_height(),
					self::get_text_align(array('', ' .tbm_title', ' .tbm_caption')),
					self::get_text_shadow(),
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_font_family('','f_f','h'),
					self::get_color(array('', ' .tbm_title', ' .tbm_caption', ' .tbm_caption p'),'f_c',null,null,'h'),
					self::get_font_size('','f_s','f_w','','h'),
					self::get_font_style( '', 'f_fs_g', 'f_fw_g', 'h' ),
					self::get_text_shadow('','t_sh','h'),
				    )
				)
			    ))
			)),
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding()
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding('', 'p', 'h')
				    )
				)
			    ))
			)),
			// Margin
			self::get_expand('m', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_margin()
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_margin('', 'm', 'h'),
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border()
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border('', 'b', 'h')
				    )
				)
			    ))
			)),
				// Height & Min Height
				self::get_expand('ht', array(
						self::get_height(),
						self::get_min_height(),
						self::get_max_height()
					)
				),
			// Rounded Corners
			self::get_expand('r_c', array(
					self::get_tab(array(
						'n' => array(
							'options' => array(
								self::get_border_radius()
							)
						),
						'h' => array(
							'options' => array(
								self::get_border_radius('', 'r_c', 'h')
							)
						)
					))
				)
			),
			// Shadow
			self::get_expand('sh', array(
					self::get_tab(array(
						'n' => array(
							'options' => array(
								self::get_box_shadow()
							)
						),
						'h' => array(
							'options' => array(
								self::get_box_shadow('', 'sh', 'h')
							)
						)
					))
				)
			),
		);

		$mosaic_title = array(
			// Font
			self::get_expand('f', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_font_family('.module figure .tbm_title' ,'f_f_m_t'),
					self::get_color( '.module figure .tbm_title' ,'f_c_m_t'),
					self::get_font_size('.module figure .tbm_title', 'f_s_m_t'),
					self::get_line_height('.module figure .tbm_title', 'l_h_m_t'),
					self::get_letter_spacing('.module figure .tbm_title', 'l_s_m_t'),
					self::get_text_align('.module figure .tbm_title', 't_a_m_t'),
					self::get_text_transform('.module figure .tbm_title', 't_t_m_t'),
					self::get_font_style('.module figure .tbm_title', 'f_sy_m_t','f_t_b'),
					self::get_text_shadow('.module figure .tbm_title' ,'t_sh_t'),
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_font_family('.module figure .tbm_title' ,'f_f_m_t','h'),
					self::get_color( '.module figure .tbm_title' ,'f_c_m_t',null,null,'h'),
					self::get_font_size('.module figure .tbm_title', 'f_s_m_t','','h'),
					self::get_font_style('.module figure .tbm_title', 'f_sy_m_t','f_t_b','h'),
					self::get_text_shadow('.module figure .tbm_title' ,'t_sh_t','h'),
				    )
				)
			    ))
			)),
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding('.module figure .tbm_title','m_t_p')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding('.module figure .tbm_title','m_t_p','h')
				    )
				)
			    ))
			)),
			// Margin
			self::get_expand('m', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_margin('.module figure .tbm_title','m_t_m')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_margin('.module figure .tbm_title','m_t_m','h')
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border('.module figure .tbm_title','m_t_b')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border('.module figure .tbm_title','m_t_b','h')
				    )
				)
			    ))
			))
		);

		$mosaic_caption = array(
			self::get_expand('bg', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_color(' figure .tbm_caption', 'b_c_cn','bg_c','background-color')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_color(' figure .tbm_caption', 'b_c_cn','bg_c','background-color','h')
				    )
				)
			    ))
			)),
			// Font
			self::get_expand('f', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_font_family(' figure .tbm_caption' ,'f_f_m_cn'),
					self::get_color(' figure .tbm_caption' ,'f_c_m_cn'),
					self::get_font_size(' figure .tbm_caption', 'f_s_m_cn'),
					self::get_line_height(' figure .tbm_caption', 'l_h_m_cn'),
					self::get_letter_spacing(' figure .tbm_caption', 'l_s_m_cn'),
					self::get_text_align(' figure .tbm_caption', 't_a_m_cn'),
					self::get_text_transform(' figure .tbm_caption', 't_t_m_cn'),
					self::get_font_style(' figure .tbm_caption', 'f_sy_m_cn','f_cn_b'),
					self::get_text_shadow(' figure .tbm_caption' ,'t_sh_c'),
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_font_family(' figure .tbm_caption' ,'f_f_m_cn','h'),
					self::get_color(' figure .tbm_caption' ,'f_c_m_cn',null,null,'h'),
					self::get_font_size(' figure .tbm_caption', 'f_s_m_cn','','h'),
					self::get_font_style(' figure .tbm_caption', 'f_sy_m_cn','f_cn_b','h'),
					self::get_text_shadow(' figure .tbm_caption' ,'t_sh_c','h'),
				    )
				)
			    ))
			)),
			// Font
			self::get_expand('l', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_color(' figure .tbm_caption a', 'l_c_m_cn'),
					self::get_text_decoration(' figure .tbm_caption a')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_color('  figure .tbm_caption a', 'l_c_hv_m_cn',null,null,'h'),
					self::get_text_decoration('  figure .tbm_caption a','t_a','h')
				    )
				)
			    ))
			)),
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding(' figure .tbm_caption','m_cn_p')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding(' figure .tbm_caption','m_cn_p','h')
				    )
				)
			    ))
			)),
			// Margin
			self::get_expand('m', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_margin(' figure .tbm_caption','m_cn_m'),
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_margin(' figure .tbm_caption','m_cn_m','h'),
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border(' figure .tbm_caption','m_cn_b')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border(' figure .tbm_caption','m_cn_b','h')
				    )
				)
			    ))
			))
		);

		$mosaic_overlay = array(
			//bacground
			self::get_expand('bg', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_image('.module figure::before','background_image','b_c_ov')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_image('.module figure:hover::before', 'b_i_hv_ov','b_c_hv_ov','background-image')
				    )
				)
			    ))
			)),
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding('.module figure figcaption','m_ov_p')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding('.module figure figcaption','m_ov_p','h')
				    )
				)
			    ))
			)),
			// Margin
			self::get_expand('m', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_margin('.module figure::before','m_ov_m')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_margin('.module figure::before','m_ov_m','h')
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border('.module figure::before','m_ov_b')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border('.module figure::before','m_ov_b','h')
				    )
				)
			    ))
			))
		);

		$mosaic_tile_container = array(
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding(' .mosaic-container','m_tc_p')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding(' .mosaic-container','m_tc_p','h')
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border(' .mosaic-container','m_tc_b')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border(' .mosaic-container','m_tc_b','h')
				    )
				)
			    ))
			))
		);
		
		$mosaic_pagination = array(
			self::get_expand(__( 'Load More', 'builder-mosaic' ), array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_color(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'b_c_pg','bg_c','background-color')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_color(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'b_c_pg','bg_c','background-color','h')
				    )
				)
			    ))
			)),
			// Font
			self::get_expand('l', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_font_family(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'f_f_m_pg'),
					self::get_color(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'f_c_m_pg'),
					self::get_font_size(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'f_s_m_pg'),
					self::get_line_height(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'l_h_m_pg'),
					self::get_letter_spacing(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'l_s_m_pg'),
					self::get_text_align(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 't_a_m_pg'),
					self::get_text_transform(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 't_t_m_pg'),
					self::get_font_style(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'f_sy_m_pg','f_pg_b'),
					self::get_text_shadow(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'t_sh_p'),
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_font_family(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'f_f_m_pg','h'),
					self::get_color(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'f_c_m_pg',null,null,'h'),
					self::get_font_size(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'f_s_m_pg','','h'),
					self::get_font_style(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'), 'f_sy_m_pg','f_pg_b','h'),
					self::get_text_shadow(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'t_sh_p','h'),
				    )
				)
			    ))
			)),
			// Padding
			self::get_expand('p', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_padding(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'m_pg_p')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_padding(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'m_pg_p','h')
				    )
				)
			    ))
			)),
			// Border
			self::get_expand('b', array(
			    self::get_tab(array(
				'n' => array(
				    'options' => array(
					self::get_border(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'m_pg_b')
				    )
				),
				'h' => array(
				    'options' => array(
					self::get_border(array(' .tbm_wrap_more .tbm_more', ' .pagenav a', ' .pagenav span'),'m_pg_b','h')
				    )
				)
			    ))
			))
		);

		return array(	'type' => 'tabs',
				'options' => array(
					'g' => array(
						'options' => $general
					),
					'm_t_c' => array(
						'label' => __('Tile Container', 'builder-mosaic'),
						'options' => $mosaic_tile_container
					),
					'm_t' => array(
						'label' => __('Title', 'builder-mosaic'),
						'options' => $mosaic_title
					),
					'm_c' => array(
						'label' => __('Caption', 'builder-mosaic'),
						'options' => $mosaic_caption
					),
					'm_o' => array(
						'label' => __('Overlay', 'builder-mosaic'),
						'options' => $mosaic_overlay
					),
					'm_p' => array(
						'label' => __('Pagination', 'builder-mosaic'),
						'options' => $mosaic_pagination
					)
				)
		);
	}
}

if(!method_exists( 'Themify_Builder_Component_Module', 'get_module_class' )){
	if ( method_exists( 'Themify_Builder_Model', 'add_module' ) ) {
		new TB_Mosaic_Module();
	} else {
		Themify_Builder_Model::register_module( 'TB_Mosaic_Module' );
	}
}