<?php

namespace App\Http\ReportGenerator;

use Illuminate\Contracts\Config\Repository as ConfigRepository;

class ReportGenerator
{
  	protected $applyFlush = false;
    protected $company;
    protected $client;
    protected $supplier;
    protected $aux;
    protected $observation;
    protected $balance;
    protected $firma;
    protected $caption = '';
    protected $fiscal;

  	protected $headers;
  	protected $columns;
  	protected $query;
  	protected $limit = null;
  	protected $groupByArr = [];
  	protected $paper = 'a4';
  	protected $orientation = 'portrait';
  	protected $editColumns = [];
  	protected $showNumColumn = true;
  	protected $showTotalColumns = [];
  	protected $styles = [];
  	protected $simpleVersion = false;
    protected $view = 'reports.list-pdf-template';
    protected $showMeta = true;
    protected $showHeader = true;
    protected $showCompany = true;
    protected $showClient = true;
    protected $showAux = false;
    protected $showObservation = true;
    protected $showBalance = true;
    protected $showFirma = true;
    protected $showCaption = true;
    protected $showFiscal = false;
    protected $showSupplier = true;

  	public function __construct()
  	{

  	}

  	public function of($title, Array $meta = [], $query, Array $columns)
  	{
    		$this->headers = [
      			'title' => $title,
      			'meta'  => $meta
    		];

    		$this->query = $query;
    		$this->columns = $this->mapColumns($columns);

    		return $this;
  	}

    public function showHeader($value = true)
    {
        $this->showHeader = $value;

        return $this;
    }

    public function showMeta($value = true)
    {
        $this->showMeta = $value;

        return $this;
    }

    public function showNumColumn($value = true)
    {
        $this->showNumColumn = $value;

        return $this;
    }

  	public function simple()
  	{
    		$this->simpleVersion = true;

    		return $this;
  	}

  	public function setView($view)
  	{
    		$this->view = strtolower($view);

    		return $this;
  	}

    public function setDetails(Array $company, Array $client, Array $supplier, Array $aux, Array $observation, Array $balance, Array $firma, $caption, Array $fiscal)
    {
        $this->company = $company;
        $this->client = $client;
        $this->supplier = $supplier;
        $this->aux = $aux;
        $this->observation = $observation;
        $this->balance = $balance;
        $this->firma = $firma;
        $this->caption = $caption;
        $this->fiscal = $fiscal;

        return $this;
    }

  	private function mapColumns(Array $columns)
  	{
    		$result = [];

    		foreach ($columns as $name => $data) {
    			if (is_int($name)) {
    				$result[$data] = snake_case($data);
    			} else {
    				$result[$name] = $data;
    			}
    		}

    		return $result;
  	}

  	public function setPaper($paper)
  	{
    		$this->paper = strtolower($paper);

    		return $this;
  	}

  	public function editColumn($columnName, Array $options)
  	{
    		foreach ($options as $option => $value) {
    			$this->editColumns[$columnName][$option] = $value;
    		}

    		return $this;
  	}

  	public function editColumns(Array $columnNames, Array $options)
  	{
    		foreach ($columnNames as $columnName) {
    			$this->editColumn($columnName, $options);
    		}

    		return $this;
  	}

  	public function showTotal(Array $columns)
  	{
    		$this->showTotalColumns = $columns;

    		return $this;
  	}

  	public function groupBy($column)
  	{
    		if (is_array($column)) {
    			$this->groupByArr = $column;
    		} else {
    			array_push($this->groupByArr, $column);
    		}

    		return $this;
  	}

  	public function limit($limit)
  	{
    		$this->limit = $limit;
    		return $this;
  	}

  	public function setOrientation($orientation)
  	{
    		$this->orientation = strtolower($orientation);

    		return $this;
  	}

  	public function setCss(Array $styles)
  	{
    		foreach ($styles as $selector => $style) {
    			array_push($this->styles, [
    				'selector' => $selector,
    				'style' => $style
    			]);
    		}

    		return $this;
  	}
}
