<?php

namespace App\Services;

use Exception;
use Illuminate\Support\Facades\Log;

class AldGmService {
    
    protected $url = "http://aldzn.gm.xy.com/Player_no_login/getBindSend";
    protected $token = 'irgBmiKx1frWFiZvR11sPieBJmbi8kv7NXIAbX0Tlw';
    
    public function checkBindCode($bindCode)
    {
        $params = [
          'token' => $this->token,
          'code' => $bindCode,
          'type' => 2
        ];
        $resp = $this->getPostCurl($this->url, $params);
        $result = json_decode($resp, true);
        if ($result['result'] == 1) {
            return $result['data'];
        }else {
            Log::info("aldzn gm checkBindCode: " . $this->url. "?"  . http_build_query($params));
            Log::info("aldzn gm resp:" . $resp);
            return false;
        }
    }
    
    public function confirmBind($bindCode, $codeInfo)
    {
        $params = [
            'token' => $this->token,
            'code' => $bindCode,
            'type' => 1,
        ];
        $params = array_merge($params, $codeInfo);
        $resp = $this->getPostCurl($this->url, $params);
        $result = json_decode($resp, true);
        if ($result['result'] == 1) {
            return true;
        }else {
            Log::info("aldzn gm confirmBind: " . $this->url. "?"  . http_build_query($params));
            Log::info("aldzn gm resp:" . $resp);
            return false;
        }
    }
    
    public function getRoles($serverId, $userId, $channel)
    {
        $url = "http://aldzn.gm.xy.com/Player_no_login/getPlayerRole";
        $params = [];
        $params['server_id'] = $serverId;
        $params['uid'] = $userId;
        $params['channel'] = $channel;
        $params['token'] = "yjaMdF1epHNhoVlUBtXTmQwpehucOU60U0rQvUU63wh9OiOx51at6r";
        $resp = $this->getGetCurl($url, $params);
        $result = json_decode($resp, true);
        if (!isset($result['data']) || empty($result['data'])) {
            Log::info("aldzn gm url: " . $url ."?" . http_build_query($params));
            Log::info("aldzn gm resp:" . $resp);
            return false;
        }
        return $result['data'];       
    }
    

    public function sendRoleGift($serverId, $roleId, $vip, $channel)
    {
        $url = "http://aldzn.gm.xy.com/Player_no_login/getSendBindGift";
        $params = [];
        $params['role_id'] = $roleId;
        $params['server_id'] = $serverId;
        $params['vip'] = $vip;
        $params['channel'] = $channel;
        $params['token'] = "yjaMdF1epHNhoVlUBtXTmQwpehucOU60U0rQvUU63wh9OiOx51at6r";
        $resp = $this->getGetCurl($url, $params);
        $result = json_decode($resp, true);
        if (!isset($result['result']) || $result['result'] != 1) {
            Log::info("aldzn gm url: " . $url ."?" . http_build_query($params));
            Log::info("aldzn gm resp:" . $resp);
        }
        return true;
    }

    /**
     * 发送阿拉德道具接口
     * @return bool
     * @date 2021/05/26
     * @author live
     */
    public function getSendSignRoleGift($serverId, $roleId,$channel,$itemId,$itemNum,$subject,$content,$reason)
    {

        $url = "http://aldzn.gm.xy.com/Player_no_login/getSendSignGift";
        $params = [];

        $params['serverId'] = $serverId;
        $params['roleId'] = $roleId;
        $params['channel'] = $channel;
        $params['token'] = "yjaMdF1epHNhoVlUBtXTmQwpehucOU60U0rQvUU63wh9OiOx51at6r";
        $params['itemId'] = $itemId;
        $params['itemNum'] = $itemNum;
        $params['subject'] = $subject;
        $params['content'] =$content;
        $params['reason'] = $reason;
        $resp = $this->getGetCurl($url, $params);
        $result = json_decode($resp, true);
        if (!isset($result['result']) || $result['result'] != 1) {
            Log::info("aldzn gm url sign: " . $url ."?" . http_build_query($params));
            Log::info("aldzn gm resp sign:" . $resp);
            return false;
        }
        return true;
    }


    public function getCPRoleInfo($serverId,$userId,$roleId)
    {
        $url = 'http://8.129.63.135:59000/';
        $params = [];
        $params['zoneid'] = $serverId;
        $params['accid'] = $userId;
        $params['roleid'] = $roleId;
        $resp = $this->getGetCurl($url, $params);
        $result = json_decode($resp, true);
        if (isset($result['code']) || $result['result'] == 0) return $result['data'];
        Log::info("aldzn cp url info: " . $url ."?" . http_build_query($params));
        Log::info("aldzn cp resp info:" . $resp);
        return false;
    }

    protected function getPostCurl($url,$submitData)
    {
        try {
            $requestHeader[] = "API-RemoteIP: ";
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTPHEADER,$requestHeader);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($curl, CURLOPT_POST, count($submitData));
            curl_setopt($curl, CURLOPT_POSTFIELDS, $submitData);
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            curl_close($curl);
            return $result;
        }catch (Exception $e) {
            Log::error("aldzn gm curl:" . $e->getMessage());
            throw new Exception($e->getMessage());
        }
    }
    
    protected function getGetCurl($url,$submitData)
    {
        try {
            $curl = curl_init();
            $url .= "?" . http_build_query($submitData);
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");       
            curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $result = curl_exec($curl);
            curl_close($curl);
            return $result;
        }catch (Exception $e) {
            Log::error("aldzn gm curl:" . $e->getMessage());
            throw new Exception($e->getMessage());
        }
    }
    
}