<?php

namespace App\Http\Controllers;

use App\Models\QrCodes;
use App\Models\QrCodesList;
use Illuminate\Http\Request;
use Barryvdh\DomPDF\Facade\Pdf;

class QrCodeController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $stavke = QrCodes::all();
        return view('admin.qrcodes.lista', compact('stavke'));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return view('admin.qrcodes.nova');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {

        $stavka = new QrCodesList;

        $stavka->naziv_liste = $request->naziv_liste;
        $stavka->id_nagradne_igre = $request->id_nagradne_igre;
        $stavka->kolicina = $request->kolicina;
        // Čuvanje stavke u bazi podataka
        $stavka->save();

        // Dobijanje ID-a stavke koju ste upravo sačuvali
        $id_stavke = $stavka->id;
        $kolicina = $request->kolicina;
        $br_bodova = $request->br_bodova;
        // Prosleđivanje ID-a stavke funkciji generate_qr_codes()
        $this->generate_qr_codes($id_stavke, $br_bodova, $kolicina);

        //Redirektuj na list qr kodova
        return redirect('administracija/qrcodes');

    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $stavka = QrCodesList::where('id', $id)->first();
        return view('admin.qrcodes.pregled', compact('stavka'));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        $stavka = QrCodesList::where('id', $id)->first();
        $qrcodes = QrCodes::where('id_qrcodelist', $id)->get();
        if ($stavka) {
            $stavka->delete();
            foreach ($qrcodes as $qrcode) {
                if (!$qrcode->id_korisnik) {
                    $qrcode->delete();
                }
            }
            return redirect('administracija/qrcodes');
        } else {
            return redirect('administracija/qrcodes');
        }


    }

    public function generate_qr_codes(int $id, int $br_bodova, int $kolicina)
    {

        for ($i = 0; $i < $kolicina; $i++) {
            $qrcodes = new QrCodes;
            $qrcodes->id_qrcodelist = $id;
            $qrcodes->br_bodova = $br_bodova;
            $qrcodes->qrcode = $this->generateUniqueQRCode();
            $qrcodes->save();
        }

    }

    public function generateUniqueQRCode()
    {
        // Generišemo jedinstveni niz koristeći kombinaciju slova i brojeva
        $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        $length = 15;

        // Generišemo niz sa 15 nasumičnih znakova
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }

        return $randomString;
    }

    public function generate_pdf_qrcode_list(string $id)
    {

        $stavke = QrCodes::where('id_qrcodelist', $id)->get();

        try {
            if ($stavke) {
                $pdf = Pdf::loadView('admin.qrcodes.pdf.pregled', compact('stavke'))->setPaper('a4', 'portrait');
                // Opciono: Postavite imena fajlova i putanje gde će se preuzeti PDF
                $imeFajla = 'qrcodes-lista.pdf';
                return $pdf->stream($imeFajla);
            }
        } catch (\Exception $e) {
            return redirect()->back();
        }

    }

}