<?php

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

class Post_Time extends Item{

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

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

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

        static function get_icon(){
            return 'time';
        }
        
        static function render_content($settings){
            
            // get format
            $format = empty($settings['format'])? get_option('time_format', 'g:i a'): $settings['format'];
            if( $format == 'custom' ){
                $format = empty($settings['custom-format'])? '': $settings['custom-format'];
            }

            // get date and post id
            $date = get_the_time($format);
            $post_id = get_the_ID();

            // default admin value
            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;
                    $date = get_the_time($format, $post_id);
                }else{
                    $date = current_time($format);
                }
            }

            // data attributes
            $tag = 'span';
            $atts = array();
            if( !empty($settings['typo']['preset']) ){
                $atts['typo-preset'] = $settings['typo']['preset'];
            }

            // href
            if( !empty($settings['date-link']['link-to']) ){
                $tag = 'a';
                $href = '';
                if( !empty($post_id) ){
                    if( $settings['date-link']['link-to'] == 'day' ){
                        $href = get_day_link(get_the_time('Y', $post_id), get_the_time('m', $post_id), get_the_time('d', $post_id));
                    }else if( $settings['date-link']['link-to'] == 'month' ){
                        $href = get_month_link(get_the_time('Y', $post_id), get_the_time('m', $post_id));
                    }else if( $settings['date-link']['link-to'] == 'year' ){
                        $href = get_year_link(get_the_time('Y', $post_id));
                    } 
                }
            }

            // print
            $ret = '';
            if( !empty($date) ){
                $ret .= '<div class="pixe-detail" >';

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

                // left icon
                if( !empty($icon) && (empty($settings['icon']['position']) || $settings['icon']['position'] == 'left') ){
                    $ret .= $icon;
                }
                
                // date
                $ret .= '<' . $tag . ' class="pixe-inline" ';
                $ret .= empty($href)? '': 'href="' . esc_attr($href) . '" ';
                $ret .= pixe_get_data_atts($atts);  
                $ret .= '>' . $date . '</' . $tag . '>';

                // 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_Time');
    