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

class B_Blade_Title extends \Bricks\Element {
  public $category     = 'Next Bricks - Texts';
  public $name         = 'blade-title';
  public $icon         = 'ti-text';
  public $tag      = 'h3';
  public $css_selector = '';
  public $scripts      = ['blade_title'];

  public function get_label() {
    return esc_html__( 'Blade Reveal', 'bricks' );
  }

  public function set_control_groups() {
    $this->control_groups['gradient'] = [
      'title' => esc_html__( 'Gradient', 'bricks' ),
      'tab'   => 'content',
    ];
  }
  
  public function set_controls() {
    $this->controls['inner_title'] = [
      'tab'            => 'content',
      'type'           => 'textarea',
	    'hasDynamicData' => 'text',
      'default'        => esc_html__( 'Blade Reveal', 'bricks' ),
      'placeholder'    => esc_html__( 'Title goes here ..', 'bricks' ),
    ];

    $this->controls['tag'] = [
        'tab'         => 'content',
        'label'       => esc_html__( 'HTML tag', 'bricks' ),
        'type'        => 'select',
        'options'     => [
            'h1' => 'h1',
            'h2' => 'h2',
            'h3' => 'h3',
            'h4' => 'h4',
            'h5' => 'h5',
            'h6' => 'h6',
            'p' => 'p',
            'span' => 'span',
            'custom' => 'custom',
        ],
        'clearable'   => false,
        'inline'      => true,
        'placeholder' => 'h3',
        'default'     => 'h3',
    ];
    $this->controls['customTag'] = [
			'tab'         => 'content',
			'label'       => esc_html__( 'Custom tag', 'bricks' ),
			'type'        => 'text',
			'inline'      => true,
			'placeholder' => 'h3',
			'required'    => [ 'tag', '=', 'custom' ],
    ];

    $this->controls['link'] = [
      'tab'   => 'content',
      'label' => esc_html__( 'Link to', 'bricks' ),
      'type'  => 'link',
    ];

    $this->controls['styleSeparator'] = [
      'label'      => esc_html__( 'Style', 'bricks' ),
      'type'       => 'separator',
    ];
    $this->controls['titleTypography'] = [
        'tab'     => 'content',
        'label'   => esc_html__( 'Typography', 'bricks' ),
        'type'    => 'typography',
              'css'     => [
          [
            'property' => 'typography',
            'selector' => '',
          ],
        ],
        'default' => [
          'line-height' => '1',
        ],
    ];

    $this->controls['animationSeparator'] = [
      'label'      => esc_html__( 'Animation', 'bricks' ),
      'type'       => 'separator',
    ];
    
    $this->controls['disablebuilder'] = [
      'hasDynamicData' => false,
      'tab' => 'content',
      'label'    => esc_html__( 'Disable at the builder', 'bricks' ),
      'type'     => 'checkbox',
      'default' => false,
    ];

    $this->controls['type'] = [
      'tab' => 'content',
      'label' => esc_html__( 'Split type', 'bricks' ),
      'type' => 'select',
      'options'     => [
        'chars' => 'chars',
        'words' => 'words',
        'lines' => 'lines',
    ],
      'inline' => true,
      'placeholder' => 'chars',
  ];

    $this->controls['from'] = [
      'tab' => 'content',
      'label' => esc_html__( 'From', 'bricks' ),
      'type' => 'select',
      'options'     => [
        '1' => 'bottom',
        '-1' => 'top',
    ],
      'inline' => true,
      'placeholder' => 'bottom',
  ];

  $this->controls['distance'] = [
    'tab' => 'content',
    'label' => esc_html__( 'Distance', 'bricks' ),
    'type' => 'number',
    'unit' => '%',
    'inline' => true,
    'placeholder' => '100%',
  ];

  $this->controls['skew'] = [
    'hasDynamicData' => false,
    'tab' => 'content',
    'label' => esc_html__( 'Skew', 'bricks' ),
    'type' => 'text',
    'inline' => true,
    'placeholder' => '0',
  ];

  $this->controls['stagger'] = [
    'hasDynamicData' => false,
    'tab' => 'content',
    'label' => esc_html__( 'Stagger', 'bricks' ),
    'type' => 'text',
    'inline' => true,
    'placeholder' => '0.05',
];

    $this->controls['duration'] = [
      'hasDynamicData' => false,
        'tab' => 'content',
        'label' => esc_html__( 'Duration', 'bricks' ),
        'type' => 'number',
        'unit' => 's',
        'inline' => true,
        'placeholder' => '0.6s',
    ];

    $this->controls['ease'] = [
      'hasDynamicData' => false,
        'tab' => 'content',
        'label' => esc_html__( 'GSAP easing', 'bricks' ),
        'type' => 'select',
        'options'     => [
          'none' => 'none',
          'power1' => 'power1',
          'power2' => 'power2',
          'power3' => 'power3',
          'power4' => 'power4',
          'back' => 'back',
          'bounce' => 'bounce',
          'circ' => 'circ',
          'elastic' => 'elastic',
          'expo' => 'expo',
          'sine' => 'sine',
          'steps' => 'steps',
          'custom' => 'custom'
        ],
        'inline' => true,
        'placeholder' => 'power1',
    ];
    
    $this->controls['customEase'] = [
      'hasDynamicData' => false,
      'tab'         => 'content',
      'label'       => esc_html__( 'Custom easing', 'bricks' ),
      'type'        => 'text',
      'inline'      => true,
      'default' => 'back.out(1.7)',
      'required'    => [ 'ease', '=', 'custom' ],
    ];

    $this->controls['scrolltrigger'] = [
      'label'      => esc_html__( 'ScrollTrigger', 'bricks' ),
      'type'       => 'separator',
      'description' => sprintf(esc_html__( 'Scrolltrigger %s', 'bricks' ), '<a href="https://greensock.com/docs/v3/Plugins/ScrollTrigger" target="_blank">documentation</a>'),
    ];

    $this->controls['trigger'] = [
      'hasDynamicData' => false,
        'tab' => 'content',
        'label' => esc_html__( 'Trigger', 'bricks' ),
        'type' => 'text',
        'inline' => true,
        'placeholder' => 'this',
    ];

    $this->controls['start'] = [
      'hasDynamicData' => false,
        'tab' => 'content',
        'label' => esc_html__( 'Start', 'bricks' ),
        'type' => 'text',
        'inline' => true,
        'placeholder' => 'top bottom',
    ];

    $this->controls['end'] = [
      'hasDynamicData' => false,
      'tab' => 'content',
      'label' => esc_html__( 'End', 'bricks' ),
      'type' => 'text',
      'inline' => true,
      'placeholder' => 'unset',
  ];

  $this->controls['delay'] = [
    'hasDynamicData' => false,
      'tab' => 'content',
      'label' => esc_html__( 'Delay', 'bricks' ),
      'type' => 'text',
      'inline' => true,
      'placeholder' => '0.2',
  ];

  $this->controls['scrub'] = [
    'hasDynamicData' => false,
      'tab' => 'content',
      'label' => esc_html__( 'Scrub', 'bricks' ),
      'type' => 'text',
      'inline' => true,
      'placeholder' => 'false',
  ];

  $this->controls['toggleActions'] = [
    'hasDynamicData' => false,
      'tab' => 'content',
      'label' => esc_html__( 'toggleActions', 'bricks' ),
      'type' => 'text',
      'placeholder' => 'play none none none',
  ];

  $this->controls['triggerSeparator'] = [
    'label'      => esc_html__( 'Additional Triggers', 'bricks' ),
    'type'       => 'separator',
  ];

  $this->controls['animationType'] = [
    'tab' => 'content',
    'label' => esc_html__( 'Trigger on', 'bricks' ),
    'type' => 'select',
    'options'     => [
      'Click' => 'Click',
      'Hover' => 'Hover',
    ],
    'inline' => true,
    'placeholder' => ' ',
  ];

  $this->controls['removescroll'] = [
    'hasDynamicData' => false,
    'tab' => 'content',
    'label'    => esc_html__( 'Disable on scroll', 'bricks' ),
    'type'     => 'checkbox',
    'default' => false,
  ];

  $this->controls['classTriggerClick'] = [
    'hasDynamicData' => false,
    'tab' => 'content',
    'label' => esc_html__( 'Element', 'bricks' ),
    'type' => 'text',
    'placeholder' => '.className',
    'inline' => true,
    'required' => [ 'animationType', '=', 'Click'],
  ];
  $this->controls['classTriggerHover'] = [
    'hasDynamicData' => false,
    'tab' => 'content',
    'label' => esc_html__( 'Element', 'bricks' ),
    'type' => 'text',
    'placeholder' => '.className',
    'inline' => true,
    'required' => [ 'animationType', '=', 'Hover'],
  ];

  //Gradient
  $this->controls['titleGradient'] = [
    'group' => 'gradient',
    'tab' => 'content',
    'type' => 'gradient',
    'css' => [
      [
        'property' => 'background-image',
        'selector' => '.bc-blade-title__inner .line, .bc-blade-title__inner .word, .bc-blade-title__inner .char',
      ],
    ],
    'default' => [
      'applyTo' => 'background',
      'gradientType' => 'linear',
      'colors' => [],
      'angle' => '0',
    ],
  ];

    //Padding from style tab applied to the root without link and to the link if exists
    $this->controls['_padding'] = [
      'tab'   => 'style',
      'group' => '_layout',
      'label' => esc_html__( 'Padding', 'bricks' ),
      'type'  => 'spacing',
      'css'   => [
        [
          'property' => 'padding',
          'selector' => '&:not(:has(> a)), > a',
        ]
      ],
      'default' => [
        'top'    => 0,
        'right'  => 0,
        'bottom' => 0,
        'left'   => 0,
      ],
    ];
  }


  public function enqueue_scripts() {
    wp_enqueue_script( 'blade_title', NEXTBRICKS_PLUGIN_URL . 'elements/js/bc_blade_title.min.js', array('bricks-scripts'), NEXTBRICKS_VERSION, false);
  }

  public function render() {
    $settings = $this->settings;
    $disablebuilder    = ! empty( $settings['disablebuilder'] ) ? $settings['disablebuilder'] : 0;
    $removescroll    = ! empty( $settings['removescroll'] ) ? $settings['removescroll'] : 0;
    $animationType    = ! empty( $settings['animationType'] ) ? $settings['animationType'] : '';
    $classTriggerClick    = ! empty( $settings['classTriggerClick'] ) ? $settings['classTriggerClick'] : 0;
    $classTriggerHover    = ! empty( $settings['classTriggerHover'] ) ? $settings['classTriggerHover'] : 0;

    $inner_title    = ! empty( $settings['inner_title'] ) ? $settings['inner_title'] : 'Next Bricks';
    $start = ! empty( $settings['start'] ) ? $settings['start'] : 'top bottom';
    $end = ! empty( $settings['end'] ) ? $settings['end'] : 'unset';
    $duration = ! empty( $settings['duration'] ) ? $settings['duration'] : 0.6;
    $delay = ! empty( $settings['delay'] ) ? $settings['delay'] : 0.2;
    $trigger = ! empty( $settings['trigger'] ) ? $settings['trigger'] : 'this';
    $ease = ! empty( $settings['ease'] ) ? $settings['ease'] : 'power1';
    if ($ease === 'custom') {
      $ease = ! empty( $settings['customEase'] ) ? $settings['customEase'] : 'power1';
    }
    $type = ! empty( $settings['type'] ) ? $settings['type'] : 'chars';
    $skew = ! empty( $settings['skew'] ) ? $settings['skew'] : 0;
    $stagger = ! empty( $settings['stagger'] ) ? $settings['stagger'] : '0.05';
    $from = ! empty( $settings['from'] ) ? $settings['from'] : '1';
    $distance = ! empty( $settings['distance'] ) ? $settings['distance'] : '100%';
    $scrub = ! empty( $settings['scrub'] ) ? $settings['scrub'] : 'false';
    $toggleActions = ! empty( $settings['toggleActions'] ) ? $settings['toggleActions'] : 'play none none none';

    $this->set_attribute( '_root', 'class', 'bc-blade-title' );
    $this->set_attribute( '_inner', 'class', 'bc-blade-title__inner' );
    
    if ( ! bricks_is_builder() ) {
      $this->set_attribute( '_inner', 'class', 'bc-blade-title__inner--to-hide' );
    }

    if ( ! empty( $settings['link'] ) ) {
			$this->set_link_attributes( '_link', $settings['link'] );
      $output ="
      <{$this->tag} {$this->render_attributes( '_root' )}>
        <a class='bc-blade-title__anchor' {$this->render_attributes( '_link' )}>
          <span {$this->render_attributes( '_inner' )}
          data-start=\"$start\"
          data-end=\"$end\"
          data-duration=\"$duration\"
          data-delay=\"$delay\"
          data-trigger=\"$trigger\"
          data-ease=\"$ease\"
          data-type=\"$type\"
          data-skew=\"$skew\"
          data-stagger=\"$stagger\"
          data-from=\"$from\"
          data-distance=\"$distance\"
          data-scrub=\"$scrub\"
          data-toggleActions=\"$toggleActions\"
          data-disable-builder=\"$disablebuilder\"
          data-animationType=\"$animationType\"
          data-remove-scroll=\"$removescroll\"
          data-classTriggerClick=\"$classTriggerClick\"
          data-classTriggerHover=\"$classTriggerHover\">$inner_title</span>
        </a>
      </{$this->tag}>
      ";
		}else{
      $output ="
      <{$this->tag} {$this->render_attributes( '_root' )}>
        <span {$this->render_attributes( '_inner' )}
        data-start=\"$start\"
        data-end=\"$end\"
        data-duration=\"$duration\"
        data-delay=\"$delay\"
        data-trigger=\"$trigger\"
        data-ease=\"$ease\"
        data-type=\"$type\"
        data-skew=\"$skew\"
        data-stagger=\"$stagger\"
        data-from=\"$from\"
        data-distance=\"$distance\"
        data-scrub=\"$scrub\"
        data-toggleActions=\"$toggleActions\"
        data-disable-builder=\"$disablebuilder\"
        data-animationType=\"$animationType\"
        data-remove-scroll=\"$removescroll\"
        data-classTriggerClick=\"$classTriggerClick\"
        data-classTriggerHover=\"$classTriggerHover\">$inner_title</span>
      </{$this->tag}>
      ";
    }

		echo $output;
  }
}
