<?php

namespace App\Http\Controllers\Cash;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Login\AccountInfo;
use App\Models\User\CashShopAll;
use App\Models\Web\HistoryShopBuyedItem;

class CashShopController extends Controller
{
    public function inquiryBalance(Request $request)
    {
        if(isset($request['userId'])){

            $userID = $request['userId'];
            $realBalance = 0;
            $bonusBalance = 0;

            $cashAccount = AccountInfo::where('Username', $userID)->first();
            if($cashAccount){
                $realBalance = $cashAccount->Real_Balance != null ? $cashAccount->Real_Balance : 0;
                $bonusBalance = $cashAccount->Bonus_Balance != null ? $cashAccount->Bonus_Balance : 0;

                return response()->json([
                    'result' => 'success',
                    'realCash' => intval($realBalance),
                    'bonusCash' => intval($bonusBalance)
                ],400);
            }

        }

        return response()->json([
            'service_code' => '',
            'Result' => -97
        ],400);
    }

    public function nexonCash(Request $request)
    {
        if(isset($request['userId'])){

            $userID = $request['userId'];
            $realBalance = 0;
            $bonusBalance = 0;

            $cashAccount = AccountInfo::where('Username', $userID)->first();
            if($cashAccount){
                $realBalance = $cashAccount->Real_Balance != null ? $cashAccount->Real_Balance : 0;
                $bonusBalance = $cashAccount->Bonus_Balance != null ? $cashAccount->Bonus_Balance : 0;

                return response()->json([
                    'result' => 'success',
                    'realCash' => intval($realBalance),
                    'bonusCash' => intval($bonusBalance)
                ],400);
            }

        }

        return response()->json([
            'service_code' => '',
            'Result' => -97
        ],400);
    }

    public function purchaseItem(Request $request)
    {
        $makeCodeNo = 0;
        $userNo = 0;
        $userId = '';
        $itemId = '';
        $itemCnt = 0;
        $itemUnitPrice = 0;
        $gameServerNo = 0;
        $statProperty1 = '';
        $statProperty2 = '';
        $statProperty3 = '';
        $location = '';
        $clientIp = '';
        $channelingUserId = '';
        $charId = 0;

        $data = explode('&', $_SERVER['QUERY_STRING']);

        foreach ($data as $param) {
            list($name, $value) = explode('=', $param, 2);

            if ($name == 'makeCodeNo') {
                $makeCodeNo = $value;
            }
            if ($name == 'userNo') {
                $userNo = $value;
            }
            if ($name == 'userId') {
                $userId = $value;
            }
            if ($name == 'itemInfos[0].itemId') {
                $itemId = $value;
            }
            if ($name == 'itemInfos[0].itemCnt') {
                $itemCnt = $value;
            }
            if ($name == 'itemInfos[0].itemUnitPrice') {
                $itemUnitPrice = $value;
            }
            if ($name == 'gameServerNo') {
                $gameServerNo = $value;
            }
            if ($name == 'statProperty1') {
                $statProperty1 = $value;
            }
            if ($name == 'statProperty2') {
                $statProperty2 = $value;
            }
            if ($name == 'statProperty3') {
                $statProperty3 = $value;
            }
            if ($name == 'location') {
                $location = $value;
            }
            if ($name == 'ClientIp') {
                $clientIp = $value;
            }
            if ($name == 'ChannelingUserId') {
                $channelingUserId = $value;
            }
            if ($name == 'charId') {
                $charId = $value;
            }
        }

        $datajson = null;
        $userId = str_replace('@KLONER', '', $userId);
        $Account = AccountInfo::where('Username', $userId)->first();
        $chekProdInfo = CashShopAll::GetProduct($itemId);

        if ($chekProdInfo != null) {

            $linkItemID = $chekProdInfo[0]->LinkItemID;
            $itemPrase = $chekProdInfo[0]->Price;
            $itemCount = $chekProdInfo[0]->Count;

            if ($itemCnt == $itemCount) {

                $realBalance = $Account->Real_Balance != null ? $Account->Real_Balance : 0;
                $bonusBalance = $Account->Bonus_Balance != null ? $Account->Bonus_Balance : 0;
                $totalBalance = $realBalance + $bonusBalance;

                if ($totalBalance >= $itemPrase) {

                    $totalBalance = $totalBalance - $itemPrase;

                    AccountInfo::UpdateBalance($Account->Username, $totalBalance);

                    HistoryShopBuyedItem::SaveHistory($Account->AccountDBID, $linkItemID, $itemCount, $itemPrase);
                    
                    $datajson = '{
                        "statProperty2": null,
                        "statProperty1": "' . $statProperty1 . '",
                        "statProperty3": "success",
                        "realCash": ' . $realBalance . ',
                        "bonusCash": 0,
                        "chargedCashAmt": ' . $itemPrase . ',
                        "itemInfos": [
                            {
                                "itemCnt": ' . $itemCount . ',
                                "itemId": "' . $linkItemID . '",
                                "itemUnitPrice": ' . $itemPrase . '
                            }
                        ]
                    }';

                    die($datajson);

                }else{
                    $datajson = [
                        'statProperty2' => '1009996'
                    ];
                } 

            }else{
                $datajson = [
                    'statProperty2' => '1001002'
                ];
            }

        }else{
            $datajson = [
                'statProperty2' => '1001002'
            ]; 
        }

        return response()->json($datajson, 200);
    }

    // Получение RecId и LinkItemID предметов из таблицы CashShopAll
    public function getItemsDetails(Request $request)
    {
        // возвращает RecId и LinkItemID предметов из таблицы CashShopAll
        $itemDetails = CashShopAll::all(['RecId', 'LinkItemID']);

        return response()->json($itemDetails, 200);
    }

}
