<?php

namespace Elementor;

class BantaCartButton extends Widget_Base
{

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

    public function get_title()
    {
        return __('Jiawaz Cart Button', TEXT_DOMAIN);
    }

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

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

    public function get_keywords()
    {
        return ['search','button','icon'];
    }

    protected function _register_controls()
    {

        $this->start_controls_section(
            'section_content',
            [
                'label' => esc_html__('Content', TEXT_DOMAIN),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );
        $this->add_control(
            'icon',
            [
                'label' => esc_html__('Icon', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::ICONS,
            ]
        );

        $this->add_control(
            'dropdown_alignment_desktop',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__( 'Dropdown alignment in desktop', TEXT_DOMAIN ),
                'options' => [
                    'open-from-left' => [
                        'title' => esc_html__( 'left', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'open-from-right' => [
                        'title' => esc_html__( 'right', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'open-from-left',
            ]
        );

        $this->add_control(
            'dropdown_alignment_mobile',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__( 'Dropdown alignment in mobile/tablet', TEXT_DOMAIN ),
                'options' => [
                    'open-from-left-mobile' => [
                        'title' => esc_html__( 'left', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'open-from-right-mobile' => [
                        'title' => esc_html__( 'right', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'open-from-left',
            ]
        );

        $this->add_control(
            'show_count',
            [
                'label'   => esc_html__( 'Show count', TEXT_DOMAIN),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
                'separator' => 'before',
            ]
        );
        $this->end_controls_section();


        // icon styles
        $this->start_controls_section(
            'section_icon_style',
            [
                'label' => esc_html__( 'Button Style', TEXT_DOMAIN ),
                'tab' => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'button_size',
            [
                'label' => __('Button size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-primary' => 'width: {{SIZE}}{{UNIT}}!important;height: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_responsive_control(
            'icon_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .btn-primary,{{WRAPPER}} .btn-primary:before,{{WRAPPER}} .btn-primary:after' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_responsive_control(
            'icon_size',
            [
                'label' => __('icon size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-primary .iconly > *' => 'font-size: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );


        $this->add_control(
            'count_heading',
            [
                'label' => esc_html__( 'items count', TEXT_DOMAIN ),
                'type' => Controls_Manager::HEADING,
                'separator' => 'before',
            ]
        );

        $this->add_responsive_control(
            'count_size',
            [
                'label' => __('count size', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cart-badge' => 'width: {{SIZE}}{{UNIT}}!important;height: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );
        $this->add_responsive_control(
            'count_offset',
            [
                'label' => __('margin', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => -50,
                        'max' => 50,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .cart-badge' => 'top: {{SIZE}}{{UNIT}}!important;right: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'count_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .cart-badge',
            ]
        );

        $this->add_control(
            'count_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .cart-badge' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'count_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .cart-badge' => 'background: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'count_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .cart-badge' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'      => 'count_shadow',
                'selector'  => '{{WRAPPER}} .cart-badge',
            ]
        );





        $this->start_controls_tabs(
            'btn_style_tabs',[
                'separator' => 'before'
            ]
        );

        $this->start_controls_tab(
            'btn_style_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'icon_color',
            [
                'label' => esc_html__('icon color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary .iconly > *' => 'color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'icon_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary' => 'background: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'button_shadow',
                'selector' => '{{WRAPPER}} a.btn-primary.btn-open-wc-cart',
            ]
        );
        $this->end_controls_tab();


        $this->start_controls_tab(
            'btn_style_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );
        $this->add_control(
            'icon_color_hover',
            [
                'label' => esc_html__('icon color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary:hover .iconly > *' => 'color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_control(
            'icon_bg_color_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary:hover' => 'background: {{VALUE}}!important;',
                    '{{WRAPPER}} .btn-primary:before,{{WRAPPER}} .btn-primary:after' => 'border-color: {{VALUE}}!important;',
                ],
            ]
        );
        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'    => 'button_shadow_hover',
                'selector' => '{{WRAPPER}} a.btn-primary.btn-open-wc-cart:hover',
            ]
        );
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();

    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $icon = $settings['icon'];
        $dropdownAlignmentDesktop = $settings['dropdown_alignment_desktop'];
        $dropdownAlignmentMobile = $settings['dropdown_alignment_mobile'];
        $showCount = $settings['show_count'] == 'yes';

        $cartItemsCount = count(WC()->cart->get_cart());

        echo "<div class='cart-button-container'>";
        echo "<a href='#' class='btn btn-primary btn-open-wc-cart'>";
        if ($showCount){
            echo "<span class='cart-badge'>".$cartItemsCount."</span>";
        }
        echo "<span class='iconly'>";
        if (!empty($icon['value'])) {
            \Elementor\Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']);
        } else {
            echo "<span class='iconlyBulk-Bag'><span class='path1'></span><span class='path2'></span></span>";
        }
        echo "</span>";
        echo "</a>";
        echo "<div class='cart-dropdown $dropdownAlignmentDesktop $dropdownAlignmentMobile'>";
        echo "<div class='cart-dropdown-close'><i class='icon-x1'></i></div>";
        echo "<div class='woocommerce-cart-form'>";

        // Use the same function that's used in the fragment
        echo get_custom_cart_content();

        echo "</div>";
        echo "</div>";
        echo "</div>";
    }

}

Plugin::instance()->widgets_manager->register_widget_type(new BantaCartButton());