<?php

namespace App\Services;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class FMSService
{
    public function getVehicleActivity(Request $request)
    {
        if (App::isProduction()) {
            $r = $request->machineCode;
            $s = $request->startDT;
            $e = $request->endDT;
            $d = $request->minStopDuration;
            $url = 'http://fms.141.ir/api/RMS/GetVehicleActivity?';
            $query = "username=RMS&password=XSW@1qaz&machineCode={$r}&startDT={$s}&endDT={$e}&minStopDuration={$d}";
            $full_url = $url . $query;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $full_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                "username=RMS&password=XSW@1qaz&machineCode={$r}&startDT={$s}&endDT={$e}&minStopDuration={$d}");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            return curl_exec($ch);
        }

        return json_encode([
            'ResultCode' => 0
        ]);
    }

    public function getVehicleData(Request $request)
    {
        if (App::isProduction()) {
            $r = $request->machine;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, "http://fms.141.ir/api/RMS/GetVehicleLastPoints?username=RMS&password=XSW@1qaz&machineCodes={$r}");
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                "username=RMS&password=XSW@1qaz&machineCodes={$r}");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            return curl_exec($ch);
        }

        $output = (object)['ResultCode' => 0];

        return json_encode([$output]);
    }

    public function getVehicleLastPoints(Request $request)
    {
        if (App::isProduction()) {
            $r = $request->machineCode;
            $url = 'http://fms.141.ir/api/RMS/GetVehicleLastPoints?';
            $query = "username=RMS&password=XSW@1qaz&machineCodes={$r}";
            $full_url = $url . $query;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $full_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                "username=RMS&password=XSW@1qaz&machineCode={$r}");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            return curl_exec($ch);
        }

        return json_encode([
            'ResultCode' => 0
        ]);
    }

    public function getVehicleActivitySummary(Request $request)
    {
        if (App::isProduction()) {
            $r = $request->machineCode;
            $s = $request->startDT;
            $e = $request->endDT;
            $url = 'http://fms.141.ir/api/RMS/GetVehicleActivitySummary?';
            $query = "username=RMS&password=XSW@1qaz&machineCode={$r}&startDT={$s}&endDT={$e}";
            $full_url = $url . $query;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $full_url);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS,
                "username=RMS&password=XSW@1qaz&machineCode={$r}&startDT={$s}&endDT={$e}");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            return curl_exec($ch);
        }

        return json_encode([
            'ResultCode' => 0
        ]);
    }
}