<?php

namespace App\Http\ReportGenerator;

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;
    }

    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;
    }

    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;
    }
}
