<?php

namespace App\Repositories\Core;

class Export
{
    public $xls;

    public $sheet;

    public $filename;

    public $stockage;

    public $lig;

    public $nb;

    public $debug;

    public function __construct()
    {
        set_time_limit(0);
        ini_set('memory_limit', '1G');
        $this->xls = new PHPExcel();
    }

    public function create($filename, $stockage = false)
    {
        $this->sheet = $this->xls->setActiveSheetIndex(0);
        $this->filename = $filename;
        $this->stockage = $stockage;
        $this->lig = 1;
    }

    public function setColumnsTitle($data)
    {
        $col = 0;
        foreach ($data as $value) {
            $this->writeTitle($this->lig, $col, $value);
            $col++;
        }
        $this->lig++;
    }

    public function writeTitle($lig, $col, $txt)
    {
        $coord = $this->conv($lig, $col);
        $style = $this->sheet->getStyle($coord);
        $styleFont = $style->getFont();
        $styleFont->setBold(true);
        $styleFont->setSize(12);
        $styleFont->setName('Arial');
        // $styleFont->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
        $this->sheet->setCellValue($coord, $txt);
        if ($this->debug) {
            echo "Col $col Ligne $lig : $coord Text $txt<br/>";
        }
    }

    public function writeCell($lig, $col, $txt)
    {
        $coord = $this->conv($lig, $col);
        $this->sheet->setCellValue($coord, $txt);
        if ($this->debug) {
            echo "Col $col Ligne $lig : $coord Text $txt<br/>";
        }
    }

    public function exportRow($data, $config = null)
    {
        if ($config) {
            $vars = $config['vars'];
            $options = $config['options'];
            $multioptions = $config['multioptions'];
        }
        $col = 0;
        if (is_array($vars)) {
            foreach ($vars as $key) {
                $txt = $data[$key];
                if (isset($options[$key])) {
                    $txt = $options[$key][$txt];
                }
                if (! isset($multioptions[$key])) {
                    $this->writeCell($this->lig, $col, $txt);
                    $this->nb++;
                    $col++;

                    continue;
                }
                $tabs = self::getReverseMultiOptions($txt);
                foreach ($tabs as $key2 => $value) {
                    $txt .= $multioptions[$key][$key2].'\n';
                }

                $this->writeCell($this->lig, $col, $txt);
                $this->nb++;
                $col++;
            }
        } else {
            foreach ($data as $value) {
                $txt = $value;
                $this->writeCell($this->lig, $col, $txt);
                $this->nb++;
                $col++;
            }
        }
        $this->lig++;
    }

    public function header()
    {
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$this->filename.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        // header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
    }

    public function close()
    {
        if ($this->debug) {
            return;
        }
        // Debug::message($this->xls);
        $objWriter = PHPExcel_IOFactory::createWriter($this->xls, 'Excel2007');
        // Debug::message($objWriter);
        // exit;
        if (! $this->stockage) {
            $this->header();
            $objWriter->save('php://output');
        } else {
            // $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
            $objWriter->save('text.xlsx');
        }

    }

    public function conv($lig, $col)
    {
        $c = static::convColtoTxt($col);
        $lig = $this->lig;

        return "$c$lig";
    }

    public function convColtoTxt($col)
    {
        $col1 = $col % 26;
        $col2 = (int) floor($col / 26);
        if ($col2) {
            $c = chr(64 + $col2);
        } else {
            $c = '';
        }
        $c .= chr(65 + $col1);

        return $c;
    }

    public function getOption($var)
    {
        $data = $this->init();

        return $data[$var.'_options'];
    }

    public function getOptions($data)
    {
        $options = [];
        foreach ($data as $key => $value) {
            $var = substr($key, 0, -8);
            $options[$var] = $value;
        }

        return $options;
    }
}
