<?php

defined('BASEPATH') or exit('deirect script access not allowed');

function request_method($method)
{
    if ($_SERVER['REQUEST_METHOD'] !== $method) {
        echo json_encode([
            'status' => '0',
            'message' => 'method not allowed',
            'details' => []
        ]);
        exit;
    } else {
        return 1;
    }
}

// function

function check_parameter($array)
{
    // print_r($_POST);
    // exit;
    $a = '';

    $final = [];
    $v = 0;
    foreach ($array as $arr => $val) {

        if ($val == 1) {

            if (empty($_POST[$arr]) || !$_POST[$arr]) {

                $message = $arr . ' parameter is required.';

                $final[] = $message;

                $v = 1;
            } else {

                $final[$arr] = $_POST[$arr];
            }
        } else {

            if (empty($_POST[$arr])) {

                unset($_POST[$arr]);
            } else {

                $final[$arr] = $_POST[$arr];
            }
        }
    }

    if ($v == 0) {

        return $final;
    } else {
        echo json_encode([
            'status' => '0',
            'message' => 'parameter required',
            'details' => $final
        ]);
        exit;
    }
}

function check_parameter_web($array)
{

    $a = '';

    $final = [];

    foreach ($array as $arr) {

        if (empty($_POST[$arr]) || !$_POST[$arr]) {

            $message = $arr . ' parameter is required.';

            $final[] = $message;
        } else {
            $a .= $_POST[$arr];
        }
    }

    if (!empty($final)) {


        echo json_encode([
            'status' => '0',
            'message' => 'parameter required',
            'details' => $final
        ]);
        exit;
    }
}

function check_get_parameter($array)
{



    $a = '';

    $final = [];

    foreach ($array as $arr => $val) {

        if ($val == 1) {

            if (empty($_GET[$arr]) || !$_GET[$arr]) {

                $message = $arr . ' parameter is required.';

                $final[] = $message;
            }
        } else {

            if (empty($_GET[$arr])) {

                unset($_GET[$arr]);
            }
        }
    }

    if (empty($final)) {

        $final = $_GET;

        return $final;
    } else {
        echo json_encode([
            'status' => '0',
            'message' => 'parameter required',
            'details' => $final
        ]);
        exit;
    }
}

function get_token($length = 64)
{
    $stringSpace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $pieces = [];
    $max = mb_strlen($stringSpace, '8bit') - 1;
    for ($i = 0; $i < $length; ++$i) {
        $pieces[] = $stringSpace[random_int(0, $max)];
    }
    return implode('', $pieces);
}

function giftNotification($regId, $message, $type, $loginId, $userId, $purchasedCoinstotal, $receivedCointotal)
{
    if (empty($checkMuteNotifiaton)) {
        $registrationIds =  array($regId);
        define('API_ACCESS_KEY', 'AAAA0WgbM-c:APA91bGsdT5rx9u_QmaG9cjyapHiAY6NrzMw_WbMXgEuQi0f7ZbZ6-GCQZUDU6PvHY52KBBYwqC_Dxc9a22la0ad84p1tblRcK2evrYM2ONKU-Oa0xuL9d9jgkMrTynDadMO2OL1VIJz');
        $msg = array(
            'message'     => $message,
            'title'        => 'LiveBazaar',
            'type'        => $type,
            'subtitle'    => $type,
            'loginId' => $loginId,
            'userId' => $userId,
            'purchasedCoins' => $purchasedCoinstotal,
            'receivedCoin' => $receivedCointotal,
            'vibrate'    => 1,
            'sound'        => 1,
            'largeIcon'    => 'large_icon',
            'smallIcon'    => 'small_icon',
        );
        $fields = array(
            'registration_ids'     => $registrationIds,
            'data'            => $msg
        );
        $headers = array(
            'Authorization: key=' . API_ACCESS_KEY,
            'Content-Type: application/json'
        );
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => "https://fcm.googleapis.com/fcm/send",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => json_encode($fields),
            CURLOPT_HTTPHEADER => $headers

        ));

        $response = curl_exec($curl);


        $err = curl_error($curl);
        curl_close($curl);
    }
}

function upload_media($image)
{
    // print_r($image);exit;
    $target_file = 'assets/images/media/';
    $name = $image['name'];

    $target_file .= $name;

    if (move_uploaded_file($image["tmp_name"], $target_file)) {


        return base_url($target_file);
    }
}
