<?php

namespace App\Facades\Otp;

use Illuminate\Http\Request;
use App\Facades\Sms\Sms;
use Carbon\Carbon;
use App\Models\Otp;
use SoapFault;
use SoapClient;

class OtpClass
{
    public function getOtpToken(Request $request, $ip, $phone_number, $message)
    {
        $otp = Otp::where([
            ['phone_number', $phone_number],
            ['created_at', '>', Carbon::now()->tomorrow()->toDateTimeString()] // check 1 day expired time
        ])->orderBy('created_at', 'desc')
            ->first();

        if ($otp) {
            Sms::sendSms($phone_number,  $message . " " . $otp->verification_code);
            // $this->sms_sender($phone_number, $message." ".$otp->verification_code);
            return true;
        }

        $verification_code = rand(100000, 999999);

        $otp = Otp::create([
            'phone_number' => $phone_number,
            'verification_code' => $verification_code,
            'ip' => $ip,
            'expired_at' => Carbon::now()->addMinute(2),
        ]);

        Sms::sendSms($phone_number,  $message . " " . $otp->verification_code);
        // $this->sms_sender($phone_number, $message." ".$otp->verification_code);
        return true;
    }

    public function verifyOtpToken($phone_number, $verification_code)
    {
        $otp = Otp::where([
            ['phone_number', $phone_number],
            ['used', 0],
            // ['created_at', '<', Carbon::now()->tomorrow()->toDateTimeString()], // check 1 day expired time ???? wtf?
            ['verification_code', $verification_code]
        ])->first();
        // dd($otp);
        if ($otp) {
            // $otp->update([
            //     'used' => 1
            // ]);

            return true;
        }
        return false;
    }


    public function useOtpToken($phone_number, $verification_code)
    {
        $otp = Otp::where([
            ['phone_number', $phone_number],
            ['used', 0],
            ['created_at', '>', Carbon::now()->tomorrow()->toDateTimeString()], // check 1 day expired time
            ['verification_code', $verification_code]
        ])->first();

        if ($otp) {
            $otp->update([
                'used' => 1
            ]);

            return true;
        }
        return false;
    }


    public function sms_sender($mobile, $msg)
    {

        $client = new SoapClient("http://sms1000.ir/webservice/sms.asmx?WSDL");
        $date = date('d-m-y h:i:s');
        $date = str_replace(" ", "T", $date);
        $result = $client->Send(
            array(
                'Username' => 'admin',
                'Password' => 'admin141',
                'ShortCode' => '1000141',
                'Cellphones' => $mobile,
                'Message' => $msg,
                'Farsi' => true,
                // 'SendDate' => $date
                // 'uTopic' => false,
                // 'uFlash' => false
            )
        );

        $x = $result->SendResult;

        return $x;

        // $client = new SoapClient("http://sms1000.ir/webservice/smspro.asmx?WSDL");
        // $result = $client->doSendSMS(
        //     array(
        //         'uUsername' => 'rms',
        //         'uPassword' => 'Rms@141',
        //         'uNumber' => '1000141',
        //         'uCellphones' => $mobile,
        //         'uMessage' => $msg,
        //         'uFarsi' => true,
        //         'uTopic' => false,
        //         'uFlash' => false
        //     )
        // );
        // $x = $result->doSendSMSResult;
        // dd($x);
        // return $x;
    }
}
