<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class StreamerController extends Controller
{
    public function index ()
    {
        $urlGet = DB::table('config_pos_hardware')
        ->select('video1_conn')
        ->where('id_pos', 'PK1')
        ->first();

        // Log::info(json_encode($$urlStream->video1_conn));
        $urlStream = $urlGet->video1_conn;

        return view('streaming', compact('urlStream'));
    }
    
    public function streamSnapshot()
    {
        // Ambil URL kamera dan autentikasi
        // $url = "http://192.168.200.211/ISAPI/streaming/channels/1/picture"; // Contoh hardcoded
        // $url = "http://192.168.10.61/cgi-bin/snapshot.cgi"; // Contoh hardcoded
        $url = "http://admin:admin12345@192.168.10.61/cgi-bin/snapshot.cgi"; // Contoh hardcoded
        $username = "admin"; // Ganti dengan username
        $password = "metal4RC"; // Ganti dengan password kantor
        $password = "admin12345"; // Ganti dengan password

        try {
            // Menggunakan Digest Authentication
            $response = Http::withDigestAuth($username, $password)->get($url); //Auth Digest
            $response = Http::get($url); //Auth Basic

            if ($response->successful()) {
                // Kirimkan gambar langsung ke browser
                return response($response->body(), 200)
                    ->header('Content-Type', 'image/jpeg');
            } else {
                return response("Gagal mendapatkan snapshot", 500);
            }
        } catch (\Exception $e) {
            return response("Error: " . $e->getMessage(), 500);
        }
    }
}
