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

class IDVE_Block_Divider {

    public static function register( IDVE_Blocks $registry ) {
        $registry->register( 'divider', array(
            'label'    => __( 'Divider', 'insight-dezign-visual-editor' ),
            'icon'     => 'minus',
            'category' => 'layout',
            'fields'   => array(
                'thickness' => array( 'type' => 'range', 'label' => __( 'Thickness (px)', 'insight-dezign-visual-editor' ), 'default' => 2, 'min' => 1, 'max' => 10, 'step' => 1 ),
                'color'     => array( 'type' => 'color', 'label' => __( 'Color', 'insight-dezign-visual-editor' ), 'default' => '#cccccc' ),
                'width'     => array( 'type' => 'select', 'label' => __( 'Width', 'insight-dezign-visual-editor' ), 'options' => array( '25%', '50%', '75%', '100%' ), 'default' => '100%' ),
                'alignment' => array( 'type' => 'select', 'label' => __( 'Alignment', 'insight-dezign-visual-editor' ), 'options' => array( 'left', 'center', 'right' ), 'default' => 'center' ),
            ),
            'render' => array( __CLASS__, 'render' ),
        ) );
    }

    public static function render( $data, $settings ) {
        $thickness = isset( $data['thickness'] ) ? absint( $data['thickness'] ) : 2;
        $color     = isset( $data['color'] ) && ! empty( $data['color'] ) ? $data['color'] : '#cccccc';
        $width     = isset( $data['width'] ) ? $data['width'] : '100%';
        $alignment = isset( $data['alignment'] ) ? $data['alignment'] : 'center';

        $margin = '0 auto';
        if ( 'left' === $alignment ) {
            $margin = '0 auto 0 0';
        } elseif ( 'right' === $alignment ) {
            $margin = '0 0 0 auto';
        }

        $inline = 'border-width:0;height:' . $thickness . 'px;background-color:' . esc_attr( $color ) . ';color:' . esc_attr( $color ) . ';width:' . esc_attr( $width ) . ';margin:' . $margin;

        $classes = 'idve-divider';
        if ( ! empty( $settings['cssClass'] ) ) {
            $classes .= ' ' . esc_attr( $settings['cssClass'] );
        }

        return '<hr class="' . $classes . '" style="' . esc_attr( $inline ) . '">';
    }
}
