<?php

namespace App\Http\Controllers\Game;


use App\Facade\TableName;
use App\Game\GameCard;
use App\Game\GlobalUserInfo;
use App\Game\Route;
use App\Http\Controllers\Api\ApiController;
use App\Http\Controllers\Controller;
use App\Models\AccountsInfo;
use App\Services\IpCheck;
use App\Util;
use App\Utility\SetNXLock;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;


// use Yansongda\Pay\Log;

class GameTestController extends Controller
{
    public function gameLunch()
    {
        $api = 'https://int.stage.onlyplay.net/api/get_frame';


        $baseInfo = [
            'game_bundle' => 'juicycrush',
            'callback_url' => 'https://br.24b.pro',
            'partner_id' => 949,
            'user_id' => strval(4478930),
            'balance' => 23527,
            'currency' => env('CONFIG_24680_CURRENCY'),
            'decimals' => 2,
            'lang' => 'liang',
            'token' => sha1(time())
        ];
        $baseInfo['sign'] = $this->_sign($baseInfo);

        $headers = [
            'Content-Type' => 'application/json',
        ];

        $rs = Util::curlPost2($api,json_encode($baseInfo),true,$headers);

        dd($rs);

    }

    private function _sign($params)
    {
        $secretKey = 'd4ecdd3835395886eaaffb20d4192159b4f8b4cd15b52763952dabb2b6e5cfe25aa6c8a8594b2480c3766ce7dac3296465fb09091c91e602d34ceddc';
        ksort($params);

        $sign_string = '';

        foreach ($params as $key => $value) {
            if(is_array($value)){
                $value = json_encode($value);
            }

            $sign_string .= $key.$value;
        }

        $sign = sha1($sign_string . $secretKey);

        return $sign;
    }

    private function _checkSign($data){
        //return true;
        if(!is_array($data) ||!$data['sign']){
            return false;
        }
        Util::WriteLog('callback_data',$data);
        $sign = $data['sign'];
        unset($data['sign']);
        return  $sign==$this->_sign($data);
    }

    public function platformInfo(Request $request)
    {
        $post = $request->post();
        //Util::WriteLog('game_test_data',$post);
        if (!is_array($post)) {
            $post = \GuzzleHttp\json_decode($post, true);
        }
        if(!$this->_checkSign($post)){
            return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
        }
        $UserID = $post['user_id'];
        $AccountsInfoModel = new AccountsInfo();
        // 用户余额
        $score = $AccountsInfoModel->Score($UserID);
        return ['success' => true,'balance' => intval($score)];
    }

    public function platformBet(Request $request)
    {
        $post = $request->post();
        if (!is_array($post)) {
            $post = \GuzzleHttp\json_decode($post, true);
        }
        if(!$this->_checkSign($post)){
            return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
        }

        $res = SetNXLock::getExclusiveLock('bet'.$post['tx_id'], 86400);
        if (!$res) {
            return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
        }

        $UserID = $post['user_id'];
        $AccountsInfoModel = new AccountsInfo();
        // 用户余额
        $score = intval($AccountsInfoModel->Score($UserID));
        $bet = intval($post['amount']);
        if($bet>$score){
            return ['success' => false,'code' => 5001,'message' => 'Insufficient funds'];
        }
        DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->decrement('Score', $bet);
        //TODO 记录下注记录 记下对局id信息
        return ['success' => true,'balance' => intval($score)-$bet];
    }

    public function platformWin(Request $request)
    {
        $post = $request->post();
        if (!is_array($post)) {
            $post = \GuzzleHttp\json_decode($post, true);
        }
        if(!$this->_checkSign($post)){
            return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
        }

        $res = SetNXLock::getExclusiveLock('win_'.$post['tx_id'], 86400);
        if (!$res) {
            return ['success' => false,'code' => 2401,'message' => 'Session not found or expired.'];
        }

        $UserID = $post['user_id'];
        $AccountsInfoModel = new AccountsInfo();
        // 用户余额
        $score = intval($AccountsInfoModel->Score($UserID));
        $win = intval($post['amount']);
        DB::connection('write')->table('QPTreasureDB.dbo.GameScoreInfo')->where('UserID', $UserID)->increment('Score', $win);
        //TODO 记录下注记录 记下对局id信息
        return ['success' => true,'balance' => intval($score)+$win];
    }

    public function platformCancel(Request $request)
    {
        $post = $request->post();
        if (!is_array($post)) {
            $post = \GuzzleHttp\json_decode($post, true);
        }
        if(!$this->_checkSign($post)){
            return ['success' => false,'code' => 3100,'message' => 'Sent data is not secure, wrong sign'];
        }

        $UserID = $post['user_id'];
        $AccountsInfoModel = new AccountsInfo();
        // 用户余额
        $score = intval($AccountsInfoModel->Score($UserID));
        //TODO 记录下注记录 记下对局id信息
        return ['success' => true,'balance' => intval($score)];
    }
}
