<?php
namespace common\helpers;

use common\lib\Mt4Api;
use common\lib\Mt4Deposit;
use Yii;
use yii\httpclient\Client;

class MtKit
{
    /**
     * 获取MT4分组
     * @return array
     */
    public static function getGroups()
    {
        $result = [];
        $client = new Client();
        // http://192.168.1.100:9090/MtService/getGroups
        $response = $client->get(Yii::$app->params['mtHttpServerUrl'] . "/MtService/getGroups")->send();
        if ($response->getIsOk()) {
            $content = $response->getContent();
            $jsonArr = @json_decode($content, true);
            foreach ((array)$jsonArr as $key => $row) {
                $result[] = isset($row['Name']) ? trim($row['Name']) : '';
            }
        } else {
            Yii::warning($response->getContent(), __CLASS__);
        }
        return $result;
    }

    /**
     * 入金
     * @param int $login
     * @param float $balance
     * @param string $comment
     * @return bool
     */
    public static function deposit($login, $balance, $comment)
    {
        $mt4 = new Mt4Deposit();
        $result = $mt4->deposit($login, $balance, $comment);
        if ($result['errCode'] == 0) {
            return true;
        }
        /*
        $client = new Client();
        $data = [
            'login' => $login,
            'balance' => $balance,
            'comment' => $comment,
        ];
        // http://192.168.1.100:9090/MtService/deposit?login=Int32&balance=Double&comment=String
        $response = $client->get(Yii::$app->params['mtHttpServerUrl'] . "/MtService/deposit", $data)->send();
        if ($response->getIsOk()) {
            $content = $response->getContent();
            $jsonArr = @json_decode($content, true);
            if ($jsonArr['Ret'] == 0) {
                return true;
            }
        }
        Yii::warning($response->getContent(), __CLASS__);
        */
        return false;
    }

    /**
     * 出金
     * @param int $login
     * @param float $balance
     * @param string $comment
     * @return bool
     */
    public static function withdrawal($login, $balance, $comment)
    {
        $mt4 = new Mt4Deposit();
        $result = $mt4->withdraw($login, $balance, $comment);
        if ($result['errCode'] == 0) {
            return true;
        }
        /*
        $client = new Client();
        $data = [
            'login' => $login,
            'balance' => $balance,
            'comment' => $comment,
        ];
        // http://192.168.1.100:9090/MtService/withdrawal?login=Int32&balance=Double&comment=String
        $response = $client->get(Yii::$app->params['mtHttpServerUrl'] . "/MtService/withdrawal", $data)->send();
        if ($response->getIsOk()) {
            $content = $response->getContent();
            $jsonArr = @json_decode($content, true);
            if ($jsonArr['Ret'] == 0) {
                return true;
            }
        }
        Yii::warning($response->getContent(), __CLASS__);
        */
        return false;
    }

    /**
     * 真实账户开户
     * @param int $login
     * @param string $password
     * @param string $passwordInvestor
     * @param string $passwordPhone
     * @param string $name
     * @param int $group
     * @param string $email
     * @param string $phone
     * @param int $leverage
     * @param string $country
     * @param int $state
     * @param string $city
     * @param string $address
     * @param string $zipCode
     * @param int $id
     * @param int $agentAccount
     * @return bool|int
     */
    public static function openUser($login, $password, $passwordInvestor, $passwordPhone, $name, $group, $email,
                                    $phone, $leverage, $country, $state, $city, $address, $zipCode, $id, $agentAccount)
    {
        /*
        $data = [
            'LOGIN' => $login,
            'PASSWORD' => $password,
            'INVESTOR' => $passwordInvestor,
            'PHONE_PASSWORD' => $passwordPhone,
            'NAME' => $name,
            'GROUP' => $group,
            'EMAIL' => $email,
            'PHONE' => $phone,
            'LEVERAGE' => $leverage,
            'COUNTRY' => $country,
            'STATE' => $state,
            'CITY' => $city,
            'ADDRESS' => $address,
            'ZIPCODE' => $zipCode,
            'ID' => $id,
            'AGENT_ACCOUNT' => $agentAccount,
        ];
        $api = new Mt4Api();
        $result = $api->addAccount($data, 1);
        if ($result['Ret'] == 0) {
            return trim($result['Obj']['Login']);
        } else {
            Yii::warning(json_encode($result), __CLASS__);
        }
        */
        $data = [
            'login' => $login,
            'password' => $password,
            'passwordInvestor' => $passwordInvestor,
            'passwordPhone' => $passwordPhone,
            'name' => $name,
            'group' => $group,
            'email' => $email,
            'phone' => $phone,
            'leverage' => $leverage,
            'country' => $country,
            'state' => $state,
            'city' => $city,
            'address' => $address,
            'zipCode' => $zipCode,
            'id' => $id,
            'agentAccount' => $agentAccount,
        ];
        $client = new Client();
        $response = $client->get(Yii::$app->params['mtHttpServerUrl'] . "/MtService/openUser", $data)->send();
        Yii::warning($response->getContent(), __CLASS__);
        if ($response->getIsOk()) {
            $content = $response->getContent();
            $jsonArr = @json_decode($content, true);
            if ($jsonArr['Ret'] == 0) {
                return trim($jsonArr['Obj']['Login']);
            } else {
                return 0;
            }
        }
        return false;
    }

    /**
     * 模拟账户开户
     * @param null $login
     * @param string $password
     * @param string $passwordInvestor
     * @param string $passwordPhone
     * @param string $name
     * @param null $group
     * @param string $email
     * @param string $phone
     * @param null $leverage
     * @param null $country
     * @param null $state
     * @param null $city
     * @param null $address
     * @param null $zipCode
     * @param null $id
     * @param null $agentAccount
     * @return bool|int|string
     */
    public static function openUserDemo($login = null, $password, $passwordInvestor, $passwordPhone, $name, $group = null,
                                        $email, $phone, $leverage = null, $country = null, $state = null, $city = null,
                                        $address = null, $zipCode = null, $id = null, $agentAccount = null)
    {
        /*
        $data = [
            'PASSWORD' => $password,
            'INVESTOR' => $passwordInvestor,
            'PHONE_PASSWORD' => $passwordPhone,
            'NAME' => $name,
            'EMAIL' => $email,
            'PHONE' => $phone,
        ];
        $api = new Mt4Api();
        $result = $api->addAccount($data, 0);
        if ($result['Ret'] == 0) {
            return $result['Obj']['Login'];
        } else {
            Yii::warning(json_encode($result), __CLASS__);
        }
        */
        $data = [
            'password' => $password,
            'investor' => $passwordInvestor,
            'phone_password' => $passwordPhone,
            'username' => $name,
            'email' => $email,
            'phone' => $phone,
        ];
        $client = new Client();
        $response = $client->get("http://live.taikong88.com/index/index/openDemo", $data)->send();
        if ($response->getIsOk()) {
            $content = $response->getContent();
            $jsonArr = @json_decode($content, true);
            if ($jsonArr['Ret'] == 0) {
                return trim($jsonArr['Obj']['Login']);
            } else {
                return 0;
            }
        }
        Yii::warning($response->getContent(), __CLASS__);
        return false;
    }
}