<?php

namespace AhmadFatoni\ApiGenerator\Controllers\API;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use LaravelSmpp\SmppServiceInterface;
use TPS\Birzha\Classes\SMPP as SMPPV2;
use TPS\Birzha\Classes\SMS;

class SmsController extends KabinetAPIController
{
    public function index(SmppServiceInterface $smpp) {
//        $smpp->sendOne(99363432211, 'Hi, this SMS was send via SMPP protocol');
        $tx=new SMPPV2('217.174.228.218', 5019); // make sure the port is integer
//        $tx->debug=true;
        $tx->bindTransmitter("birja","Birj@1");
//        dump('bind transmitter');
        $result = $tx->sendSMS("0773",'99363432211','message');
//        dump('send sms attempt');
//        echo $tx->getStatusMessage($result);
        $tx->close();
        unset($tx);
        return $result;

    }

    public function sendSmsCode()
    {
        if($this->user->dial_code != '+993') {
                return response()->json([
                        'dial_code' => $this->user->dial_code,
                        'result' => 1,
                        'message' => [
                                'ru' => trans('validation.api.user_not_tm_resident', [], 'ru'),
                                'en' => trans('validation.api.user_not_tm_resident', [], 'en'),
                                'tm' => trans('validation.api.user_not_tm_resident', [], 'tm'),
                        ]
                ], 400);
        }

        if($this->user->phone_verified && $this->user->dial_code == '+993') {
                return response()->json([
                        'result' => 1,
                        'message' => [
                                'ru' => trans('validation.api.phone_already_verified', [], 'ru'),
                                'en' => trans('validation.api.phone_already_verified', [], 'en'),
                                'tm' => trans('validation.api.phone_already_verified', [], 'tm'),
                        ]
                ], 200);
        }
        
        $code = random_int(1000, 9999);

        $result = SMS::send(str_replace(array('+', ' ', '(' , ')', '-'), '', $this->user->username), $code);
        // $result = 0;

        switch ($result) {
                case 0:
                        $this->user->phone_activation_code = $code;
                        $this->user->save();
                        return response()->json([
                                'result' => $result,
                                'message' => [
                                        'ru' => trans('validation.api.sms_sent', [], 'ru'),
                                        'en' => trans('validation.api.sms_sent', [], 'en'),
                                        'tm' => trans('validation.api.sms_sent', [], 'tm'),
                                ]
                        ], 201);
                        break;

                case 1:
                        return response()->json([
                                'result' => $result,
                                'message' => [
                                        'ru' => trans('validation.api.sms_not_sent_error', [], 'ru'),
                                        'en' => trans('validation.api.sms_not_sent_error', [], 'en'),
                                        'tm' => trans('validation.api.sms_not_sent_error', [], 'tm'),
                                ]
                        ], 500);
                        break;
                
                default:
                        return response()->json([
                                'result' => $result,
                                'message' => [
                                        'ru' => trans('validation.api.sms_not_sent_error', [], 'ru'),
                                        'en' => trans('validation.api.sms_not_sent_error', [], 'en'),
                                        'tm' => trans('validation.api.sms_not_sent_error', [], 'tm'),
                                ]
                        ], 500);
                        break;
        }
    }

    public function checkSmsCode(Request $request)
    {
        if($this->user->dial_code != '+993') {
                return response()->json([
                        'dial_code' => $this->user->dial_code,
                        'status' => false,
                        'message' => [
                                'ru' => trans('validation.api.user_not_tm_resident', [], 'ru'),
                                'en' => trans('validation.api.user_not_tm_resident', [], 'en'),
                                'tm' => trans('validation.api.user_not_tm_resident', [], 'tm'),
                        ]
                ], 400);
        }

        if($this->user->phone_verified && $this->user->dial_code == '+993') {
                return response()->json([
                        'status' => false,
                        'message' => [
                                'ru' => trans('validation.api.phone_already_verified', [], 'ru'),
                                'en' => trans('validation.api.phone_already_verified', [], 'en'),
                                'tm' => trans('validation.api.phone_already_verified', [], 'tm'),
                        ]
                ], 200);
        }
        
        $validator = Validator::make($request->all(), [
                'sms_code' => 'required|digits:4',
        ]);
        if($validator->fails()) {
            return response()->json($validator->errors(), 400);
        }

        if($this->user->phone_activation_code == $request->get('sms_code')) {
                $this->user->phone_verified = true;
                $this->user->phone_activation_code = null;
                $this->user->save();
                return response()->json([
                        'status' => true,
                        'message' => [
                                'ru' => trans('validation.api.phone_verified_message', [], 'ru'),
                                'en' => trans('validation.api.phone_verified_message', [], 'en'),
                                'tm' => trans('validation.api.phone_verified_message', [], 'tm'),
                        ]
                ], 201);
        } else {
                return response()->json([
                        'status' => false,
                        'message' => [
                                'ru' => trans('validation.api.phone_verification_code_invalid', [], 'ru'),
                                'en' => trans('validation.api.phone_verification_code_invalid', [], 'en'),
                                'tm' => trans('validation.api.phone_verification_code_invalid', [], 'tm'),
                        ]
                ], 400);
        }
    }
}
