<?php

namespace GemBoards\utils\traits;

use Exception;
use GemBoards\Classes\Exception\GemboardsException;
use GemBoards\Entities\Column;
use GemBoards\Entities\FavouriteBoard;

trait Board {

	public static function get_board( int $id ) {
		try {
			return new \GemBoards\Entities\Board( $id );
		} catch ( GemBoardsException | Exception $e ) {
			return false;
		}
	}

	public static function get_column( int $id ) {
		try {
			return new Column( $id );
		} catch ( GemBoardsException | Exception $e ) {
			return false;
		}
	}

	public static function get_favourite_board( int $id ) {
		try {
			return new FavouriteBoard( $id );
		} catch ( GemBoardsException | Exception $e ) {
			return false;
		}
	}

	public static function get_all_labels(): array {
		$labels   = [];
		foreach (
			get_terms( [
				'taxonomy'   => GEMBOARDS_PLUGIN_SLUG . '_label',
				'hide_empty' => false,
			] ) as $term
		) {
			$labels[ $term->term_id ] = [
				'id'         => $term->term_id,
				'name'       => $term->name,
				'slug'       => $term->slug,
				'bg_color'   => get_term_meta( $term->term_id, 'bg_color', true ) ?: '#cccccc',
				'text_color' => get_term_meta( $term->term_id, 'text_color', true ) ?: '#000000',
			];
		}

		return $labels;
	}

	public static function get_all_priorities(): array {
		$priorities = [];
		foreach (
			get_terms( [
				'taxonomy'   => GEMBOARDS_PLUGIN_SLUG . '_priority',
				'hide_empty' => false,
			] ) as $term
		) {
			$priorities[ $term->term_id ] = [
				'id'   => $term->term_id,
				'name' => $term->name,
				'slug' => $term->slug,
			];
		}

		return $priorities;
	}

}
