<?php

namespace App\Paper;

use Illuminate\Support\Facades\DB;
use Mike42\Escpos\PrintConnectors\FilePrintConnector;
use Mike42\Escpos\Printer;
use Mike42\Escpos\EscposImage;
use Mockery\Exception;

class Paper
{
    private $imageDirectory = 'large/';
    private $imageDirectorySmall = 'small/';


    private $printer;
    private $connector;


    public function __construct()
    {
        try {
            $this->connector = new FilePrintConnector("/dev/usb/lp0");
            $this->printer = new Printer($this->connector);
        } catch (Exception $e) {
            die($e->getMessage());
        }
    }


    public function sendHeaderPrint($title)
    {
        if (strlen($title)) {
            $this->printer->setDoubleStrike(true);
            $this->printer->setJustification(Printer::JUSTIFY_CENTER);
            $this->printer->setEmphasis(true);
            $this->printer->text($title . "\n\n");
            $this->printer->setEmphasis(false);
            $this->printer->setJustification(Printer::JUSTIFY_LEFT);
            $this->printer->setDoubleStrike(false);
        }
    }

    public function sendImagePrint($image, $imageLocal = true)
    {
        if ($image) {
            if ($imageLocal) {
                $img = EscposImage::load($this->imageDirectory . basename($image) . '.png');
            } else { //image not local so then remote image
                $extension = strtolower(pathinfo($image, PATHINFO_EXTENSION));

                $tmpFile = storage_path() . '/image_packt.' . $extension;
                file_put_contents($tmpFile, file_get_contents($image));
                $img = EscposImage::load($tmpFile);
            }
            $this->printer->setJustification(Printer::JUSTIFY_CENTER);
            $this->printer->bitImageColumnFormat($img);
            $this->printer->feed(2);
            $this->printer->setJustification(Printer::JUSTIFY_LEFT);
        }
    }


    public function sendPrint($title, $text = '', $image = false, $imageLocal = true)
    {
        $this->sendImagePrint($image, $imageLocal);

        $this->sendHeaderPrint($title);
        $htmlToPos = new HtmlToPos();
        $this->printer->text($htmlToPos->convert($text));

        $this->printer->feed(4);
    }

    /**
     *
     * pobieranie ikon z bazy w kolejności ilości użyć, ikon z dysku a następnie
     * usunięcie pozdbioru ikon z bazy ze wszystkich ikon a następnie dodanie
     * ich na początek listy ikon
     *
     * @return array
     */
    public function getIcons()
    {
//        $icons = array_diff(scandir($this->imageDirectorySmall), ['.', '..']);

        $icons = ["address-card-o", "anchor", "archive-3", "at", "balance-scale", "ban", "bar-chart-o", "barcode", "battery-empty", "battery-full", "battery-half", "battery-quarter", "battery-three-quarters", "bed", "beer", "bell-o", "bell-slash-o", "bicycle", "birthday-cake", "bolt", "bomb", "book", "bug", "building-o", "bullhorn", "bus", "camera", "car", "chain", "chat-2", "check", "cloud", "code", "coffee", "cog", "cutlery", "dashboard", "database", "diamond", "dollar", "dribbble", "envelope-o", "envira", "exclamation-triangle", "female", "file-text-o", "film", "fingerprint", "fire-extinguisher", "fire", "flag-o", "flask", "floppy-o", "folder-o", "folder-open-o", "frown-o", "gamepad", "gift", "git", "glass", "graduation-cap", "grav", "group", "hand-o-left", "heart-o", "home", "lemon-o", "lightbulb-o", "list-alt", "location-arrow", "lock", "male", "map-1", "map-marker", "microchip", "money", "moon-o", "music", "paper-plane", "paperclip", "paw", "pencil", "phone", "pie-chart", "piggy-bank", "plane", "question-circle-o", "rocket", "search", "ship", "shopping-cart", "smile-o", "snowflake-o", "steam", "subway", "success", "support", "thermometer-2", "thumbs-o-down", "thumbs-o-up", "ticket", "times", "trash-o", "tree", "trophy", "truck", "umbrella", "usd", "warning", "wifi", "wpexplorer", "wrench", "youtube-play"];


        $iconsDatabase = [];
        $iconsDb = DB::select('SELECT icon FROM note WHERE icon is not null group by icon  ORDER BY count(icon) DESC ');
        foreach ($iconsDb as $icon) {
            $iconsDatabase[] = pathinfo(basename($icon->icon), PATHINFO_FILENAME);
        }
        return (array_merge($iconsDatabase, array_diff($icons, $iconsDatabase)));
    }

    public function __destruct()
    {
        $this->printer->close();
    }

}