<?php

namespace App\Http\Controllers;

use App\Models\TestSessionsModel;
use App\Models\TobiiDataModel;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class TobiiReceiverController extends Controller
{
    /**
     * Fetch eye tracking data from database.
     */
    public function fetchDataTobii($id_test_sessions)
    {
        $data_tobii = TobiiDataModel::where('id_test_sessions', $id_test_sessions)->orderByDesc('timestamp')->first();
        if ($data_tobii) {
            Log::info('DATA DIAMBIL WEB', [
                'timestamp_data' => $data_tobii->timestamp,
                'created_at' => $data_tobii->created_at,
                'time_now' => now()->timestamp * 1000
            ]);
            return response()->json([
                'status' => true,
                'msg' => 'Data tobii ditemukan (' . $id_test_sessions . ')',
                'gaze_x' => $data_tobii->gaze_x,
                'gaze_y' => $data_tobii->gaze_y,
            ]);
        } else {
            return response()->json([
                'status' => false,
                'msg' => 'Data tobii tidak ditemukan (' . $id_test_sessions . ')',
            ]);
        }
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $data_sessions = TestSessionsModel::where('id_test_sessions', $request->id_test_sessions)->first();
            if($data_sessions != null) {
                if($request->token_session == $data_sessions->token_session) {
                    if($data_sessions->status_session == '1') {
                        $is_valid = '0';
                        if($data_sessions->is_active == '1') {
                            $is_valid = '1';
                        }
                        // simpan data tobii ke db
                        $data_tobii = TobiiDataModel::create([
                            'gaze_x' => $request->gaze_x,
                            'gaze_y' => $request->gaze_y,
                            'timestamp' => Carbon::createFromTimestampMs($request->timestamp),
                            'is_valid' => $is_valid,
                            'id_test_sessions' => $request->id_test_sessions,
                        ]);
                        Log::info('DATA MASUK API', [
                            'timestamp_client' => Carbon::createFromTimestampMs($request->timestamp),
                            'time_server' => now()->timestamp * 1000
                        ]);
                        return response()->json(['message' => 'Data tobii berhasil disimpan.'], 200);
                    } else {
                        return response()->json(['message' => 'Sesi tes belum aktif.'], 403);
                    }
                } else {
                    return response()->json(['message' => 'Token sesi tidak valid.'], 401);
                }
            } else {
                return response()->json(['message' => 'Data sesi tes tidak ditemukan.'], 404);
            }
        } catch(\Exception $e) {
            return response()->json(['message' => 'Terjadi kesalahan saat menyimpan data tobii.', 'error' => $e->getMessage()], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        $data_tobii = TobiiDataModel::where('id_test_sessions', $id)->orderByDesc('timestamp')->first();
        if ($data_tobii && $data_tobii->timestamp->lt(now()->subSeconds(20))) {
            return response()->json([
                'status' => false,
                'msg' => 'Data tobii kadaluarsa (' . $id . ')',
                'connected' => false,
            ]);
        }
        return response()->json([
            'status' => true,
            'connected' => true,
            'msg' => 'Data tobii ditemukan (' . $id . ')',
            'data_tobii' => $data_tobii,
        ]);
    }

    /**
     * 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)
    {
        //
    }
}
