<?php

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

    class Post_Title extends Item{

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

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

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

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

        static function item_class($settings){
            return 'pixe-heading';
        }

        static function render_content($settings){
            global $pixe_query;

            $settings['link'] = empty($settings['link'])? array(): $settings['link'];
            $title = '';
            $link = '';
            if( in_the_loop() || !empty($pixe_query->in_the_loop) ){
                $title = get_the_title();
                $link_tag = pixe_get_link_tag($settings['link'], array(
                    'class' => 'pixe-inline',
                    'default-link-to' => 'query'
                ));

            // get title depending on the type
            }else if( is_main_query() ){
                
                if( is_home() ){
                    $title = esc_html__('Home', 'pixebuilder');
                }else if( is_page() ){
                    $title = get_the_title();
                }else if( is_single() ){
                    $title = get_the_title();
                }else if( is_404() ){
                    $title = esc_html__('404', 'pixebuilder');
                }else if( is_search() ){
                    $title = get_search_query();

                // archive pages
                }else if( is_archive() ){
                    if( is_day() ){
                        $title = get_the_date('F j, Y');
                    }else if( is_month() ){
                        $title = get_the_date('F Y');
                    }else if( is_year() ){
                        $title = get_the_date('F Y');
                    }else if( is_author() ){
                        $author = get_queried_object();
                        $title = $author->display_name;	
                    }else if( is_tax() || is_category() || is_tag() ){
                        $term = get_queried_object();
                        $title = $term->name;
                    }
                }else{
                    $title = get_the_title();
                }
                $link_tag = pixe_get_link_tag($settings['link'], array(
                    'class' => 'pixe-inline',
                    'default-link-to' => 'query'
                ));
            }

            $ret = '';
            if( !empty($title) ){

                // icon
                $icon_data = empty($settings['icon'])? '': $settings['icon'];
                $icon = empty($icon_data['icon'])? '': pixe_get_icon($icon_data['icon'], array(
                    'icon-class' => 'pixe-inline-icon' . pixe_get_responsive_class($icon_data, 'icon-hide-on')
                ));
                $icon_position = empty($icon_data['position'])? 'left': $icon_data['position'];

                // get variables
                $tag = empty($settings['text-tag'])? 'h3': $settings['text-tag'];

                $atts = array();
                if( !empty($settings['text-typo']['preset']) ){
                    $atts['typo-preset'] = $settings['text-typo']['preset'];
                }

                // print
                $ret  = '<' . $tag . ' class="pixe-text' . (empty($link_tag)? ' pixe-inline': '') . '" ';
                $ret .= pixe_get_data_atts($atts);
                $ret .= '>';
                $ret .= $link_tag;
                $ret .= $icon && in_array($icon_position, array('left', 'left-block', 'left-stretch'))? $icon: '';
                $ret .= $title;
                $ret .= $icon && in_array($icon_position, array('right', 'right-block', 'right-stretch'))? $icon: '';
                $ret .= empty($link_tag)? '': '</a>';
                $ret .= '</' . $tag . '>'; 
            }

            return $ret;
        }

    }

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