<?php

namespace Elementor;

class BantaButton extends Widget_Base
{

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

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

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

    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(
            'label',
            [
                'label' => esc_html__('label', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('Click me', TEXT_DOMAIN),
                'placeholder' => esc_html__('enter your text', TEXT_DOMAIN),
            ]
        );

        $this->add_control(
            'link',
            [
                'label' => esc_html__('link', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::URL,
                'placeholder' => esc_html__('https://your-link.com', TEXT_DOMAIN),
                'dynamic' => [
                    'active' => true,
                ],
            ]
        );

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

        $this->add_control(
            'direction',
            [
                'type' => \Elementor\Controls_Manager::SELECT,
                'label' => esc_html__( 'direction', TEXT_DOMAIN ),
                'options' => [
                    '' => [
                        'title' => esc_html__( 'before', TEXT_DOMAIN ),
                    ],
                    'dir-ltr' => [
                        'title' => esc_html__( 'after', TEXT_DOMAIN ),
                    ],
                ],
                'default' => '',
            ]
        );

        $this->end_controls_section();


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

        $this->add_responsive_control(
            'btn_padding',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .btn-primary' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );
        $this->add_responsive_control(
            'button_height',
            [
                'label' => __('Button height', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 1,
                        'max' => 200,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-primary' => '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_margin',
            [
                'label' => __('space between', TEXT_DOMAIN),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em', 'rem'],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'size' => 5,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .btn-primary' => 'gap: {{SIZE}}{{UNIT}}!important;',
                ],
            ]
        );


        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'btn_typography',
                'label'    => esc_html__( 'typography', TEXT_DOMAIN ),
                'selector' => '{{WRAPPER}} .btn-primary .btn-primary-tlt',
            ]
        );

        $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->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(
            'label_color',
            [
                'label' => esc_html__('label color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary-tlt' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'icon_color',
            [
                'label' => esc_html__('icon color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary .iconly > *' => 'color: {{VALUE}};',
                ],
            ]
        );
        $this->add_control(
            'icon_bg_color',
            [
                'label' => esc_html__('background color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary' => 'background: {{VALUE}};',
                ],
            ]
        );
        $this->end_controls_tab();


        $this->start_controls_tab(
            'btn_style_hover_tab',
            [
                'label' => esc_html__('hover', TEXT_DOMAIN),
            ]
        );
        $this->add_control(
            'label_color_hover',
            [
                'label' => esc_html__('label color', TEXT_DOMAIN),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .btn-primary:hover .btn-primary-tlt' => 'color: {{VALUE}};',
                ],
            ]
        );
        $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}};',
                ],
            ]
        );
        $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}};',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->end_controls_section();

    }

    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $label = $settings['label'];
        $icon = $settings['icon'];
        $direction = $settings['direction'];

        if (!empty($settings['link']['url'])) {
            $this->add_link_attributes('link', $settings['link']);
        }

        echo "<a " . $this->get_render_attribute_string('link') . " class='btn btn-primary d-lg-inline-flex ".$direction."'>";
        echo "<span class='iconly'>";
        if (!empty($icon['value'])) {
            \Elementor\Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']);
        } else {
            echo "<span class='iconlyBulk-Profile'><span class='path1'></span><span class='path2'></span></span>";
        }
        echo "</span>";
        echo "<span class='btn-primary-tlt'>" . $label . "</span>";
        echo "</a>";

    }

}

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