<?php

namespace App\Repositories;

use App\Repositories\Contracts\TagRepository;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Milon\Barcode\DNS2D;

class EloquentTagRepository extends EloquentBaseRepository implements TagRepository
{
    /**
     * Store a newly created resource in storage.
     *
     * @param $request
     * @return array
     */
    public function requestHandler($request)
    {
        DB::beginTransaction();
        $newTags = [];
        for($i = 0; $i < (int)$request['number']; $i++) {
            $uid = '0' . (DB::table('tags')->max('uid') + 1);
            $last_four_digits = substr($uid, -4);
            $dns = new DNS2D();
            $image = base64_decode($dns->getBarcodePNG($uid,'QRCODE',3.0,3.0));
            $barcode = 'tag_barcode/'.$uid.'.png';
            Storage::disk('public')->put($barcode, $image);
            $tag = [
                'uid'             => $uid,
                'barcode'         => $barcode,
                'qr_code' => $uid,
            ];
            $newTags[] = parent::save($tag);
        }
        DB::commit();

        return $newTags;
    }
}
