<?php

if (!function_exists('flo_categories_render_frontend_block')) {
	function flo_categories_render_frontend_block($attr = [])
	{
		extract($attr);

		Flo_Guten_Blocks_Helper::load_google_fonts([
			$itemsTypography,
			$titleTypography,
		]);

		$alignfullClass = '';
		if (isset($attr['align']) && $attr['align'] === 'full') {
			$alignfullClass = ' alignfull';
		}

		$linksType = $attr['linksType'];
		$postType = $attr['postType'];
		$blockTitle = $attr['blockTitle'];
		$catItems = $attr['categoriesItems'];
		$catAllItems = $attr['categoriesAllItems'];
		$taxonomy = $attr['taxonomyType'];
		$itemsGap = $attr['itemsGap'];
		if (empty($catItems)) {
			$catItems = $catAllItems;
		}
		$itemColor = Flo_Guten_Blocks_Helper::get_style_color(
			$attr['elementsColor']
		);
		$activeItemColor = Flo_Guten_Blocks_Helper::get_style_color(
			$attr['activeItemColor']
		);
		$itemColorHover = Flo_Guten_Blocks_Helper::get_style_color(
			$attr['elementsColorHover']
		);
		$indicatorColor = Flo_Guten_Blocks_Helper::get_style_color(
			$attr['indicatorColor']
		);
		$bordeColor = Flo_Guten_Blocks_Helper::get_style_color($attr['bordeColor']);
		$bgColor = Flo_Guten_Blocks_Helper::get_style_color(
			$attr['backgroundColor']
		);

		$titleFont = Flo_Guten_Blocks_Helper::get_style_for_typography(
			'block-title',
			$titleTypography
		);
		$itemsFont = Flo_Guten_Blocks_Helper::get_style_for_typography(
			'block-items',
			$itemsTypography
		);

		$dataTitleFont =
			$titleTypography['type'] == 'theme' ? $titleTypography['theme'] : '';
		$dataItemsFont =
			$itemsTypography['type'] == 'theme' ? $itemsTypography['theme'] : '';

		$hideHoverItem = $attr['hideHoverItem'];
		$customItems = $attr['customItems'];
		$csClassName = 'flo-category-switcher';
		$hide = '';



 if(is_home() && is_front_page())
 {
	$pageLink = home_url( '/' );

 }
 else
 {
	$pageLink = get_permalink();
 }
		if (empty($hideHoverItem)) {
			$hide = 'hidden';
		}

		if ($linksType == 'custom-links') {
			$catItemsHtml = '';
			$catItemsListHtml = '';
			$blockTitleHtml = '';
			if ($blockTitle) {
				$blockTitleHtml = "
            <div
              class=\"{$csClassName}__title\" data-font=\"{$dataTitleFont}\" >
              {$blockTitle}
            </div>
            ";
			}

			if ($customItems) {
				foreach ($customItems as $item) {
					$targetUrl = $item['targetUrl'] ? 'target="_blank"' : '';
					$catItemsListHtml .= "
              <div class=\"{$csClassName}__item {$hide}\">
                  <a  {$targetUrl} href=\"{$item['url']}\"><span data-font=\"{$dataItemsFont}\">{$item['title']}</span></a>
              </div>
              ";
				}
				$catItemsHtml = "
            <div class=\"{$csClassName}__items\">{$catItemsListHtml}</div>
          ";
			}
		} else {
			$catItemsHtml = '';
			$catItemsListHtml = '';
			$blockTitleHtml = '';
			if ($blockTitle) {
				$blockTitleHtml = "
            <div  class=\"{$csClassName}__title\" data-font=\"{$dataTitleFont}\" >
              {$blockTitle}
            </div>
            ";
			}

			if ($catItems) {
				if (!empty($allLinksLabel)) {
					$catItemsListHtml = "
              <div class=\"{$csClassName}__item {$hide}\"  >
                  <a href=\"{$pageLink}\" ><span data-font=\"{$dataItemsFont}\">{$allLinksLabel}</span></a>
              </div>
            ";
				}
				foreach ($catItems as $item) {
					$currentUrl = $_SERVER['REQUEST_URI'];
					$itemLabel = strtolower($item['label']);
					$getTerm = get_term($item['value'], $taxonomy);
					$activeCat = strpos($currentUrl, $itemLabel) ? 'active-cat' : '';

					if ($getTerm) {
						$termLink = get_term_link($getTerm->term_id);
						//$pageLink = get_permalink();
						$catItemsListHtml .= "
								<div  class=\"{$csClassName}__item {$hide} {$activeCat}\" >
										<a href=\"{$pageLink}?tax=$taxonomy&categ=$getTerm->slug\" ><span data-font=\"{$dataItemsFont}\">{$getTerm->name}</span></a>
								</div>
								";
					}
				}
				$catItemsHtml = "    <div class=\"{$csClassName}__items\">{$catItemsListHtml}</div>  ";
			}
		}
		$html =
			"
    <div class=\"{$csClassName} {$alignfullClass} flo-block " .
			$attr['className'] .
			Flo_Guten_Blocks_Helper::get_additional_class($attr) .
			"\"
      style=\"--bg-color: {$bgColor};
      --color-title: {$itemColor};
      {$titleFont}
      {$itemsFont}
      --color-item: {$itemColor};
	  --active-color-item: {$activeItemColor};
      --margin-left:" .
			Flo_Guten_Blocks_Helper::get_em_by_value($itemsGap) .
			";
      --color-indicator: {$indicatorColor};
      --color-item-hover: {$itemColorHover};
      --border-color: {$bordeColor};
      --margin-top: " .
			Flo_Guten_Blocks_Helper::get_em_by_value($marginTop) .
			";
      --margin-bottom: " .
			Flo_Guten_Blocks_Helper::get_em_by_value($marginBottom) .
			";
      --mobile-margin-top: " .
			Flo_Guten_Blocks_Helper::get_em_by_value($mobileMarginTop) .
			";
      --mobile-margin-bottom: " .
			Flo_Guten_Blocks_Helper::get_em_by_value($mobileMarginBottom) .
			";
      \"
	  " .
			(!empty($anchor) ? 'id="' . $anchor . '"' : '') .
			"
    >
      <div class=\"{$csClassName}__wrap\"  >
        {$blockTitleHtml}
        {$catItemsHtml}
      </div>
    </div>
  ";
		return apply_filters('flo_category_block_output', $html, $attr);
	}
}
