<?php
include_once('../includes/crud.php');
include_once('../library/jwt.php');

function generate_token()
{
    $jwt = new JWT();
    $payload = [
        'iat' => time(),
        /* issued at time */
        'iss' => 'eKart',
        'exp' => time() + (30 * 60),
        /* expires after 1 minute */
        'sub' => 'eKart Authentication',
        'web' => '29260418',
        'cust' => '22015911',
        'admin' => '30288442'
    ];
    $token = $jwt::encode($payload, JWT_SECRET_KEY);
    return $token;
}
// generate_token();
// $token = generate_token();
// print_r($token);

function verify_token()
{
    $jwt = new JWT();
    $fn = new custom_functions;
    try {
        $token = $jwt->getBearerToken();
    } catch (Exception $e) {
        $response['error'] = true;
        $response['message'] = $e->getMessage();
        print_r(json_encode($response));
        return false;
    }
    if (!empty($token)) {
        try {
            // $payload = $jwt->decode($token, new key(JWT_SECRET_KEY, 'HS256'));
            $payload = $jwt->decode($token, new key(JWT_SECRET_KEY, 'HS256'));
            // $payload = $jwt->decode($token, JWT_SECRET_KEY, ['HS256']);
            if (!isset($payload->iss) || $payload->iss != 'eKart') {
                $response['error'] = true;
                $response['message'] = 'Invalid Hash';
                print_r(json_encode($response));
                return false;
            } else {
                /* web */
                // if (isset($payload->web) && $payload->web == '29260418') {
                //     /* fetch doctor_brown web and match item id here*/
                //   
                //     $purchase_code = $fn->get_settings('doctor_brown');
                //     $cal_time_check = $time_check = '';
                //     if (!empty($purchase_code)) {

                //         $isAuthWeb = json_decode($purchase_code);
                //         $time_check = $isAuthWeb->time_check;
                //         $token_web = trim($isAuthWeb->dr_firestone);
                //     }

                //     if ($token_web == '29260418') {
                //         return true;
                //     } else {

                //         $response['error'] = true;
                //         $response['message'] = 'Web purchase is not verified! Kindly check your purchase code registration!';
                //         print_r(json_encode($response));
                //         return false;
                //     }
                // }


                /* customer app */

                // if (isset($payload->cust) && $payload->cust == '22015911') {
                //     /* fetch doctor_brown web and match item id here*/
                //     $purchase_code = $fn->get_settings('doctor_brown_customer');
                //     $cal_time_check = $time_check = '';
                //     if (!empty($purchase_code)) {
                //         $isAuthCust = json_decode($purchase_code);
                //         $time_check = $isAuthCust->time_check;
                //         $token_cust = trim($isAuthCust->dr_firestone);
                //     }

                //     if ($token_cust == '22015911') {
                //         return true;
                //     } else {

                //         $response['error'] = true;
                //         $response['message'] = 'Customer app purchase is not verified! Kindly check your purchase code registration!';
                //         print_r(json_encode($response));
                //         return false;
                //     }
                // }

                /* admin app */

                // if (isset($payload->admin) && $payload->admin == '30288442') {
                //     /* fetch doctor_brown web and match item id here*/
                //     $purchase_code = $fn->get_settings('doctor_brown_admin');
                //     $cal_time_check = $time_check = '';
                //     if (!empty($purchase_code)) {
                //         $isAuthAdmin = json_decode($purchase_code);
                //         $time_check = $isAuthAdmin->time_check;
                //         $token_admin = trim($isAuthAdmin->dr_firestone);
                //     }

                //     if ($token_admin == '30288442') {
                //         return true;
                //     } else {

                //         $response['error'] = true;
                //         $response['message'] = 'Admin app purchase is not verified! Kindly check your purchase code registration!';
                //         print_r(json_encode($response));
                //         return false;
                //     }
                // }

                $purchase_code_web = (null !== $fn->get_settings('doctor_brown')) ? $fn->get_settings('doctor_brown') : "";
                $purchase_code_app = (null !== $fn->get_settings('doctor_brown_customer')) ? $fn->get_settings('doctor_brown_customer') : "";
                $purchase_code_adminapp = (null !== $fn->get_settings('doctor_brown_admin')) ? $fn->get_settings('doctor_brown_admin') : "";

                /** if all registerd  */
                if (!empty($purchase_code_web) && !empty($purchase_code_app) && !empty($purchase_code_adminapp)) {
                    if (isset($purchase_code_web) && !empty($purchase_code_web) && isset($purchase_code_app) && !empty($purchase_code_app) && isset($purchase_code_adminapp) && !empty($purchase_code_adminapp)) {

                        $isAuthWeb = isset($purchase_code_web) ? json_decode($purchase_code_web) : "";
                        $time_check = isset($isAuthWeb->time_check) ? $isAuthWeb->time_check : "";
                        $token_web = isset($isAuthWeb->dr_firestone) ? trim($isAuthWeb->dr_firestone) : "";

                        $isAuthApp = isset($purchase_code_app) ? json_decode($purchase_code_app) : "";
                        $time_check = isset($isAuthApp->time_check) ? $isAuthApp->time_check : "";
                        $token_app = isset($isAuthApp->dr_firestone) ? trim($isAuthApp->dr_firestone) : "";

                        $isAuthAdmin = isset($purchase_code_adminapp) ? json_decode($purchase_code_adminapp) : "";
                        $time_check = isset($isAuthAdmin->time_check) ? $isAuthAdmin->time_check : "";
                        $token_admin = isset($isAuthAdmin->dr_firestone) ? trim($isAuthAdmin->dr_firestone) : "";

                        if ($token_web == '29260418' && $token_app == '22015911' && $token_admin == '30288442') {


                            return true;
                        } else {
                            $response['error'] = true;
                            $response['message'] = 'System purchase is not verified! Kindly check your purchase code registration!';
                            print_r(json_encode($response));
                            return false;
                        }
                    }
                }


                /** if only one or two registerd */

                if (!empty($purchase_code_web) || !empty($purchase_code_app) || !empty($purchase_code_adminapp)) {
                    if (isset($purchase_code_web) && !empty($purchase_code_web) || isset($purchase_code_app) && !empty($purchase_code_app) || isset($purchase_code_adminapp) && !empty($purchase_code_adminapp)) {

                        $isAuthWeb = isset($purchase_code_web) ? json_decode($purchase_code_web) : "";
                        $time_check = isset($isAuthWeb->time_check) ? $isAuthWeb->time_check : "";
                        $token_web = isset($isAuthWeb->dr_firestone) ? trim($isAuthWeb->dr_firestone) : "";

                        $isAuthApp = isset($purchase_code_app) ? json_decode($purchase_code_app) : "";
                        $time_check = isset($isAuthApp->time_check) ? $isAuthApp->time_check : "";
                        $token_app = isset($isAuthApp->dr_firestone) ? trim($isAuthApp->dr_firestone) : "";

                        $isAuthAdmin = isset($purchase_code_adminapp) ? json_decode($purchase_code_adminapp) : "";
                        $time_check = isset($isAuthAdmin->time_check) ? $isAuthAdmin->time_check : "";
                        $token_admin = isset($isAuthAdmin->dr_firestone) ? trim($isAuthAdmin->dr_firestone) : "";

                        if ($token_web == '29260418' || $token_app == '22015911' || $token_admin == '30288442') {
                            return true;
                        } else {
                            $response['error'] = true;
                            $response['message'] = 'System purchase is not verified! Kindly check your purchase code registration!';
                            print_r(json_encode($response));
                            return false;
                        }
                    }
                }
            }
        } catch (Exception $e) {
            $response['error'] = true;
            $response['message'] = $e->getMessage();
            print_r(json_encode($response));
            return false;
        }
    } else {
        $response['error'] = true;
        $response['message'] = "Unauthorized access not allowed";
        print_r(json_encode($response));
        return false;
    }
}