<?php
namespace Prodigy\Includes\Content;

use Prodigy\Includes\Prodigy;

/**
 * Prodigy brands data parser
 *
 * @version 3.1.5
 */
class Prodigy_Brands_Parser extends Prodigy_Main_Content {

	const BRAND_LOGOS_TYPE = 'brand_logos';
	const LOGOS_TYPE       = 'logos';

	/**
	 * @param array $response
	 *
	 * @return array
	 */
	public function get_brands_data( array $response ): array {
		$brands = array();

		if ( ! isset( $response['data'] ) || ! is_array( $response['data'] ) ) {
			return array();
		}

		foreach ( $response['data'] as $brand ) {
			$brand_id   = $brand['id'] ?? '';
			$brand_slug = $brand['attributes']['slug'] ?? '';
			$brand_name = $brand['attributes']['name'] ?? '';
			$brand_link = '';

			if ( $brand_slug ) {
				$term       = get_term_by( 'slug', $brand_slug, Prodigy::get_prodigy_brand_type() );
				$brand_link = $term && ! is_wp_error( $term ) ? get_term_link( $term ) : '';
			}

			$brands[ $brand_id ] = array(
				'brand_name' => $brand_name,
				'brand_slug' => $brand_slug,
				'brand_link' => $brand_link,
				'logos'      => array(),
			);

			if ( isset( $brand['relationships']['brand-logos']['data'] ) && is_array( $brand['relationships']['brand-logos']['data'] ) ) {
				foreach ( $brand['relationships']['brand-logos']['data'] as $logo ) {
					$brands[ $brand_id ]['logos'][] = array( 'logo_id' => $logo['id'] ?? null );
				}
			}
		}

		$result = $brands;
		if ( ! empty( $response['included'] ) ) {
			$default_brand_logos = $this->get_default_logos( $brands, $response['included'] );
			$result              = $this->get_brands_logos( $default_brand_logos, $response['included'] );
		}

		return $result;
	}

	/**
	 * @param array $brands
	 * @param array $included
	 *
	 * @return array
	 */
	private function get_brands_logos( array $brands, array $included ): array {
		$logosMap = $this->build_logos_map( $included, self::LOGOS_TYPE );

		foreach ( $brands as $brandKey => $brand ) {
			foreach ( $brand['logos'] as $key => $logo ) {
				if ( isset( $logo['logo_id'], $logosMap[ $logo['logo_id'] ] ) ) {
					$brands[ $brandKey ]['logos'][ $key ] = array_merge(
						$brands[ $brandKey ]['logos'][ $key ],
						$logosMap[ $logo['logo_id'] ]
					);
				}
			}
		}

		return $brands;
	}

	/**
	 * @param array $brands
	 * @param array $included
	 *
	 * @return array
	 */
	private function get_default_logos( array $brands, array $included ): array {
		$defaultLogosMap = $this->build_logos_map( $included, self::BRAND_LOGOS_TYPE );

		foreach ( $brands as $brandKey => $brand ) {
			foreach ( $brand['logos'] as $key => $logo ) {
				if ( isset( $defaultLogosMap[ $logo['logo_id'] ]['default'] ) && isset( $logo['logo_id'] ) && $defaultLogosMap[ $logo['logo_id'] ]['default'] ) {
					$brands[ $brandKey ]['logos'][ $key ]['logo_id'] = $defaultLogosMap[ $logo['logo_id'] ]['logo_id'];
					$brands[ $brandKey ]['logos'][ $key ]['default'] = $defaultLogosMap[ $logo['logo_id'] ]['default'];
				}
			}
		}

		return $brands;
	}

	/**
	 * Build a map of logos for quick access by ID
	 *
	 * @param array  $included
	 * @param string $type
	 * @return array
	 */
	private function build_logos_map( array $included, string $type ): array {
		$map = array();
		foreach ( $included as $include ) {
			if ( isset( $include['type'] ) && $include['type'] === $type ) {
				$id = $include['id'] ?? null;

				$map[ $id ] = array(
					'logo_id'        => $include['relationships']['logo']['data']['id'] ?? null,
					'default'        => $include['attributes']['default'] ?? null,
					'name'           => $include['attributes']['name'] ?? null,
					'logo-thumb-url' => $include['attributes']['logo-thumb-url'] ?? null,
					'logo-url'       => $include['attributes']['logo-url'] ?? null,
				);
			}
		}
		return $map;
	}
}
