<?php

class Wpadverts_Ci
{
    /**
     * Wpjb_Grayareatechsolutions Singleton
     * 
     * @var Wpadverts_Ci
     */
    protected static $_instance = null;

    /**
     * Path to wpadverts-category-icons (eg. /public_html/wp-content/plugins/wpadverts-category-icons)
     *
     * @var string
     */
    protected $_basedir = null;

    /**
     * URL to wpadverts-category-icons (eg. http://example.com/wp-content/plugins/wpadverts-category-icons)
     *
     * @var string
     */
    protected $_baseurl = null;

    
    protected $_admin = null;

    /**
     * Object contructor
     * 
     * Protected constructor allows to create only one instance of this class.
     * 
     * @since 1.0
     * @return Wpadverts_Ci
     */
    protected function __construct() {

        $this->_basedir = dirname( dirname( __FILE__ ) );
        $this->_baseurl = plugin_dir_url( dirname( __FILE__ ) );

        if(is_admin()) {
            include $this->_basedir . '/includes/class-wpadverts-ci-admin.php';
            $this->_admin = new Wpadverts_Ci_Admin();
        }
        
        
        add_action( 'init', array( $this, 'init' ));
    }

    /**
     * Singleton
     * 
     * Creates if NULL and returns Wpadverts_Ci instance
     * 
     * @since 1.0
     * @return Wpadverts_Ci
     */
    public static function get_instance() {
        if( self::$_instance === null ) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    /**
     * Returns basedir path
     * 
     * @since 1.0
     * @return string   Basedir path (eg. /public_html/wp-content/plugins/wpadverts-category-icons)
     */
    public function get_basedir() {
        return $this->_basedir;
    }

    /**
     * Returns plugin base URL
     * 
     * @since 1.0
     * @return string   Basedir path (eg. http://example.com/wp-content/plugins/wpadverts-category-icons)
     */
    public function get_baseurl() {
        return $this->_baseurl;
    }


    /**
     * General Init Function
     * 
     * This function is executed in both frontend and wp-admin
     * 
     * @since 1.0
     * @return void
     */
    public function init() {

        load_plugin_textdomain("wpadverts-ci", false, $this->_basedir . "/languages/");
        
        wp_register_script(
            'wpadverts-ci',
            Wpadverts_Ci::get_instance()->get_baseurl() . '/assets/js/wpadverts-ci.js',
            array( 'jquery' ),
            '1.0',
            true  
        );

        wp_localize_script( 'wpadverts-ci', 'wpadverts_ci_lang', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' )
        ));


        wp_register_style(
            'wpadverts-ci',
            $this->_baseurl . '/assets/css/wpadverts-ci.css',
            array(),
            '1.0'
        );
        
        wp_enqueue_style('wpadverts-ci');
        
        add_filter( 'wpadverts_mal_pins_localize', array( &$this, 'wpadverts_mal_pins_localize') );
        add_filter( 'adverts_category_font_icon', array( &$this, 'adverts_category_font_icon' ), 10, 3 );
        add_action( 'adverts_category_pre_title', array( &$this, 'adverts_category_pre_title' ), 10, 2 );
        add_action( 'wp_footer', array( &$this, 'my_adverts_category_pre_title_footer' ) );
        
    }
    
    public function wpadverts_mal_pins_localize($localize) {
        
        for($i = 0 ; $i < count($localize) ; $i++) {
            
            $term = wp_get_post_terms( $localize[$i]['advert_id'], 'advert_category' );
            
            if(isset($term[0]) && !empty($term[0])) {
                $t_id = $term[0]->term_id;
                //$term_meta = get_option( "advert_category_$t_id" ); 
                $img_url = esc_attr( get_term_meta( $t_id, 'advert_category_small_icon', true) ) ? esc_attr( get_term_meta( $t_id, 'advert_category_small_icon', true) ) : null; 
                
                if($img_url == '') {
                    $term_meta = get_option( "advert_category_$t_id" ); 
                    $img_url = esc_attr( $term_meta['image_small'] ) ? esc_attr( $term_meta['image_small'] ) : ''; 
                }
                
                if($img_url != null) {
                    //$localize[$i]['label'] = '<img class="map-icon map-icon-img" src="'.$img_url.'" />';
                    $localize[$i]['label'] = '<span class="map-icon"> <img class="map-icon-img" src="'.$img_url.'" /></span>';
                    $localize[$i]['label'] = '"><img class="map-icon-img" src="'.$img_url.'" /><span class="';
                }
            }
        }
        
        return $localize;
    }
    
    public function adverts_category_font_icon($icon, $term_item, $size) {

        $t_id = $term_item->term_id;
        //$term_meta = get_option( "advert_category_$t_id" ); 
        $img_url = esc_attr( get_term_meta( $t_id, 'advert_category_'.$size.'_icon', true) ) ? esc_attr( get_term_meta( $t_id, 'advert_category_'.$size.'_icon', true) ) : null; 

        if($img_url == '') {
            $term_meta = get_option( "advert_category_$t_id" ); 
            $img_url = esc_attr( $term_meta['image_'.$size] ) ? esc_attr( $term_meta['image_'.$size] ) : ''; 
        }
        
        if($img_url != null) {
            $icon = "my-adverts-category-icon-none";
        } 
        
        return $icon;
    }
    
    public function adverts_category_pre_title($term_item, $size) {
        
        $t_id = $term_item->term_id;
        //$term_meta = get_option( "advert_category_$t_id" ); 
        $img_url = esc_attr( get_term_meta( $t_id, 'advert_category_'.$size.'_icon', true) ) ? esc_attr( get_term_meta( $t_id, 'advert_category_'.$size.'_icon', true) ) : null; 
        if($img_url == '') {
            $term_meta = get_option( "advert_category_$t_id" ); 
            $img_url = esc_attr( $term_meta['image_'.$size] ) ? esc_attr( $term_meta['image_'.$size] ) : ''; 
        }
        
        
        if($img_url != null) {
            if($size == "small") {
                ?> <img class="my-adverts-category-icon my-adverts-max-size" src="<?php echo $img_url; ?>" /> <?php
            } else {
                ?> 
                    <div class="my-adverts-category-icon-big">
                        <img class="my-adverts-category-icon" src="<?php echo $img_url; ?>" />
                    </div>          
                <?php
            }
        }
        
    }
    
    public function my_adverts_category_pre_title_footer() {
    ?>
        <style type="text/css">
        .adverts-flexbox-icon.my-adverts-category-icon-none {
            display: none !important;
            
        }
        .my-adverts-category-icon {
            border:0;
            margin:0px .2em;
            padding:0;
            box-shadow: none !important;
            vertical-align: text-top;
        }
        .my-adverts-category-icon-big {
            display: block;
            color: #DF3539;
            height: 80px;
            line-height: 80px;
        }
        .my-adverts-category-icon-big .my-adverts-category-icon {
            vertical-align: middle;
        }
        
        img.my-adverts-max-size {
            max-width: 14px;
            max-height: 14px;
        }
        </style>
    <?php
    }

}