<?php

    namespace Pixebuilder\Element;
    
    // prevent direct file access
	if( !defined( 'ABSPATH' ) ) exit;

    class Post_Categories extends Item{

        static function get_type(){
            return 'post';
        }

        static function get_name(){
            return 'post-categories';
        }

        static function get_title(){
            return esc_html__('Post Categories', 'pixebuilder');
        }

        static function get_icon(){
            return 'category';
        }

        static function get_term_list($post_id, $taxonomy, $before = '', $sep = '', $after = '', $limit = 999, $link_to_archive = true ){
            $terms = get_the_terms($post_id, $taxonomy);
            if( is_wp_error( $terms ) || empty($terms) ){
                return '';
            }

            $links = array();
            foreach( $terms as $term ) {
                $link = get_term_link($term, $taxonomy);
                if( is_wp_error($link) ){ continue; }

                if( $link_to_archive ){
                    $links[] = '<a href="' . esc_url($link) . '" rel="tag" >' . $term->name . '</a>';
                }else{
                    $links[] = '<span>' . $term->name . '</span>';
                }
                
                if( count($links) >= $limit ) break;
            }

            return $before . implode($sep, $links) . $after;
        }

        static function get_term_list_preview( $before = '', $sep = '', $after = '', $limit = 2, $link_to_archive = true ){
            $links = array();
            for( $i = 1; $i <= $limit; $i++ ){
                if( $link_to_archive ){
                    $links[] = sprintf('<a href="#" >Cat %d</a>', $i);
                }else{
                    $links[] = sprintf('<span>Cat %d</span>', $i);
                }
            }

            return $before . implode($sep, $links) . $after;
        }
        
        static function render_content($settings){

            // post id
            $post_id = get_the_ID();
            if( empty($post_id) && pixe_is_admin() ){
                global $pixe_request_type, $pixe_request_id;
                if( $pixe_request_type == 'post' && !empty($pixe_request_id) ){
                    $post_id = $pixe_request_id;
                }
            }

            // get variable
            $taxonomy = empty($settings['taxonomy']['taxonomy'])? 'category': $settings['taxonomy']['taxonomy'];
            $archive_link = empty($settings['link']['link-to'])? true: false;
            $separator_type = empty($settings['separator']['type'])? '': $settings['separator']['type'];

            // $terms
            $limit = empty($settings['taxonomy']['limit'])? 999: intval($settings['taxonomy']['limit']);
            if( empty($separator_type) ){
                $term_list = self::get_term_list($post_id, $taxonomy, '<span class="pixe-inline" >', ', ' , '</span>', $limit, $archive_link);
            }else if($separator_type == 'dot' || $separator_type == 'divider'){
                $term_list = self::get_term_list($post_id, $taxonomy, '', '<span class="pixe-sep pixe-' . $separator_type . '" ></span>' , '', $limit, $archive_link);
            }else{
                $term_list = self::get_term_list($post_id, $taxonomy, '', '' , '', $limit, $archive_link);
            }

            // default admin term style 
            if( pixe_is_admin() && empty($term_list) ){
                $limit = empty($settings['taxonomy']['limit'])? 2: intval($settings['taxonomy']['limit']);
                if( empty($separator_type) ){
                    $term_list = self::get_term_list_preview('<span class="pixe-inline" >', ', ' , '</span>', $limit, $archive_link);
                }else if($separator_type == 'dot' || $separator_type == 'divider'){
                    $term_list = self::get_term_list_preview('', '<span class="pixe-sep pixe-' . $separator_type . '" ></span>' , '', $limit, $archive_link);
                }else{
                    $term_list = self::get_term_list_preview('', '' , '', $limit, $archive_link);
                }
            }
            
            // atts
            $atts = array();
            if( !empty($settings['typo']['preset']) ){
                $atts['typo-preset'] = $settings['typo']['preset'];
            }

            // print
            $ret = '';
            if( !empty($term_list) ){

                // icon
                if( empty($settings['icon']['icon']) ){
                    $icon = pixe_icon_preset('tag');
                }else{
                    $icon = pixe_get_icon($settings['icon']['icon']);
                }

                $ret .= '<div class="pixe-detail" ';
                $ret .= pixe_get_data_atts($atts);
                $ret .= ' >';

                // left icon
                if( !empty($icon) && (empty($settings['icon']['position']) || $settings['icon']['position'] == 'left') ){
                    $ret .= $icon;
                }

                $ret .= $term_list;

                // right icon
                if( !empty($icon) && !empty($settings['icon']['position']) && $settings['icon']['position'] == 'right' ){
                    $ret .= $icon;
                }

                $ret .= '</div>';
            }

            return $ret;
        }

    }

    \Pixe_Builder::register_element('Pixebuilder\Element\Post_Categories');
    