<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class IDSC_Elementor_Social_Links extends \Elementor\Widget_Base {

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

    public function get_title() {
        return __( 'Social Links', 'idsc' );
    }

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

    public function get_categories() {
        return array( 'idsc' );
    }

    public function get_keywords() {
        return array( 'social', 'links', 'icons', 'share' );
    }

    protected function register_controls() {
        $this->start_controls_section( 'section_items', array(
            'label' => __( 'Social Links', 'idsc' ),
        ) );

        $repeater = new \Elementor\Repeater();

        $repeater->add_control( 'platform', array(
            'label'   => __( 'Platform', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'facebook',
            'options' => array(
                'facebook'  => 'Facebook',
                'twitter'   => 'Twitter / X',
                'instagram' => 'Instagram',
                'linkedin'  => 'LinkedIn',
                'youtube'   => 'YouTube',
                'tiktok'    => 'TikTok',
                'pinterest' => 'Pinterest',
                'github'    => 'GitHub',
                'dribbble'  => 'Dribbble',
                'email'     => 'Email',
                'website'   => 'Website',
                'rss'       => 'RSS',
            ),
        ) );

        $repeater->add_control( 'url', array(
            'label'       => __( 'URL', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::URL,
            'options'     => false,
            'placeholder' => 'https://',
        ) );

        $this->add_control( 'items', array(
            'label'       => __( 'Links', 'idsc' ),
            'type'        => \Elementor\Controls_Manager::REPEATER,
            'fields'      => $repeater->get_controls(),
            'default'     => array(
                array( 'platform' => 'facebook', 'url' => array( 'url' => '' ) ),
                array( 'platform' => 'twitter', 'url' => array( 'url' => '' ) ),
                array( 'platform' => 'instagram', 'url' => array( 'url' => '' ) ),
            ),
            'title_field' => '{{{ platform }}}',
        ) );

        $this->end_controls_section();

        $this->start_controls_section( 'section_settings', array(
            'label' => __( 'Settings', 'idsc' ),
        ) );

        $this->add_control( 'style', array(
            'label'   => __( 'Style', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'filled',
            'options' => array(
                'filled'  => __( 'Filled', 'idsc' ),
                'outline' => __( 'Outline', 'idsc' ),
                'plain'   => __( 'Plain', 'idsc' ),
            ),
        ) );

        $this->add_control( 'size', array(
            'label'   => __( 'Size', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'medium',
            'options' => array(
                'small'  => __( 'Small', 'idsc' ),
                'medium' => __( 'Medium', 'idsc' ),
                'large'  => __( 'Large', 'idsc' ),
            ),
        ) );

        $this->add_control( 'shape', array(
            'label'   => __( 'Shape', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'rounded',
            'options' => array(
                'square'  => __( 'Square', 'idsc' ),
                'rounded' => __( 'Rounded', 'idsc' ),
                'circle'  => __( 'Circle', 'idsc' ),
            ),
        ) );

        $this->add_control( 'color_mode', array(
            'label'   => __( 'Color Mode', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'brand',
            'options' => array(
                'brand'  => __( 'Brand Colors', 'idsc' ),
                'custom' => __( 'Custom Color', 'idsc' ),
            ),
        ) );

        $this->add_control( 'custom_color', array(
            'label'     => __( 'Custom Color', 'idsc' ),
            'type'      => \Elementor\Controls_Manager::COLOR,
            'default'   => '#333333',
            'condition' => array( 'color_mode' => 'custom' ),
        ) );

        $this->add_control( 'alignment', array(
            'label'   => __( 'Alignment', 'idsc' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'center',
            'options' => array(
                'left'   => __( 'Left', 'idsc' ),
                'center' => __( 'Center', 'idsc' ),
                'right'  => __( 'Right', 'idsc' ),
            ),
        ) );

        $this->end_controls_section();
    }

    protected function render() {
        $s     = $this->get_settings_for_display();
        $items = $s['items'] ?? array();

        if ( empty( $items ) ) {
            return;
        }

        $atts  = ' style="' . esc_attr( $s['style'] ) . '"';
        $atts .= ' size="' . esc_attr( $s['size'] ) . '"';
        $atts .= ' shape="' . esc_attr( $s['shape'] ) . '"';
        $atts .= ' color_mode="' . esc_attr( $s['color_mode'] ) . '"';
        if ( $s['color_mode'] === 'custom' && ! empty( $s['custom_color'] ) ) {
            $atts .= ' custom_color="' . esc_attr( $s['custom_color'] ) . '"';
        }
        $atts .= ' alignment="' . esc_attr( $s['alignment'] ) . '"';

        $inner = '';
        foreach ( $items as $item ) {
            $url = ! empty( $item['url']['url'] ) ? $item['url']['url'] : '';
            $inner .= '[idsc_social_link platform="' . esc_attr( $item['platform'] ) . '" url="' . esc_attr( $url ) . '"]';
        }

        echo do_shortcode( '[idsc_social_links' . $atts . ']' . $inner . '[/idsc_social_links]' );
    }
}
