<?php

namespace Elementor;

class BantaAccountButton extends Widget_Base
{

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

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

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

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

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

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

        $this->add_control(
            'label',
            [
                'label' => esc_html__('logged in label', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::TEXT,
                'default' => esc_html__('My Account', TEXT_DOMAIN),
                'placeholder' => esc_html__('enter your text', TEXT_DOMAIN),
            ]
        );


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

        $this->add_control(
            'logged_in_icon',
            [
                'label' => esc_html__('logged in 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();
        $direction = $settings['direction'];
        $label = $settings['label'];
        $notLoggedInLabel = $settings['not_logged_label'];
        $icon = $settings['icon'];
        $loggedInIcon = $settings['logged_in_icon'];
        $my_account_page_id  = wc_get_page_id( 'myaccount' );
        $my_account_page_url = $my_account_page_id ? get_permalink( $my_account_page_id ) : '';

        echo "<a href='".$my_account_page_url."' class='btn btn-primary d-lg-inline-flex ".$direction."'>";
        echo "<span class='iconly'>";

        if(is_user_logged_in()){
            if (!empty($loggedInIcon['value'])) {
                \Elementor\Icons_Manager::render_icon($loggedInIcon, ['aria-hidden' => 'true']);
            } else {
                echo "<span class='iconlyBulk-Profile'><span class='path1'></span><span class='path2'></span></span>";
            }
        }else{
            if (!empty($icon['value'])) {
                \Elementor\Icons_Manager::render_icon($icon, ['aria-hidden' => 'true']);
            } else {
                echo "<span class='iconlyBulk-Login'><span class='path1'></span><span class='path2'></span></span>";
            }
        }

        echo "</span>";
        echo "<span class='btn-primary-tlt'>";
        if(is_user_logged_in()) {
            echo $label;
        }else{
            echo $notLoggedInLabel;
        }
        echo "</span>";
        echo "</a>";

    }

}

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