<?php

namespace GemBoards\Entities;

use GemBoards\Classes\AbstractEntity;
use GemBoards\Classes\Exception\GemboardsInvalidArgumentException;
use GemBoards\Collections\FolderCollection;
use GemBoards\factories\ProjectFactory;
use GemBoards\Utils\Helper;


class Project extends AbstractEntity {

	protected string $table       = 'gemboards_projects';
	protected string $object_type = 'project';

	protected array $data = [
		'owner_id'       => 0,
		'title'          => '',
		'slug'           => '',
		'project_status' => 'active',
		'description'    => '',
		'icon'           => '',
		'color'          => '',
		'created_at'     => '',
		'updated_at'     => '',
		'settings'       => [
			'background_type' => 'none',
			'background'      => '',
		],
	];

	protected array $folders     = [];
	protected bool $read_folders = false;

	public static function factory(): ProjectFactory {
		return ProjectFactory::init();
	}

	public function get_owner_id( string $context = 'view' ) {
		return (int) $this->get_prop( 'owner_id', $context );
	}

	public function get_title( string $context = 'view' ) {
		return $this->get_prop( 'title', $context );
	}

	public function get_slug( string $context = 'view' ) {
		return $this->get_prop( 'slug', $context );
	}

	public function get_project_status( string $context = 'view' ) {
		return $this->get_prop( 'project_status', $context );
	}

	public function get_description( string $context = 'view' ) {
		return $this->get_prop( 'description', $context );
	}

	public function get_icon( string $context = 'view' ) {
		return $this->get_prop( 'icon', $context );
	}

	public function get_color( string $context = 'view' ) {
		return $this->get_prop( 'color', $context );
	}

	public function get_date_created( string $context = 'view' ) {
		return $this->get_prop( 'created_at', $context );
	}

	public function get_date_updated( string $context = 'view' ) {
		return $this->get_prop( 'updated_at', $context );
	}

	/**
	 * @param bool $force_read
	 *
	 * @return Folder[]
	 */
	public function get_folders( bool $force_read = false ): array {
		if ( ! $this->read_folders || $force_read ) {
			$this->read_folders();
		}

		return $this->folders;
	}

	protected function read_folders() {
		$this->folders = [];

		try {
			$assignees          = new FolderCollection( [
				'per_page' => - 1,
				'where'    => [
					[
						'key'   => 'project_id',
						'value' => $this->get_id(),
					],
				],
			] );
			$this->folders      = $assignees->get_results();
			$this->read_folders = true;
		} catch ( GemboardsInvalidArgumentException $e ) {
			Helper::error_log( $e->getMessage() );
		}
	}

	public function get_settings( string $context = 'view' ): array {
		return $this->get_prop( 'settings', $context ) ?? [];
	}

	public function get_project_settings( string $key = '', string $context = 'view' ) {
		$settings = $this->get_settings( $context );
		if ( empty( $key ) ) {
			return $settings;
		} else {
			return array_key_exists( $key, $settings ) ? $settings[ $key ] : null;
		}
	}

	public function get_background_type() {
		return $this->get_project_settings( 'background_type' );
	}

	public function get_background() {
		return $this->get_project_settings( 'background' );
	}

	public function set_owner_id( int $value ) {
		$this->set_prop( 'owner_id', $value );
	}

	public function set_title( string $value ) {
		$this->set_prop( 'title', $value );
	}

	public function set_slug( string $value ) {
		$this->set_prop( 'slug', $value );
	}

	public function set_project_status( string $value ) {
		$this->set_prop( 'project_status', $value );
	}

	public function set_description( string $value ) {
		$this->set_prop( 'description', $value );
	}

	public function set_icon( string $value ) {
		$this->set_prop( 'icon', $value );
	}

	public function set_color( string $value ) {
		$this->set_prop( 'color', $value );
	}

	public function set_project_settings( string $key, $value ) {
		$settings         = $this->get_project_settings();
		$settings[ $key ] = $value;

		$this->set_prop( 'settings', $settings );
	}

	public function set_background_type( string $value ) {
		$this->set_project_settings( 'background_type', $value );
	}

	public function set_background( ?string $value ) {
		$this->set_project_settings( 'background', $value );
	}

	public function set_props( $props, string $context = 'set' ) {
		if ( array_key_exists( 'settings', $props ) && null === $props['settings'] ) {
			unset( $props['settings'] );
		} else {
			$props['settings'] = json_decode( $props['settings'], true );
		}

		return parent::set_props( $props, $context );
	}

}
