<?php
require_once 'app/Helpers.php';
/* Client Settings */
$liveapi    = 'https://control.locowiz.com/public/';
$stageapi   = 'http://app.timesspot.com/public/';
$company    = 'v-mart';
$clientHost = 'stores.vmartretail.com';
/* End Client Settings */

$app_data = [];
$postData = [];
$app_data['POWERED_BY'] = 'LocoWiz';
$app_data['POWERED_BY_LINK'] = 'https://www.locowiz.com/?utm_source=vmartretail&utm_medium=Store-Locator&utm_campaign=locations_tracker';
$app_data['STG_HOST'] = 'vmart.timesspot.com';

if($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == 'localhost:8888'){
    $app_data['APP_URL'] = 'http://'.$_SERVER['HTTP_HOST'].'/vmart';
    $app_data['APP_DEBUG'] = true;
    $app_data['APP_ENV'] = 'local';
} elseif($_SERVER['HTTP_HOST'] == $app_data['STG_HOST']){
    $app_data['APP_URL'] = 'https://'.$_SERVER['HTTP_HOST'];
    $app_data['APP_DEBUG'] = true;
    $app_data['APP_ENV'] = 'local';
} else {
    $app_data['APP_URL'] = 'https://'.$_SERVER['HTTP_HOST'];
    $app_data['APP_DEBUG'] = false;
    $app_data['APP_ENV'] = 'production';
}
if($_SERVER['HTTP_HOST'] == $clientHost){
    $app_data['API_COMPANY'] = $company;
    $base_url = $liveapi;
    $postData['client_id'] = 1;
    $postData['client_secret'] = 'kSGfU1MNQdfB43u9gfVv7d3h82jzN6UDiOhXoIM1';
} else {
    $app_data['API_COMPANY'] = $company;
    $base_url = $stageapi;
    $postData['client_id'] = 1;
    $postData['client_secret'] = 'OXabvK3zQelP4uLNZVnDDnvYP2zlpJpKa03WGHcz';
}
if(empty(extension_loaded('curl'))){
    echo 'Please install PHP CURL extension.';
    exit;
}
require_once 'app/EnvSetting.php';
$env_object = new EnvSetting();

$header   = [];
$postData['grant_type'] = 'client_credentials';
$header[] = 'Content-Type: application/json';

$variables_file_path = 'storage/framework/variables.json';
if(!file_exists($variables_file_path)){
    exit('Config file not exist');
}
if(!is_writable($variables_file_path)){
    exit('Config file do not have permissions.');
}
$token_variables = json_decode(file_get_contents($variables_file_path),true);
$app_data['APP_KEY'] = 'base64:rM4va1rxboyCrw7ofn01KYKRdiCEvVPGl/7AwJ2+/gA=';

// if($_GET['debug']=!null && $_GET['debug'] == 'true'){
// $app_data['APP_DEBUG'] = true;
// }

$app_data['API_BASE_URL'] = $base_url.'api';
$token_url                = $base_url.'oauth/token';
$update_api_url = $app_data['API_BASE_URL'].'/'.$app_data['API_COMPANY'].'/branddate';
$data_api_url = $app_data['API_BASE_URL'].'/'.$app_data['API_COMPANY'].'/company';
$last_updated_file = 0;
$last_updated_api = 1;

//Generate Token If not Exists
if(empty($token_variables['access_token'])){
    $res = $env_object->curl_post($token_url,$postData);
    if(!empty($res['access_token'])){
        $app_data['access_token'] = $res['access_token'];
    } else {
      //  exit('Auth token missing');
    }
} else {
    $app_data['access_token'] = $token_variables['access_token'];
}

//Add Bearer Token in Header if Exhist.
$header[] = 'Authorization: Bearer '.$app_data['access_token'];
$upt_variable = json_decode(file_get_contents($variables_file_path),true);
if(!empty($upt_variable['updated_at'])){
    $last_updated_file = $upt_variable['updated_at'];
}
// hit last updated api
//if(!empty($_GET['update_brand_data'])){
//$update_api_response = $env_object->curl_hit($update_api_url,$header);
//if(!empty($update_api_response['data']['updated_at'])) {
//    $last_updated_api = $update_api_response['data']['updated_at'];
//}
//if($last_updated_file < $last_updated_api || empty($token_variables['access_token']) || empty($token_variables['POWERED_BY_LINK']) || ($token_variables['POWERED_BY_LINK'] != $app_data['POWERED_BY_LINK'])) {
//    // hit brand data api
//    $data_api_response = $env_object->curl_hit($data_api_url,$header);
//    // write data in variable file 
//    if(!empty($data_api_response['data']['brands'])){
//        $fp = fopen($variables_file_path, 'w');
//        fwrite($fp, json_encode(array_merge($app_data,$data_api_response['data']['brands']),JSON_PRETTY_PRINT));
//        fclose($fp);
//        file_get_contents($app_data['APP_URL'].'/clear-cache');
//    } 
//}
//}


$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public'.$uri)) {
    return false;
}

require_once __DIR__.'/public/index.php';