<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use DB;
use Illuminate\Support\Facades\Crypt;
use App\Models\Customer_model;
use App\Jobs\SendEmailAktifasiUserJob;

class DebugController extends Controller
{
	protected $uri;
    protected $client_id;
    protected $client_secret;
    protected $api_secret;
    protected $api_key;
    protected $corporate_id;
    protected $account_number;

    protected $auth;
    protected $access_token;
    protected $signature_bca;

    public function index()
    {
        $this->uri = env('API_BCA_URI');
        $this->client_id = env('API_BCA_CLIENTID');
        $this->client_secret = env('API_BCA_CLIENTSECRET');
        $this->api_secret = env('API_BCA_APISECRET');
        $this->api_key = env('API_BCA_APIKEY');
        $this->corporate_id = env('API_BCA_CORPORATEID');
        $this->account_number = env('API_BCA_ACCOUNTNUMBER');

    	$this->auth = base64_encode("{$this->client_id}:{$this->client_secret}");
        $this->auth = "Basic {$this->auth}";

        $this->get_access_token_bca();
    }

    private function get_access_token_bca()
    {
        $uri = "{$this->uri}/api/oauth/token";
        $client = new Client();
        $result = $client->post($uri, [
            'headers' => [
                'Content-Type' => 'application/x-www-form-urlencoded',
                'Authorization' => $this->auth
            ],
            'form_params' => [
                'grant_type' => 'client_credentials'
            ]
        ]);

        $array = json_decode($result->getBody(), true);

        $this->access_token = $array['access_token'];
    }

    private function get_mutasi()
    {
        $start_date = date('Y-m-d', strtotime('-7 days'));
        $end_date = date('Y-m-d');

        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');

        $relative_url = "/banking/v3/corporates/{$this->corporate_id}/accounts/{$this->account_number}/statements?EndDate={$end_date}&StartDate={$start_date}";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();
            dd($responseBody);
        }
        $array = json_decode($result->getBody(), true);
        echo "<pre>";
        print_r($array);
        //return $array['Data'];
    }

    private function get_saldo()
    {
        $start_date = date('Y-m-d');
        $end_date = date('Y-m-d');

        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');

        $relative_url = "/banking/v3/corporates/{$this->corporate_id}/accounts/{$this->account_number}";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();
            dd($responseBody);
        }
        $array = json_decode($result->getBody(), true);
        print_r($array);
        //return $array['Data'];
    }

    private function fund_transfer_bca($nomor_rekening_tujuan, $amt, $remark1, $remark2)
    {
        $unique_number = rand(10000000,99999999);
        $body = [
            'CorporateID' => $this->corporate_id,
            'SourceAccountNumber' => $this->account_number,
            'TransactionID' => $unique_number,
            'TransactionDate' => date('Y-m-d'),
            'ReferenceID' => '9892902641',
            'CurrencyCode' => 'IDR',
            'Amount' => $amt . '.00',
            'BeneficiaryAccountNumber' => $nomor_rekening_tujuan,
            'Remark1' => $remark1,
            'Remark2' => $remark2
        ];

        $body = json_encode($body);
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');

        $relative_url = "/banking/corporates/transfers";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "POST:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature = hash_hmac('sha256', $StringToSign, $this->api_secret);

        $headers = [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => "Bearer {$this->access_token}",
                'Origin' => 'test-cron.lakuemas.com',
                'X-BCA-Key' => $this->api_key,
                'X-BCA-Timestamp' => $Timestamp,
                'X-BCA-Signature' => $signature
            ],
            'body' => $body
        ];

        $client = new Client();
        $result = $client->post($uri, $headers);
        $array = json_decode($result->getBody(), true);

        if($array['Status'] == 'Success') return true;
        else return false;
    }

    private function get_currency()
    {
        $body = '';
        $Timestamp = date('Y-m-d')."T".date('H:i:s').".000".date('P');
        $relative_url = "/general/rate/forex?CurrencyCode=USD,XXX&RateType=erate";
        $uri = "{$this->uri}{$relative_url}";

        $encode_body = strtolower(hash('sha256', $body));
        $StringToSign = "GET:{$relative_url}:{$this->access_token}:{$encode_body}:{$Timestamp}";
        $signature_bca = hash_hmac('sha256', $StringToSign, $this->api_secret);

        try {
            $client = new Client();
            $result = $client->get($uri, [
                'headers' => [
                    'Content-Type' => 'application/x-www-form-urlencoded',
                    'Authorization' => "Bearer {$this->access_token}",
                    'Origin' => 'cron.lakuemas.com',
                    'X-BCA-Key' => $this->api_key,
                    'X-BCA-Timestamp' => $Timestamp,
                    'X-BCA-Signature' => $signature_bca
                ]
            ]);
        } catch (RequestException $e) {
            $responseBody = $e->getMessage();
            dd($responseBody);
        }
        $array = json_decode($result->getBody(), true);
        print_r($array);
        //return $array['Data'];
    }
}