<?php
namespace Webien\Elementor\Widgets\Breadcrumbs;

class Widget extends \Elementor\Widget_Base
{
    protected $breadcrumbs = [];
    protected $breadcrumbs_collapsed = [];

    public function get_name()
    {
        return 'webien_breadcrumbs';
    }

    public function get_title()
    {
        return __('Webien breadcrumbs', 'webien');
    }

    public function get_icon()
    {
        return 'eicon-date';
    }

    public function get_categories()
    {
        return ['webien'];
    }

    protected function is_dynamic_content(): bool {
        return true;
    }


    protected function register_controls()
    {

        $this->start_controls_section(
            'section_content',
            [
                'label' => __('Allmänt', 'webien'),
            ]
        );


        $this->add_control(
            'content_type',
            [
                'label' => __('Breadcrumbs baserat på', 'webien'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'post_structure' => __('Sidstruktur', 'webien'),
                    'taxonomy' => __('Taxanomi', 'webien'),
                    'custom' => __('Manuellt urval', 'webien'),
                ],
                'label_block' => true,
                'default' => 'post_structure',
            ]
        );

        $taxonomies = get_taxonomies([], 'objects');

        foreach ( $taxonomies as $taxonomy ) {
            if($taxonomy->public){
                $options[$taxonomy->name] = $taxonomy->label;
            }
        }

        $this->add_control(
            'taxonomy',
            [
                'label' => __( 'Välj taxanomi', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => $options,
                'classes' => 'taxonomy',
                'condition' => [
                    'content_type' => 'taxonomy',
                ],
            ]
        );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control(
            'custom_selection_type',
            [
                'label' => __( 'Välj typ', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'multiple' => false,
                'options' => [
                    'post' => __('Sida från posttyp', 'webien'),
                    'posttype' => __('Posttyparkiv', 'webien'),
                    'custom' => __('Manuell länk', 'webien'),
                ],
                'default' => 'post',
                'label_block' => true,
            ]
        );

        $repeater->add_control(
            'custom_selection',
            [
                'label' => __( 'Sök efter sida från alla posttyper', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => false,
                'options' => $this->getPostSelection(),
                'default' => [ '' ],
                'label_block' => true,
                'condition' => [
                    'custom_selection_type' => 'post',
                ],
            ]
        );

        $repeater->add_control(
            'custom_selection_archive',
            [
                'label' => __( 'Välj posttypsarkiv', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'multiple' => false,
                'options' => $this->getArchiveSelection(),
                'default' => '',
                'label_block' => true,
                'condition' => [
                    'custom_selection_type' => 'posttype',
                ],
            ]
        );

        $repeater->add_control(
            'custom_selection_archive_label',
            [
                'label'   => __('Egen länk text', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'condition' => [
                    'custom_selection_type' => 'posttype',
                ],
            ]
        );

        $repeater->add_control(
            'custom_selection_custom_link',
            [
                'label'   => __('Egen länk', 'webien'),
                'description' => __('Använd relativ länk om det är en sida på denna site. Ex: /medarbetare', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'condition' => [
                    'custom_selection_type' => 'custom',
                ],
            ]

        );

        $repeater->add_control(
            'custom_selection_custom_label',
            [
                'label'   => __('Egen länk text', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'condition' => [
                    'custom_selection_type' => 'custom',
                ],
            ]

        );

        $this->add_control(
            'custom_selections',
            [
                'label' => __( '', 'elementor' ),
                'type' => \Elementor\Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'condition' => [
                    'content_type' => 'custom',
                ],
            ]
        );



        $this->add_control(
            'show_home',
            [
                'label'     => __( 'Visa hem' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no'
            ]
        );
        $this->add_control(
            'home_text',
            [
                'label'   => __('Text för hem', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __('Hem', 'webien'),
                'condition' => [
                    'show_home' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'custom_home_type',
            [
                'label' => __( 'Hämlänk', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'start' => __('Startsida', 'webien'),
                    'post' => __('Välj sida', 'webien'),
                    'custom' => __('Egen url', 'webien'),
                ],
                'label_block' => true,
                'default' => 'start',
                'condition' => [
                    'show_home' => 'yes',
                ],
            ]
        );
        $this->add_control(
            'custom_home_post',
            [
                'label' => __( 'Sök efter sida', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'multiple' => false,
                'options' => $this->getPostSelection(),
                'default' => [ '' ],
                'label_block' => true,
                'condition' => [
                    'custom_home_type' => 'post',
                ],
            ]
        );

        $this->add_control(
            'custom_home_custom',
            [
                'label'   => __('Egen länk', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => '',
                'placeholder' => __( 'https://www...', 'webien' ),
                'label_block' => true,
                'condition' => [
                    'custom_home_type' => 'custom',
                ],
            ]
        );

        $this->add_control(
            'show_post_type_home',
            [
                'label'     => __( 'Visa posttyp arkiv' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no',
                'condition' => [
                    //'content_type' => ['taxonomy','post_structure'],
                ],
            ]
        );

        $this->add_control(
            'show_current',
            [
                'label'     => __( 'Visa nuvarande' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no',
                'separator' => 'before',
            ]
        );

        $this->add_control(
            'collapse_mobile',
            [
                'label'     => __( 'Visa endast nivån över på mobil' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'yes'
            ]
        );

        $this->add_control(
            'limit_crumbs',
            [
                'label'     => __( 'Limitera antal crumbs' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'default'   => 'no'
            ]
        );

        $this->add_control(
            'limit_crumbs_num',
            [
                'label'     => __( 'Antal crumbs att visa' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::NUMBER,
                'default'   => '',
                'condition' => [
                    'limit_crumbs' => 'yes',
                ],
            ]
        );



        $this->end_controls_section();






        $this->start_controls_section(
            'section_separator',
            [
                'label' => __( 'Avskiljare', 'elementor' ),
            ]
        );

        $this->add_control(
            'icon_separator_text',
            [
                'label'   => __('Tecken för avskiljare', 'webien'),
                'type'    => \Elementor\Controls_Manager::TEXT,
                'default' => __('', 'webien'),
            ]
        );

        $this->add_control(
            'icon_separator',
            [
                'label' => __( 'Ikon', 'webien' ),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fal fa-angle-right',
                    'library' => 'solid',
                ],
            ]
        );

        $this->add_control(
            'icon_separator_mobile',
            [
                'label' => __( 'Ikon för tillbaka på mobil', 'webien' ),
                'type' => \Elementor\Controls_Manager::ICONS,
                'default' => [
                    'value' => 'fal fa-angle-left',
                    'library' => 'solid',
                ],
                'condition' => [
                    'collapse_mobile' => 'yes',
                ],
            ]
        );

        $this->end_controls_section();





        // ----------------------------//
        // Styles

        $this->start_controls_section(
            'section_item_style',
            [
                'label' 	=> __( 'Crumbs', 'webien' ),
                'tab' 		=> \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'items_align',
            [
                'label' 		=> __( 'Align Crumbs', 'webien' ),
                'type' 			=> \Elementor\Controls_Manager::CHOOSE,
                'default' 		=> 'start',
                'options' 		=> [
                    'start' 			=> [
                        'title' 	=> __( 'Left', 'webien' ),
                        'icon' 		=> 'eicon-h-align-left',
                    ],
                    'center' 		=> [
                        'title' 	=> __( 'Center', 'webien' ),
                        'icon' 		=> 'eicon-h-align-center',
                    ],
                    'end' 		=> [
                        'title' 	=> __( 'Right', 'webien' ),
                        'icon' 		=> 'eicon-h-align-right',
                    ],
                    'space-between' => [
                        'title' 	=> __( 'Space between', 'webien' ),
                        'icon' 		=> 'eicon-h-align-stretch',
                    ],
                    'space-around' => [
                        'title' 	=> __( 'Space around', 'webien' ),
                        'icon' 		=> 'eicon-h-align-stretch',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs' => 'justify-content: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'items_text_align',
            [
                'label' 		=> __( 'Align Text', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::CHOOSE,
                'default' 		=> '',
                'options' 		=> [
                    'left' 			=> [
                        'title' 	=> __( 'Left', 'elementor-extras' ),
                        'icon' 		=> 'fa fa-align-left',
                    ],
                    'center' 		=> [
                        'title' 	=> __( 'Center', 'elementor-extras' ),
                        'icon' 		=> 'fa fa-align-center',
                    ],
                    'right' 		=> [
                        'title' 	=> __( 'Right', 'elementor-extras' ),
                        'icon' 		=> 'fa fa-align-right',
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'item_spacing',
            [
                'label' 	=> __( 'Spacing', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::SLIDER,
                'default'	=> [
                    'size'	=> 12
                ],
                'range' 	=> [
                    'px' 	=> [
                        'max' => 36,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs' => 'margin-left: -{{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .breadcrumbs li.item' => 'margin-left: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .breadcrumbs li.separator' => 'margin-left: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'item_padding',
            [
                'label' 		=> __( 'Padding', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' 	=> [ 'px', 'em', '%' ],
                'selectors' 	=> [
                    '{{WRAPPER}} .breadcrumbs li.item' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'allowed_dimensions' => [ 'right', 'left' ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' 		=> 'item_border',
                'label' 	=> __( 'Border', 'elementor-extras' ),
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.item',
            ]
        );

        $this->add_control(
            'item_border_radius',
            [
                'label' 		=> __( 'Border Radius', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' 	=> [ 'px', '%' ],
                'selectors' 	=> [
                    '{{WRAPPER}} .breadcrumbs li.item' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' 		=> 'item_typography',
                'label' 	=> __( 'Typography', 'elementor-extras' ),
                'global' => [
                    'default' => \Elementor\Core\Kits\Documents\Tabs\Global_Typography::TYPOGRAPHY_TEXT,
                ],
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.item a',
            ]
        );

        $this->start_controls_tabs( 'crumb_style' );

        $this->start_controls_tab( 'crumb_default', [ 'label' => __( 'Default', 'elementor-extras' ) ] );

        $this->add_control(
            'item_background_color',
            [
                'label' 	=> __( 'Background Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs li.item' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'item_color',
            [
                'label' 	=> __( 'Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'global' => [
                    'default' => \Elementor\Core\Kits\Documents\Tabs\Global_Colors::COLOR_ACCENT,
                ],
                'selectors' => [
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.item' => 'color: {{VALUE}} !important;',
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.item a' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab( 'crumb_hover', [ 'label' => __( 'Hover', 'elementor-extras' ) ] );

        $this->add_control(
            'item_background_color_hover',
            [
                'label' 	=> __( 'Background Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs li.item:hover' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'item_color_hover',
            [
                'label' 	=> __( 'Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'default'	=> '',
                'selectors' => [
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.item:hover' => 'color: {{VALUE}}  !important;',
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.item:hover a' => 'color: {{VALUE}}  !important;',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        $this->start_controls_section(
            'section_separator_style',
            [
                'label' 	=> __( 'Separators', 'elementor-extras' ),
                'tab' 		=> \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'separator_padding',
            [
                'label' 		=> __( 'Padding', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' 	=> [ 'px', 'em', '%' ],
                'selectors' 	=> [
                    '{{WRAPPER}} .breadcrumbs li.separator' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
                'allowed_dimensions' => [ 'right', 'left' ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' 		=> 'separator_border',
                'label' 	=> __( 'Border', 'elementor-extras' ),
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.separator',
            ]
        );

        $this->add_control(
            'separator_border_radius',
            [
                'label' 		=> __( 'Border Radius', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' 	=> [ 'px', '%' ],
                'selectors' 	=> [
                    '{{WRAPPER}} .breadcrumbs li.separator' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'separator_background_color',
            [
                'label' 	=> __( 'Background Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs li.separator' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'separator_color',
            [
                'label' 	=> __( 'Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'default'	=> '',
                'selectors' => [
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.separator' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' 		=> 'separator_typography',
                'label' 	=> __( 'Typography', 'elementor-extras' ),
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.separator',
            ]
        );

        $this->end_controls_section();

        $this->start_controls_section(
            'section_current_style',
            [
                'label' 	=> __( 'Current', 'elementor-extras' ),
                'tab' 		=> \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Border::get_type(),
            [
                'name' 		=> 'current_border',
                'label' 	=> __( 'Border', 'elementor-extras' ),
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.current',
            ]
        );

        $this->add_control(
            'current_border_radius',
            [
                'label' 		=> __( 'Border Radius', 'elementor-extras' ),
                'type' 			=> \Elementor\Controls_Manager::DIMENSIONS,
                'size_units' 	=> [ 'px', '%' ],
                'selectors' 	=> [
                    '{{WRAPPER}} .breadcrumbs li.current' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'current_background_color',
            [
                'label' 	=> __( 'Background Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .breadcrumbs li.current' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'current_color',
            [
                'label' 	=> __( 'Color', 'elementor-extras' ),
                'type' 		=> \Elementor\Controls_Manager::COLOR,
                'global' => [
                    'default' => \Elementor\Core\Kits\Documents\Tabs\Global_Colors::COLOR_TEXT,
                ],
                'selectors' => [
                    '{{WRAPPER}} #breadcrumbs.breadcrumbs li.current a' => 'color: {{VALUE}} !important;',
                ],
            ]
        );

        $this->add_group_control(
            \Elementor\Group_Control_Typography::get_type(),
            [
                'name' 		=> 'current_typography',
                'label' 	=> __( 'Typography', 'elementor-extras' ),
                'selector' 	=> '{{WRAPPER}} .breadcrumbs li.current a',
            ]
        );

        $this->end_controls_section();
    }


    protected function render()
    {
        $settings = $this->get_settings_for_display();


        if($settings['content_type'] === 'post_structure'){
            $this->get_page_breadcrumbs();
        }
        if($settings['content_type'] === 'taxonomy'){
            $this->get_tax_breadcrumbs($settings['taxonomy']);
        }
        if($settings['content_type'] === 'custom'){
            $this->get_custom_breadcrumbs();
        }
        
        $breadcrumbs = $this->breadcrumbs;
        $breadcrumbs_collapsed = $this->breadcrumbs_collapsed;


        // If limit is set, cut array
        if($settings['limit_crumbs'] === 'yes'){
            $breadcrumbs = array_splice($breadcrumbs, intval($settings['limit_crumbs_num'])*-1 );
            $breadcrumbs[0]->separator = false;
        }

        // Get JSON-LD schema markup for breadcrumbs
        echo $this->getSchemaMarkup($breadcrumbs);


        ob_start();
        include('view.php');
        $html = ob_get_clean();

        echo $html;

    }


    function get_tax_breadcrumbs($tax)
    {
        global $post;
        $settings = $this->get_settings_for_display();

        if(is_single()){
            $terms = get_the_terms($post->ID, $tax);
            if(isset($terms[0])){
                $term = $terms[0];

                if($settings['show_current'] === 'yes'){
                    $this->breadcrumbs[] = $this->add_crumb($post->post_title, get_permalink($post->ID), 'current');
                }
                $this->breadcrumbs[] = $this->add_crumb($term->name, get_term_link($term, $tax));
            }
        } else {
            $term = get_term(get_queried_object()->term_id, $tax);
            if($settings['show_current']){
                $this->breadcrumbs[] = $this->add_crumb($term->name, get_term_link($term, $tax), 'current');
            }
        }



        // get all parents
        $x = 0;
        while($x <= 99) {
            if(isset($term->parent) && $term->parent !== 0){
                $term = get_term($term->parent, $tax);
                $this->breadcrumbs[] = $this->add_crumb($term->name, get_term_link($term, $tax));
            } else {
                break;
            }
            $x++;
        }


        if($settings['show_post_type_home'] === 'yes'){
            $post_type = get_post_type_object($post->post_type);
            $name = $post_type->label;
            $link = get_post_type_archive_link($post->post_type);

            if($link){ // Not all post types have archives
                $this->breadcrumbs[] = $this->add_crumb($name, $link);
            }
        }


        if($settings['show_home'] === 'yes'){
            $type = $settings['custom_home_type'];
            $home_link = get_home_url();
            $home_link = $type === 'post' ? get_permalink($settings['custom_home_post']) : $home_link;
            $home_link = $type === 'custom' ? $settings['custom_home_custom'] : $home_link;
            $this->breadcrumbs[] = $this->add_crumb( __($settings['home_text'], 'webien'), $home_link, '', true);
        }

        // Collapsed breadcrumbs
        $collapsed = $this->breadcrumbs;
        if($settings['show_current'] === 'yes'){
            unset($collapsed[0]);
            $collapsed = array_values($collapsed);
        }
        if(!empty($collapsed)){
            $this->breadcrumbs_collapsed[] = $collapsed[0];
        }


        $this->breadcrumbs = array_reverse($this->breadcrumbs);

        // Remove first separator
        $this->breadcrumbs[0]->separator = false;

    }



    function get_page_breadcrumbs()
    {
        global $post;
        $settings = $this->get_settings_for_display();

        if($settings['show_current']){
            $this->breadcrumbs[] = $this->add_crumb($post->post_title, get_permalink($post->ID), 'current');
        }

        // get all parents
        $x = 0;
        while($x <= 99) {
            if(isset($post->post_parent) && $post->post_parent !== 0){
                $post = get_post($post->post_parent);
                $this->breadcrumbs[] = $this->add_crumb($post->post_title, get_permalink($post->ID));
            } else {
                break;
            }
            $x++;
        }
        wp_reset_postdata();


        if($settings['show_post_type_home'] === 'yes'){
            $post_type = get_post_type_object($post->post_type);
            $name = $post_type->label;
            $link = get_post_type_archive_link($post->post_type);

            if($link){ // Not all post types have archives
                $this->breadcrumbs[] = $this->add_crumb($name, $link);
            }
        }

        if($settings['show_home'] === 'yes'){
            $type = $settings['custom_home_type'];
            $home_link = get_home_url();
            $home_link = $type === 'post' ? get_permalink($settings['custom_home_post']) : $home_link;
            $home_link = $type === 'custom' ? $settings['custom_home_custom'] : $home_link;
            $this->breadcrumbs[] = $this->add_crumb( __($settings['home_text'], 'webien'), $home_link, '', true);
        }


        // Collapsed breadcrumbs
        $collapsed = $this->breadcrumbs;
        if($settings['show_current'] === 'yes'){
            unset($collapsed[0]);
            $collapsed = array_values($collapsed);
        }
        if(!empty($collapsed)){
            $this->breadcrumbs_collapsed[] = $collapsed[0];
        }


        $this->breadcrumbs = array_reverse($this->breadcrumbs);

        // Remove first separator
        if(isset($this->breadcrumbs[0])){
            $this->breadcrumbs[0]->separator = false;
        }

    }


    function get_custom_breadcrumbs()
    {
        global $post;
        $settings = $this->get_settings_for_display();

        if($settings['show_current']){
            array_push($this->breadcrumbs, $this->add_crumb($post->post_title, get_permalink($post->ID), 'current' ) );
        }

        // Reverse order for correct position
        $custom_crumbs = array_reverse($settings['custom_selections']);

        // get posts from custom selection
        foreach ( $custom_crumbs as $index => $item ) {

            if($item['custom_selection_type'] === 'post'){
                array_push($this->breadcrumbs, $this->add_crumb(get_the_title($item['custom_selection']), get_permalink($item['custom_selection'])) );
            }

            if($item['custom_selection_type'] === 'posttype'){

                $url = get_post_type_archive_link($item['custom_selection_archive']);
                $post_type = get_post_type_object($item['custom_selection_archive']);
                $name = $post_type->label;
                $label = !empty($item['custom_selection_archive_label']) ? $item['custom_selection_archive_label'] : $name;

                array_push($this->breadcrumbs, $this->add_crumb($label, $url) );
            }

            if($item['custom_selection_type'] === 'custom'){

                $url = $item['custom_selection_custom_link'];
                $label = $item['custom_selection_custom_label'];

                array_push($this->breadcrumbs, $this->add_crumb($label, $url) );
            }

        }

        if($settings['show_post_type_home'] === 'yes'){
            $post_type = get_post_type_object($post->post_type);
            $name = $post_type->label;
            $link = get_post_type_archive_link($post->post_type);

            if($link){ // Not all post types have archives
                $this->breadcrumbs[] = $this->add_crumb($name, $link);
            }
        }


        if($settings['show_home'] === 'yes'){
            $type = $settings['custom_home_type'];
            $home_link = get_home_url();
            $home_link = $type === 'post' ? get_permalink($settings['custom_home_post']) : $home_link;
            $home_link = $type === 'custom' ? $settings['custom_home_custom'] : $home_link;
            $this->breadcrumbs[] = $this->add_crumb( __($settings['home_text'], 'webien'), $home_link, '', true);
        }


        // Collapsed breadcrumbs
        $collapsed = $this->breadcrumbs;
        if($settings['show_current'] === 'yes'){
            unset($collapsed[0]);
            $collapsed = array_values($collapsed);
        }
        if(!empty($collapsed)){
            array_push($this->breadcrumbs_collapsed, $collapsed[0]);
        }


        $this->breadcrumbs = array_reverse($this->breadcrumbs);

        // Remove first separator
        if(isset($this->breadcrumbs[0])){
            $this->breadcrumbs[0]->separator = false;
        }

    }


    function getPostSelection(){

        // Exit if for some reason not in editor
        if ( ! \Elementor\Plugin::$instance->editor->is_edit_mode() && !is_admin() ) return [];

        // Include public post types
        $post_types = get_post_types(['public' => true]);

        $posts = get_posts([
            'post_type' => $post_types,
            'post_status' => 'publish',
            'posts_per_page' => -1
        ]);

        $arr = [];

        foreach ($posts as $post){

            $pt = get_post_type_object($post->post_type);
            $label = $pt->label;

            $arr[$post->ID] = $label .  ' &rarr; ' . $post->post_title;
        }

        return $arr;
    }


    protected function getArchiveSelection(){

        $post_types = get_post_types(['public' => true, 'capability_type' => 'post'], 'objects');
        $arr = [];
        foreach ($post_types as $pt){
            $arr[$pt->name] = $pt->labels->singular_name;
        }

        return $arr;
    }


    protected function add_crumb($title, $href = null, $class = null, $home = false)
    {
        return (object)[
            'title' => $title,
            'href' => $href,
            'class' => $class,
            'separator' => true,
            'home' => $home,
        ];
    }



    function getSchemaMarkup($breadcrumbs)
    {
        $itemListElement = [];
        $position = 1;

        foreach ($breadcrumbs as $crumb) {
            $item = [
                "@type" => "ListItem",
                "position" => $position,
            ];

            if (!empty($crumb->href)) {
                $item["item"] = [
                    '@id' => $crumb->href,
                    "name" => $crumb->title,
                ];
            }

            $itemListElement[] = $item;
            $position++;
        }

        $schema = [
            "@context" => "https://schema.org",
            "@type" => "BreadcrumbList",
            "itemListElement" => $itemListElement,
        ];

        $html = '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '</script>';

        return $html;
    }


}
