<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\DeviceModel;
use Illuminate\Http\Request;

class DeviceApiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $result = DeviceModel::all();
        $count = DeviceModel::all()->count();
        if($count) {
            return response([
                'result'    =>  $result,
                'message'   =>  'Device Berhasil Ditampilkan',
                'status'    =>  1
            ],200);
        } else {
            return response([
                'result'    =>  null,
                'message'   =>  'Device Tidak Ada',
                'status'    =>  0
            ],404);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $result = DeviceModel::where("uuid",$id)->first();
        $count = DeviceModel::where("uuid",$id)->first();
        if($count) {
            return response([
                'result'    =>  $result,
                'message'   =>  'Device Berhasil Ditampilkan',
                'status'    =>  1
            ],200);
        } else {
            return response([
                'result'    =>  null,
                'message'   =>  'Device Tidak Ditemukan',
                'status'    =>  0
            ],404);
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $count = DeviceModel::where('uuid',$id)->count();
        if($count) {
            try {
                DeviceModel::where("uuid",$id)->update([
                    'nama_device'   =>  $request->nama_device,
                    'imei'  =>  $request->imei,
                    'lat'   =>  $request->lat,
                    'lng'   =>  $request->lng,
                    'location'  =>  $request->location,
                    'depth' =>  $request->depth,
                    'depth_sensor'  =>  $request->depth_sensor,
                    'status_run'    =>  $request->status_run
                ]);
                $result = DeviceModel::where('uuid',$id)->first();
                return response([
                    'result'    =>  $result,
                    'message'   =>  'Device Berhasil Diupdate',
                    'status'    => 1
                ],200);
            } catch (\Throwable $th) {
                return response([
                    'result'    =>  null,
                    'message'   =>  'Device Gagal Diupdate',
                    'status'    =>  0
                ],401);
            }
        } else {
            return response([
                'result'    =>  null,
                'message'   =>  'Device Tidak Ditemukan',
                'status'    =>  0
            ],404);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
