<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Recorder\RecordController;
use Illuminate\Http\Request;
use App\Models\Mongo;
use Exception;
use App\Models\Server;
use App\Models\Datacase;

class CameraController extends Controller
{


    public function store(Request $r){
        if (isset($r->event)) {
            $event = $r->event;
            $this->$event($r);
        }
    }

    public function get_room($r){
        $room = getCONFIG("admin")->station_room;
        echo json_encode($room);
    }

    public function obs_record($r){
        // $tb_case = Mongo::table('tb_case')->where('_id', $r->cid)->first();
        // $temp['cid']        = $r->cid;
        // $temp['hn']         = $tb_case['hn'];
        // $temp['procedure']  = $tb_case['procedurename'];
        // $temp['department'] = $tb_case['department'];
        // $temp['scope']      = "1";
        // $json = jsonEncode($temp);
        //เขียนลงไฟล์เพื่อใช้ค่าเหล่านี้ในการบันทึก vdo
        // file_put_contents('D:/allindex/obs/case.temp', $json);
        //ทำการปิดไฟล์
    }


    public function update_local_room($r){

        $val['room'] = intval($r->room);
        $val['statusjob'] = "operation";
        $val['event'] = "update_case_room";
        $val['cid'] = $r->cid;
        $res = connectwebPOST("http://screening.endocapture.asia/api/camera", $val);
        echo $res;
    }


    public function update_case_room($r){

        $val['room'] = intval($r->room);
        $val['statusjob'] = "operation";
        Mongo::table('tb_case')->where('_id',$r->cid)->update($val);
    }


    public function room_save($r){
        $val['station_room'] = intval($r->room);
        Mongo::table('tb_config')->where('config_type','admin')->update($val);
    }


    public function back2home($r){

        $feature    = getCONFIG("feature");
        $is_photocaseuniq = false;
        $w[0] = array('_id',$r->cid);
        if(@$feature->photocaseuniq){
            $is_photocaseuniq = true;
            $w[0] = array('caseuniq', $r->caseuniq);
        }
        $is_photocaseuniq ? autocrop($r->caseuniq,$r->hn) : autocrop($r->cid,$r->hn);
        $case = $is_photocaseuniq ? (object) Mongo::table('tb_case')->where('caseuniq', $r->caseuniq)->first() : Datacase::first($r->cid);
        createTEMP('tb_case',$case->caseuniq,$case->comcreate,date("ymdHis"));
        NURSEMONITORreporting($case->caseuniq);
        SYSTEMQueue('operation',$case);
        Mongo::table("tb_case")->where($w)->update(['statusjob'=>"operation"]);
        fastsemi($r->cid);
    }


    public function scopetracking($r){
        $admin  = getCONFIG("admin");
        $room   = $admin->station_room;
        $sid    = intval($r->val);
        $scope  = SERVER::table("tb_scope")->where("scope_id",$sid)->first();
        $uid    = isset($r->uid) ? $r->uid : uid();
        if(isset($scope)){
            $rfid   = $scope["scope_rfid"];
            $serial = $scope["scope_serial"];
            if(isset($rfid) && isset($serial)){
                $val['track_rfid']      = $rfid;
                $val['track_user']      = $uid;
                $val['track_username']  = "endo";
                $val['track_time']      = date("H:i:s");
                $val['track_date']      = date("Y-m-d");
                $val['track_serial']    = $serial;
                $val['track_station']   = $room;
                $val['track_status']    = "success";
                $val['track_type']      = "capture";
                SERVER::table("tb_casetrack")->insert($val);

                $w1['scope_serial']  = $serial;
                $u['scope_status'] = 'capture';
                SERVER::table('tb_scope_update')->where($w1)->update($u);
            }
        }
    }

    public function recordvdo($r){
        // $tb_case    = Mongo::table("tb_case")->where("_id",$r->cid)->first();
        // if(isset($tb_case["video"])){
        //     array_push($tb_case["video"],$r->vdoname);
        // }else{
        //     $tb_case["video"] = array($r->vdoname);
        // }
        // $val["video"] = array_unique($tb_case["video"]);
        // Mongo::table("tb_case")->where("_id",$r->cid)->update($val);
    }

    public function set_lumina_config($r){
        $id = isset($r->_id) ?  $r->_id : '';
        $tb_lumina = Mongo::table('tb_lumina')->where('id', 1)->first();
        if(!isset($tb_lumina)){
            $arr['id'] = 1;
            Mongo::table('tb_lumina')->insert($arr);
        }
        try{
            $this_case = Mongo::table('tb_case')->where('_id', $id)->first();
            $this_case = (Object) $this_case;
            $data['patientname'] = isset($this_case->patientname) ? $this_case->patientname : '';
            $data['doctorname']  = isset($this_case->doctorname) ? $this_case->doctorname : '';
            $data['procedurename']   = isset($this_case->procedurename) ? $this_case->procedurename : '';
            $data['age'] = isset($this_case->age) ? $this_case->age : '';
            $data['hn'] = isset($this_case->hn) ? $this_case->hn : '';
            $data['appointment_date'] = isset($this_case->appointment_date) ? $this_case->appointment_date : '';
            $data['appointment'] = isset($this_case->appointment) ? $this_case->appointment : '';
            $data['cid'] = isset($id) ? $id : '';
            $data['open'] = 'capture';
            $data['event'] = 'case_data';
            $data['python'] = "true";
            $data['caseuniq'] = isset($this_case->caseuniq) ? $this_case->caseuniq : '';
            $json = json_encode($data);
            Mongo::table('tb_lumina')->where('id', 1)->update($data);
            return $json;
        } catch (Exception $e) {
            return '';
        }

    }

    public function recorder_action($r){
        $tb_lumina = Mongo::table('tb_lumina')->where('id',1)->first();
        if(!isset($tb_lumina)){
            $u['id'] = 1;
            Mongo::table('tb_lumina')->insert($u);
        }

        if(isset($r->cid)){
            $case = (object) Mongo::table('tb_case')->where('_id', $r->cid)->first();
            if(isset($case)){
                if($r->type == 'record_start'){
                    $data['case_status']  = 1;
                    $data['record_start'] = date('Y-m-d H:i:s');
                    $data['statusjob']    = 'operation';
                    Mongo::table('tb_case')->where('_id',$r->cid)->update($data);
                    try{
                        foreach (isset($r->config)?$r->config:[] as $key => $val) {
                            $u[$key] = $val;
                            Mongo::table('tb_lumina')->where('id', 1)->update($u);
                        }
                    } catch(\Exception $e){dd($e);}
                } else {

                }
            }

        }
    }

    public function set_data_config($r){
        $data = $r->data;
        $tb_lumina = Mongo::table('tb_lumina')->where('id',1)->first();
        if(!isset($tb_lumina)){
            $u['id'] = 1;
            Mongo::table('tb_lumina')->insert($u);
        }

        foreach(isset($data)?$data:[] as $key=>$val){
            $decode[$key] = $val;
        }
        $decode['python'] = 'true';
        Mongo::table('tb_lumina')->where('id',1)->update($decode);
        $case = Mongo::table('tb_case')->where('caseuniq', @$decode['caseuniq']."")->first() ?? [];
        // dd($decode, $case);
        echo jsonEncode($case);
        // return $case;
    }

    public function set_marker_height($r){
        try {
            setCONFIG('camera', 'marker_height', $r->height);
        } catch (Exception) {}
    }

    public function set_doctorname($r){
        // dd($r->all());
        $tb_user = Mongo::table("users")->where("id" , intval($r->value))->first();
        // dd($tb_user);
        $u['doctorname'] = fullname($tb_user);
        $u['case_physicians01'] = intval($r->value);
        $w[] = array('_id', $r->cid);
        Mongo::table('tb_case')->where($w)->update($u);
        echo $u['doctorname'];
    }


}
