<?php

namespace App\Console\Commands;

use App\Http\Controllers\Parametres\Imports\ImportCSVClient;
use Illuminate\Console\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;

class AddDataClientCSVCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'import:csvdonnees:client {file} {user_id} {limit?} {offset?} {step?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '
  file : the file you want to add in DB
  user_id : the user that import the csv
  offset : from which line you want to read the file (optional)
  limit : the number of lines you want to save from the offset (optional)
  step : the number of steps at each recording (optional)
  ';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(ImportCSVClient $importCSVClient)
    {
        parent::__construct();
        $this->importCSVClient = $importCSVClient;
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $file = $this->argument('file');
        $user_id = $this->argument('user_id');
        $offset = $this->argument('offset') ?? 1;
        $limit = $this->argument('limit') !== null ? $this->argument('limit') + $offset : $this->importCSVClient->countCSVline($file, $user_id) - 1;
        $step = $this->argument('step') ?? $this->importCSVClient::STEP;

        if ($limit <= $offset) {
            $this->error("La limite doit être supérieure à l'offset");
            return 1;
        }

        if ($offset == 0) {
            $offset++;
            $limit++;
        }

        $csvlinecount = $this->importCSVClient->countCSVline($file, $user_id) - 1;

        if ($limit > $csvlinecount) {
            $limit = $csvlinecount;
        }

        if ($csvlinecount > 0) {

            $this->info(print_r("Nombre de ligne du fichier : " . $csvlinecount, true));

            $pourcent = 0;
            $output = new ConsoleOutput();
            $progressBar = new ProgressBar($output, $limit - $offset);
            $this->setProgressBar($progressBar, $limit, $pourcent);

            $progressBar->start();
            $headersAptimap = $this->importCSVClient->getHeaders($file, $user_id);
            for ($offset; $offset < $limit; $offset += $step) {
                if ($offset + $step >= $limit) {
                    $this->importCSVClient->import($file, $offset, $limit - $offset, $headersAptimap, $user_id);
                    $progressBar->advance($limit - $offset);
                    $pourcent = +$limit - $offset;
                } else {
                    $this->importCSVClient->import($file, $offset, $step, $headersAptimap, $user_id);
                    $progressBar->advance($step);
                    $pourcent += $step;
                }
                $progressBar->setMessage(round(($pourcent * 100) / $limit, 2) . '%', 'current_step');
                flush();
            }
            $progressBar->setMessage('100%', 'current_step');
            $progressBar->finish();
            $this->info(print_r("Terminé !", true));
            return 0;
        }
        $this->error("Le fichier CSV est vide ou n'existe pas.");
        return 1;
    }

    public function setProgressBar(ProgressBar $progressBar, $csvlinecount, $pourcent)
    {
        $progressBar->setFormat("Progression de l'import : %current_step%
                                                [%bar%]");
        $progressBar->setEmptyBarCharacter('░');
        $progressBar->setBarCharacter('▓');
        $progressBar->setProgressCharacter('>');
        $progressBar->setBarWidth(120);
        $progressBar->setMessage(($pourcent * 100) / $csvlinecount . '%', 'current_step');
    }

}
