<?php 
namespace App\Traits;

trait GeneralTrait{

    public function returnError($errNum, $msg){
        return response()->json([
            'status' => false,
            'errNum' => $errNum,
            'msg' => $msg
        ], $errNum);
    }

    public function returnSuccessMessage($msg = "", $errNum = 200){
        return response()->json([
            'status' => true,
            'errNum' => $errNum,
            'msg' => $msg
        ], $errNum);
    }

    public function returnData($key, $value, $msg = "", $resultCout){
        return response()->json([
            'status' => true,
            'errNum' => 200,
            'msg' => $msg,
            'RecordsTotal' => $resultCout,
            $key => $value
        ]);
    }

    public function returnCodeAccordingToInput($validator){
        $inputs = array_keys($validator->errors()->toArray());
        $code = $this -> getErrorCode($inputs[0]);
        return $code;
    }

    public function getErrorCode($input){
        if($input == "name") return "E0011";
        else if ($input == "owner_password") return "E002";
    }

    public function returnValidationError($code = "E001", $validator){
        return $this -> returnError($code, $validator->errors()->first());
    }

    public function sendExpoNotification($expoToken, $title, $body, $orderStatus){
        $client = new \GuzzleHttp\Client();
        $client->post(
            'https://exp.host/--/api/v2/push/send',
            array('form_params' => array(
                'to' => $expoToken,
                'title' => $title,
                'body'=> $body,
                'sound' => 'default',
                // '_displayInForeground' => true, 
                'data' => ['order_status' => $orderStatus]
            ))
        );
    }

    public function sendLoginExpoNotification($expoToken, $title, $body, $option){
        $client = new \GuzzleHttp\Client();
        $client->post(
            'https://exp.host/--/api/v2/push/send',
            array('form_params' => array(
                'to' => $expoToken,
                'title' => $title,
                'body'=> $body,
                'sound' => 'default',
                // '_displayInForeground' => true, 
                'data' => ['isLoggedinFromAnotherDevice' => $option]
            ))
        );
    }

    public function sendOnesignalNotification($playerID, $title, $message){
        $fields['include_player_ids'] = [$playerID]; // to send one signal notification by playerID
        // $fields['included_segments'] = ["Active Users"]; // to send one signal notification by segment
        // $message = 'hey!! this is test push.!'; 
        $fields['headings'] = array(
            "en" => $title
        );
        $fields['contents'] = array(
            "en" => $message
        );
        return \OneSignal::sendPush($fields);
    }

    public function sendSMS($message, $phone_no){
        $client = new \GuzzleHttp\Client();
        $client->post(
            'http://basic.unifonic.com/rest/SMS/messages',
            array('form_params' => array(
                'AppSid' => env('UNIFONIC_APP_ID'),
                'SenderID' => env('UNIFONIC_SENDER_ID'),
                'Body'=> $message,
                'Recipient' => $phone_no
            ))
        );
    }
}
?>
