<?php

namespace App\Services;

use Exception;
use Illuminate\Support\Facades\Http;

class LmsApi
{
    public static $headerKey = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc";

    public static function http()
    {
        return Http::withHeaders([
            'id' => self::$headerKey
        ]);
    }

    public static function baseUrl()
    {
        return env('LMS_API_URL', 'http://lmsapidev.primaya.id');
    }

    /**
     * jadwalDokter
     *
     * @param  string $HISKodeRS HIS RS Code example: C for Primaya Tangerang
     * @param  string $HISKodeDokter HIS Doctor Code example: SPKG_01
     * @param  string $type HIS Slot Type walkin, teleconsultation
     * @return void
     */
    public static function jadwalDokter($HISKodeRS, $HISKodeDokter, $type)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dr' => $HISKodeDokter,
        ];
        if (!empty($type) && $type == 'walkin') {
            $data['channel'] = 'Website';
        }

        $res = self::http()->post(self::baseUrl() . '/jadwaldokter', $data)->json();

        // Reformat Jam Data
        $res['data'] = collect($res['data'])->map(function ($day) {

            $day['Jam'] = !empty($day['Jam']) ? explode(', ', $day['Jam']) : null;
            return $day;
        })->toArray();

        return $res;
    }

    /**
     * dokter
     * 
     * 
     */

    public static function dokter($kodeRs, $channel = 'hospitaloka', $timeout = 30)
    {
        $data = [
            'rs' => $kodeRs,
            'channel' => $channel,
        ];

        $res = self::http()->timeout($timeout)->post('http://lmsapi.primaya.id/dokter', $data)->json();

        return $res;
    }

    /**
     * jadwalDokterTersedia
     *
     * @param  string $HISKodeRS HIS RS Code example: C for Primaya Tangerang
     * @param  string $HISKodeDokter HIS Doctor Code example: SPKG_01
     * @param  string $tanggalBooking
     * @param  string $type HIS Slot Type walkin, teleconsultation
     * @return void
     */
    public static function jadwalDokterTersedia($HISKodeRS, $HISKodeDokter, $tanggalBooking, $type)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dr' => $HISKodeDokter,
            'tanggalbooking' => $tanggalBooking
        ];
        if (!empty($type) && $type == 'walkin') {
            $data['channel'] = 'Website';
        }

        $res = self::http()->post(self::baseUrl() . '/jadwaldoktertersedia', $data)->json();

        // Reformat Jam Data
        $res['data'] = collect($res['data'])->map(function ($day) {

            // Change jam to Jam to Standarize
            $day['Jam'] = !empty($day['jam']) ? explode(', ', $day['jam']) : null;
            unset($day['jam']);
            return $day;
        })->toArray();

        return $res;
    }

    /**
     * checkMedrec
     *
     * @param  string $HISKodeRS
     * @param  array $patientData
     * @return void
     */
    public static function checkMedrec($HISKodeRS, $patientData)
    {
        $data = [
            'rs' => $HISKodeRS,
            'nm' => $patientData['name'],
            'tgl' => $patientData['birth_date'],
            'ktp' => $patientData['nik'],
            'telp' => $patientData['phone'],
        ];

        if (
            empty($patientData['name']) ||
            empty($patientData['birth_date']) ||
            empty($patientData['nik']) ||
            empty($patientData['phone'])
        ) {
            throw new Exception('name, birth_date, nik, phone are required in $patientData');
        }

        $res = self::http()->post(self::baseUrl() . '/medrec_nama', $data)->json();

        return $res;
    }

    /**
     * checkGantung
     *
     * @param  string $HISKodeRS
     * @param  array $patientData
     * @return void
     */
    public static function checkGantung($HISKodeRS, $HISMedrec, $patientData)
    {
        $data = [
            'rs' => $HISKodeRS,
            'rm' => $HISMedrec,
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
        ];

        if (
            empty($patientData['name']) ||
            empty($patientData['birth_date'])
        ) {
            throw new Exception('name, birth_date are required in $patientData');
        }

        $res = self::http()->post(self::baseUrl() . '/cek_gantung', $data)->json();

        return $res;
    }

    /**
     * checkSlot
     *
     * @param  string $HISKodeRS
     * @param  string $HISKodeDokter
     * @param  string $tanggalAppointment
     * @param  string $jamAppointment
     * @param  string $type HIS Slot Type walkin, teleconsultation
     * @return void
     */
    public static function checkSlot($HISKodeRS, $HISKodeDokter, $tanggalAppointment, $jamAppointment, $type)
    {

        $data = [
            'rs' => $HISKodeRS,
            'dr' => $HISKodeDokter,
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
        ];
        if (!empty($type) && $type == 'walkin') {
            $data['channel'] = 'Website';
        }

        $res = self::http()->post(self::baseUrl() . '/cek_slot', $data)->json();

        return $res;
    }

    /**
     * appointmentBaru
     *
     * @param  string $HISKodeRS
     * @param  string $HISKodeDep
     * @param  string $HISKodeDokter
     * @param  string $jenisTC
     * @param  string $tanggalAppointment
     * @param  string $jamAppointment
     * @param  string $jumlahBayar
     * @param  string $kodePembayaran
     * @param  array $patientData
     * @param  mixed $userId
     * @return void
     */
    public static function appointmentBaru($HISKodeRS, $HISKodeDep, $HISKodeDokter, $jenisTC, $tanggalAppointment, $jamAppointment, $jumlahBayar, $kodePembayaran, $patientData, $userId)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'jenistc' => $jenisTC,
            'dr' => $HISKodeDokter,
            'uid' => $userId,
            'rm' =>  'BARU',
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
            'tempatlahir' => $patientData['birth_place'],
            'jnskelamin' => $patientData['gender'],
            'goldar' => $patientData['blood_type'],
            'agama' => $patientData['religion'],
            'pendidikan' => $patientData['last_education'],
            'pekerjaan' => $patientData['current_employment'],
            'perkawinan' => $patientData['marital_status'],
            'nomorktp' => $patientData['nik'],
            'kewarganegaraan' => $patientData['citizenship'],
            'alamat' => $patientData['address'],
            'kota' => $patientData['city_name'],
            'alamatdomisili' => $patientData['domicile_address'],
            'kotadomisili' => $patientData['domicile_city_name'],
            'email' => $patientData['email'],
            'telepon' => $patientData['phone'],
            'byr' => $jumlahBayar,
            'id_bayar' => $kodePembayaran,
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
        ];

        $res = self::http()->post(self::baseUrl() . '/appointment_baru', $data)->json();

        return $res;
    }

    /**
     * appointment
     *
     * @param  mixed $HISKodeRS
     * @param  mixed $HISKodeDep
     * @param  mixed $HISKodeDokter
     * @param  mixed $jenisTC
     * @param  mixed $tanggalAppointment
     * @param  mixed $jamAppointment
     * @param  mixed $jumlahBayar
     * @param  mixed $kodePembayaran
     * @param  mixed $HISMedrec
     * @param  mixed $patientData
     * @param  mixed $userId
     * @return void
     */
    public static function appointment($HISKodeRS, $HISKodeDep, $HISKodeDokter, $jenisTC, $tanggalAppointment, $jamAppointment, $jumlahBayar, $kodePembayaran, $HISMedrec, $patientData, $userId)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'dr' => $HISKodeDokter,
            'jenistc' => $jenisTC,
            'uid' => $userId,
            'rm' => $HISMedrec,
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
            'email' => $patientData['email'],
            'telepon' => $patientData['phone'],
            'byr' => $jumlahBayar,
            'id_bayar' => $kodePembayaran,
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
        ];

        $res = self::http()->post(self::baseUrl() . '/appointment', $data)->json();

        return $res;
    }

    /**
     * appointmentEdit
     *
     * @param  mixed $HISRegid
     * @param  mixed $HISKodeRS
     * @param  mixed $HISKodeDep
     * @param  mixed $HISKodeDokter
     * @param  mixed $tanggalAppointment
     * @param  mixed $jamAppointment
     * @param  mixed $HISMedrec
     * @param  mixed $patientData
     * @return void
     */
    public static function appointmentEdit($HISRegid, $HISKodeRS, $HISKodeDep, $HISKodeDokter, $tanggalAppointment, $jamAppointment, $HISMedrec, $patientData)
    {
        $data = [
            'regid' => $HISRegid,
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'dr' => $HISKodeDokter,
            'rm' => $HISMedrec,
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
            'email' => $patientData['email'],
            'telepon' => $patientData['phone'],
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
        ];

        $res = self::http()->post(self::baseUrl() . '/appointment_edit', $data)->json();

        return $res;
    }

    /**
     * booking
     *
     * @param  mixed $HISKodeRS
     * @param  mixed $HISKodeDep
     * @param  mixed $HISKodeDokter
     * @param  mixed $tanggalAppointment
     * @param  mixed $jamAppointment
     * @param  mixed $HISMedrec
     * @param  mixed $patientData
     * @param  mixed $jenisPenjamin 0: Umum, 1: Perusahaan, 2: Asuransi, 3: BPJS
     * @param  mixed $namaPenjamin
     * @param  mixed $nomorKartuBPJS
     * @param  mixed $nomorRujukanBPJS
     * @return void
     */
    public static function booking($HISKodeRS, $HISKodeDep, $HISKodeDokter, $tanggalAppointment, $jamAppointment, $HISMedrec, $patientData, $jenisPenjamin = 0, $namaPenjamin = '', $nomorKartuBPJS = null, $nomorRujukanBPJS = null)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'dr' => $HISKodeDokter,
            'uid' => 'Website',
            'rm' => $HISMedrec,
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
            'email' => $patientData['email'],
            'telepon' => $patientData['phone'],
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
            'jenispenjamin' => $jenisPenjamin,
            'penjamin' => $namaPenjamin,
            'ktp' => $patientData['nik'],
            'nokartu' => $nomorKartuBPJS,
            'norujukan' => $nomorRujukanBPJS
        ];

        $res = self::http()->post(self::baseUrl() . '/booking', $data)->json();

        return $res;
    }

    /**
     * bookingBaru
     *
     * @param  mixed $HISKodeRS
     * @param  mixed $HISKodeDep
     * @param  mixed $HISKodeDokter
     * @param  mixed $tanggalAppointment
     * @param  mixed $jamAppointment
     * @param  mixed $patientData
     * @param  mixed $jenisPenjamin
     * @param  mixed $namaPenjamin
     * @param  mixed $nomorKartuBPJS
     * @param  mixed $nomorRujukanBPJS
     * @return void
     */
    public static function bookingBaru($HISKodeRS, $HISKodeDep, $HISKodeDokter, $tanggalAppointment, $jamAppointment, $patientData, $jenisPenjamin = 0, $namaPenjamin = '', $nomorKartuBPJS = null, $nomorRujukanBPJS = null)
    {
        $data = [
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'dr' => $HISKodeDokter,
            'uid' => 'Website',
            'nm' => $patientData['name'],
            'tgllahir' => $patientData['birth_date'],
            'tmptlahir' => $patientData['birth_place'],
            'jk' => $patientData['gender'],
            'goldar' => $patientData['blood_type'],
            'agama' => $patientData['religion'],
            'pendidikan' => $patientData['last_education'],
            'pekerjaan' => $patientData['current_employment'],
            'perkawinan' => $patientData['marital_status'],
            'nomorktp' => $patientData['nik'],
            'kewarganegaraan' => $patientData['citizenship'],
            'propinsi' => null,
            'kabupaten' => null,
            'kecamatan' => null,
            'kelurahan' => null,
            'alamat' => $patientData['address'],
            'email' => $patientData['email'],
            'telepon' => $patientData['phone'],
            'tgl' => $tanggalAppointment,
            'jam' => $jamAppointment,
            'jenispenjamin' => $jenisPenjamin,
            'penjamin' => $namaPenjamin,
            'nokartu' => $nomorKartuBPJS,
            'norujukan' => $nomorRujukanBPJS
        ];

        $res = self::http()->post(self::baseUrl() . '/booking_baru', $data)->json();

        return $res;
    }

    /**
     * bookingEdit
     * Used for Rescheduling appointment
     *
     * @param  mixed $kodeBooking
     * @param  mixed $HISKodeRS
     * @param  mixed $HISKodeDep
     * @param  mixed $HISKodeDokter
     * @param  mixed $tanggalAppointment
     * @param  mixed $jamAppointment
     * @param  mixed $tanggalAppointmentBaru
     * @param  mixed $jamAppointmentBaru
     * @param  mixed $jenisPenjamin
     * @param  mixed $namaPenjamin
     * @param  mixed $nomorKartuBPJS
     * @param  mixed $nomorRujukanBPJS
     * @return void
     */
    public static function bookingEdit($kodeBooking, $HISKodeRS, $HISKodeDep, $HISKodeDokter, $tanggalAppointment, $jamAppointment, $tanggalAppointmentBaru, $jamAppointmentBaru, $jenisPenjamin = 0, $namaPenjamin = '', $nomorKartuBPJS = null, $nomorRujukanBPJS = null)
    {
        $data = [
            'kodebooking' => $kodeBooking,
            'rs' => $HISKodeRS,
            'dep' => $HISKodeDep,
            'dr' => $HISKodeDokter,

            'jenispenjamin' => $jenisPenjamin,
            'penjamin' => $namaPenjamin,
            'uid' => 'Website',
            'tgllama' => $tanggalAppointment,
            'jamlama' => $jamAppointment,
            'tglbaru' => $tanggalAppointmentBaru,
            'jambaru' => $jamAppointmentBaru,
        ];

        $res = self::http()->post(self::baseUrl() . '/booking_edit', $data)->json();

        return $res;
    }

    public static function bookingPaket($kodeRs, $kodeDep,  $rm, $ktp, $nama_pasien, $tgl_lahir, $email, $telp, $tgl, $jam, $jenis_penjamin, $penjamin, $paket, $jenis_paket)
    {
        throw new Exception("Not Implemented", 1);

        $url = Http::withHeaders([
            'id' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc'
        ])->post('http://lmsapidev.primaya.id/booking_paket', [
            'rs' => $kodeRs,
            'dep' => $kodeDep,
            'uid' => 'Website',
            'rm' => $rm,
            'ktp' => $ktp,
            'nm' => $nama_pasien,
            'tgllahir' => $tgl_lahir,
            'email' => $email,
            'telepon' => $telp,
            'tgl' => $tgl,
            'jam' => $jam,
            'jenispenjamin' => $jenis_penjamin,
            'penjamin' => $penjamin,
            'paket' => $paket,
            'jenispaket' => $jenis_paket,
        ])->json();

        return $url;
    }
    public static function bookingBaruPaket(
        $kodeRs,
        $kodeDep,
        $nm,
        $tgl_lahir,
        $tempat_lahir,
        $jenis_kelamin,
        $goldar,
        $agama,
        $pendidikan,
        $pekerjaan,
        $perkawinan,
        $nomorktp,
        $kewarganegaraan,
        $propinsi,
        $kabupaten,
        $kecamatan,
        $kelurahan,
        $alamat,
        $email,
        $telepon,
        $tgl,
        $jam,
        $jenis_penjamin,
        $penjamin,
        $paket,
        $jenis_paket
    ) {
        throw new Exception("Not Implemented", 1);

        $url = Http::withHeaders([
            'id' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc'
        ])->post('http://lmsapidev.primaya.id/booking_baru_paket', [
            'rs' => $kodeRs,
            'dep' => $kodeDep,
            'uid' => 'Website',
            'rm' => 'BARU',
            'nm' => $nm,
            'tgllahir' => $tgl_lahir,
            'tmptlahir' => $tempat_lahir,
            'jk' => $jenis_kelamin,
            'goldar' => $goldar,
            'agama' => $agama,
            'pendidikan' => $pendidikan,
            'pekerjaan' => $pekerjaan,
            'perkawinan' => $perkawinan,
            'nomorktp' => $nomorktp,
            'kewarganegaraan' => $kewarganegaraan,
            'propinsi' => $propinsi,
            'kabupaten' => $kabupaten,
            'kecamatan' => $kecamatan,
            'kelurahan' => $kelurahan,
            'alamat' => $alamat,
            'email' => $email,
            'telepon' => $telepon,
            'tgl' => $tgl,
            'jam' => $jam,
            'jenispenjamin' => $jenis_penjamin,
            'penjamin' => $penjamin,
            'paket' => $paket,
            'jenispaket' => $jenis_paket,
        ])->json();

        return $url;
    }

    public static function bookingEditPaket(
        $kodeRs,
        $kodeDep,

        $kodeBooking,
        $tgllama,
        $jamlama,
        $tglbaru,
        $jambaru,
        $jenis_penjamin,
        $penjamin,
        $paket,
        $jenis_paket
    ) {
        throw new Exception("Not Implemented", 1);

        $url = Http::withHeaders([
            'id' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc'
        ])->post('http://lmsapidev.primaya.id/booking_edit_paket', [
            'rs' => $kodeRs,
            'dep' => $kodeDep,
            'uid' => 'Website',
            'kodebooking' => $kodeBooking,
            'tgllama' => $tgllama,
            'jamlama' => $jamlama,
            'tglbaru' => $tglbaru,
            'jambaru' => $jambaru,
            'jenispenjamin' => $jenis_penjamin,
            'penjamin' => $penjamin,
            'paket' => $paket,
            'jenispaket' => $jenis_paket,
        ])->json();

        return $url;
    }

    public static function bookingBayar($kodeBooking, $HISKodeRS, $tanggalBooking, $jumlahBayar, $merchantCode, $merchantOrderId, $reference)
    {
        $data = [
            "rs" => $HISKodeRS,
            "uid" => "Website",
            "tglbooking" => $tanggalBooking,
            "kodebooking" => $kodeBooking,
            "amount" => $jumlahBayar,
            "merchantCode" => $merchantCode,
            'merchantorderid' => $merchantOrderId,
            'reference' => $reference,
        ];

        $res = self::http()->post(self::baseUrl() . '/booking_bayar', $data)->json();

        return $res;
    }

    public static function bookingBatal($kodeRs, $kodeBooking, $tglBooking)
    {
        $url = Http::withHeaders([
            'id' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc'
        ])->post('http://lmsapidev.primaya.id/booking_batal', [
            "rs" => $kodeRs,
            "channel" => "Website",
            "kodebooking" => $kodeBooking,
            "tgl" => $tglBooking,
        ])->json();

        return $url;
    }

    public static function cekRujukan($kodeRs, $noKartu, $noRujukan)
    {
        $url = Http::withHeaders([
            'id' => 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjE3NWZiNDI0LTA4MzMtNGZiMS1iOWNhLWQwMzQ5Nzc'
        ])->post('http://lmsapidev.primaya.id/cek_rujukan', [
            "rs" => $kodeRs,
            "uid" => "Website",
            "nokartu" => $noKartu,
            "norujukan" => $noRujukan,
        ])->json();

        return $url;
    }
}
