<?php

namespace BwWinnersGlobalSite\Entities;

if ( ! class_exists( __NAMESPACE__ . '\Entity_Service' ) ) {

	class Entity_Service {

		private Entity_Cache $cache;
		private Entity_Store $store;

		public function __construct( Entity_Cache $cache, Entity_Store $store ) {
			$this->cache = $cache;
			$this->store = $store;
		}

		public function end_sync ( $update_at ) {
			// $this->store->clean( $update_at );
			$this->cache->flush_cache();
		}

		// ----------------------------------------------------------------------------
		// Brand
		// ----------------------------------------------------------------------------

		public function get_brands () {
			return $this->store->get_brands();
		}

		public function get_brand ( $brand_id ) {

			if ( $brand = $this->cache->get_brand( $brand_id ) ) {
				return $brand;
			}

			$brand = $this->store->get_brand( $brand_id );

			if ( ! empty( $brand ) ) {
				$brand['awards'] = $this->get_brand_awards( $brand_id );
				$brand['posts'] = $this->get_brand_posts( $brand_id );
			}

			$this->cache->set_brand( $brand );

			return $brand;
		}

		public function get_brand_id ( $import_id ) {
			return $this->store->get_brand_id( $import_id );
		}

		public function get_brand_id_from_name ( $brand_name ) {
			return $this->store->get_brand_id_from_name( $brand_name );
		}

		public function update_brand ( $brand ) {

			if ( ! empty ( $brand['id'] ) ) {
				$this->cache->delete_brand( $brand['id'] );
			}

			return $this->store->update_brand( $brand );
		}

		public function delete_brand ( $brand_id ) {

			if ( ! empty ( $brand_id ) ) {
				$this->cache->delete_brand( $brand_id );
			}

			return $this->store->delete_brand( $brand_id );
		}

		public function get_brand_posts ( $brand_id ) {
			return $this->store->get_brand_posts( $brand_id );
		}

		public function set_brand_posts ( $brand_id, $post_ids ) {
			return $this->store->set_brand_posts( $brand_id, $post_ids );
		}

		public function get_post_brands ( $post_id ) {
			return $this->store->get_post_brands( $post_id );
		}

		public function set_post_brands ( $post_id, $brand_ids ) {
			return $this->store->set_post_brands( $post_id, $brand_ids );
		}


		// ----------------------------------------------------------------------------
		// Brand Awards
		// ----------------------------------------------------------------------------

		public function get_brand_awards ( $brand_id ) {
			return $this->store->get_brand_awards( $brand_id );
		}

		public function set_brand_awards ( $brand_id, $awards ) {
			return $this->store->set_brand_awards( $brand_id , $awards);
		}

		public function delete_brand_awards ( $brand_id ) {
			return $this->store->delete_brand_awards( $brand_id );
		}

		public function get_brand_award ( $award_id ) {
			return $this->store->get_brand_award( $brand_id );
		}

		public function get_brand_award_id ( $award ) {
			return $this->store->get_brand_award_id( $award );
		}

		public function update_brand_award ( $award ) {

			if ( ! empty( $award['brand_id'] ) ) {
				$this->cache->delete_brand( $award['brand_id'] );
			}

			return $this->store->update_brand_award( $award );
		}

		public function delete_brand_award ( $award_id ) {

			$this->cache->flush_cache();

			return $this->store->delete_brand_award( $award_id );
		}


		// ----------------------------------------------------------------------------
		// Product
		// ----------------------------------------------------------------------------

		public function get_products () {
			return $this->store->get_products();
		}

		public function get_product ( $product_id ) {

			if ( $product = $this->cache->get_product( $product_id ) ) {
				return $product;
			}

			$product = $this->store->get_product( $product_id );

			if ( ! empty( $product ) ) {
				$product['entries'] = $this->get_product_entries( $product_id );
				$product['awards'] = $this->get_product_awards( $product_id );
			}

			$this->cache->set_product( $product );

			return $product;
		}

		public function get_product_id ( $import_id ) {
			return $this->store->get_product_id( $import_id );
		}

		public function get_product_id_from_name ( $product_name, $brand_id ) {
			return $this->store->get_product_id_from_name( $product_name, $brand_id );
		}

		public function update_product ( $product ) {

			if ( ! empty ( $product['id'] ) ) {
				$this->cache->delete_product( $product['id'] );
			}

			return $this->store->update_product( $product );
		}

		public function delete_product ( $product_id ) {

			if ( ! empty ( $product_id ) ) {
				$this->cache->delete_product( $product_id );
			}

			return $this->store->delete_product( $product_id );
		}


		// ----------------------------------------------------------------------------
		// Product Awards
		// ----------------------------------------------------------------------------

		public function get_product_awards ( $product_id ) {
			return $this->store->get_product_awards( $product_id );
		}

		public function set_product_awards ( $product_id, $awards ) {
			return $this->store->set_product_awards( $product_id , $awards);
		}

		public function delete_product_awards ( $product_id ) {
			return $this->store->delete_product_awards( $product_id );
		}

		public function get_product_award ( $award_id ) {
			return $this->store->get_product_award( $product_id );
		}

		public function get_product_award_id ( $award ) {
			return $this->store->get_product_award_id( $award );
		}

		public function update_product_award ( $award ) {

			if ( ! empty( $award['product_id'] ) ) {
				$this->cache->delete_product( $award['product_id'] );
			}

			return $this->store->update_product_award( $award );
		}

		public function delete_product_award ( $award_id ) {

			$this->cache->flush_cache();

			return $this->store->delete_product_award( $award_id );
		}


		// ----------------------------------------------------------------------------
		// Product Entries
		// ----------------------------------------------------------------------------

		public function get_product_entries ( $product_id ) {
			return $this->store->get_product_entries( $product_id );
		}

		public function set_product_entries ( $product_id, $entries ) {
			return $this->store->set_product_entries( $product_id, $entries );
		}

		public function delete_product_entries ( $product_id ) {
			return $this->store->delete_product_entries( $product_id );
		}

		public function get_entry ( $entry_id ) {

			return $this->store->get_entry( $product_id );
		}

		public function update_entry ( $entry ) {

			if ( ! empty( $entry['product_id'] ) ) {
				$this->cache->delete_product( $entry['product_id'] );
			}

			return $this->store->update_entry( $entry );
		}

		public function delete_entry ( $entry_id ) {

			$this->cache->flush_cache();

			return $this->store->delete_entry( $entry_id );
		}


		// ----------------------------------------------------------------------------
		// Competition
		// ----------------------------------------------------------------------------

		public function get_competitions () {
			return $this->store->get_competitions();
		}

		public function get_competition ( $competition_id ) {

			if ( $competition = $this->cache->get_competition( $competition_id ) ) {
				return $competition;
			}

			$competition = $this->store->get_competition( $competition_id );

			$this->cache->set_competition( $competition );

			return $competition;
		}		

		public function get_competition_id ( $name ) {
			return $this->store->get_competition_id( $name );
		}

		public function update_competition ( $competition ) {

			if ( ! empty ( $competition['id'] ) ) {
				$this->cache->delete_competition( $competition['id'] );
			}

			return $this->store->update_competition( $competition );
		}

		public function delete_competition ( $competition_id ) {

			if ( ! empty ( $competition_id ) ) {
				$this->cache->delete_competition( $competition_id );
			}

			return $this->store->delete_competition( $competition_id );
		}

		public function get_competition_entries ( $competition_id ) {
			return $this->store->get_competition_entries( $competition_id );
		}

		public function get_competition_product_categories ( $competition_id ) {
			return $this->store->get_competition_product_categories( $competition_id );
		}
	}
}
