<?php

namespace App\Dashboard\Widget\Helper;

class QrSheet
{

	/**
	 * The pivot content generation contained in the current sheet.
	 * @var \App\QuickReports\PivotTables\GenerateContent
	 */
	protected \App\QuickReports\PivotTables\GenerateContent $pivotContentGen;

	/**
	 * The sheet name.
	 * @var String
	 */
	protected $name;

	/**
	 * The sheet description.
	 * @var String
	 */
	protected $description;

	/**
	 * The inline title - appears in the first cell of the pivot.
	 * @var String
	 */
	protected $inlineTitle;

	/**
	 * The display currency for the values in the pivot.
	 * @var String
	 */
	protected $displayCurrency = null;

	/**
	 * The displayed filters.
	 * @var array
	 */
	protected $displayFilters = [];

	/**
	 * Initializes a QR sheet.
	 * @param \App\QuickReports\PivotTables\GenerateContent $pivotContentGen
	 * @param String $name
	 * @param String $description
	 */
	public function __construct(\App\QuickReports\PivotTables\GenerateContent $pivotContentGen, $name, $description, $inlineTitle)
	{
		$this->pivotContentGen = $pivotContentGen;
		$this->name = $name;
		$this->description = $description;
		$this->inlineTitle = $inlineTitle;
	}

	/**
	 * Returns the sheet name.
	 * @return String
	 */
	public function getName()
	{
		return $this->name;
	}

	/**
	 * Returns the sheet description.
	 * @return String
	 */
	public function getDescription()
	{
		return $this->description;
	}

	/**
	 * Returns the inline title.
	 * @return String
	 */
	public function getInlineTitle()
	{
		return $this->inlineTitle;
	}

	/**
	 * Returns the pivot content generation.
	 * @return \App\\App\QuickReports\PivotTables\GenerateContent\GenerateContent
	 */
	public function getPivotContentGen() :\App\QuickReports\PivotTables\GenerateContent
	{
		return $this->pivotContentGen;
	}

	/**
	 * Sets the display currency.
	 * @param String $displayCurrency
	 */
	public function setDisplayCurrency($displayCurrency)
	{
		$this->displayCurrency = $displayCurrency;
	}

	/**
	 * Returns the display currency.
	 * @return String
	 */
	public function getDisplayCurrency()
	{
		return $this->displayCurrency;
	}

	public function addDisplayFilter($field, $value)
	{
		$this->displayFilters[$field] = $value;
	}

	public function getDisplayFiltersText()
	{

		$pivotSource = $this->pivotContentGen->getPivotSource();
		$class = "\\App\\QuickReports\\PivotTables\\Data\\{$pivotSource}";

		$pivotData = new $class();

		$return = [];
		foreach($this->displayFilters as $field=>$value) {
			$filterReadValue = $pivotData->getColumnDisplayValue($field, $value);
			$filterFieldName = $pivotData->getColumnName($field);
			$return[] = "{$filterFieldName} = {$filterReadValue}";
		}

		return implode(" , ", $return);


	}

}

